Skip to content
Back to skills

Concurrent Programming

ASecurity

多线程并发开发大师——从goroutine/async到分布式锁的完整实战指南。覆盖:Go并发模型、Python async/await、Node.js事件循环、Java虚拟线程、线程安全设计模式、锁策略、无锁编程、并发测试。触发词:并发、多线程、goroutine、async/await、线程安全、锁、race condition、deadlock、互斥锁、Channel、协程、同步原语

  • 2 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 6, 2026
developmentpythonrustgojavabashnodetestingapi

Works with

  • cli
  • api

Security analysis

A100/100

Scanned October 6, 2026

npx -y skills add aAAaqwq/openclaw-team --skill concurrent-programming --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Concurrent Programming?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Concurrent Programming
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/aaaaqwq-concurrent-programming/badge)](https://www.skillsdirectory.com/skills/aaaaqwq-concurrent-programming)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: concurrent-programming
description: "多线程并发开发大师——从goroutine/async到分布式锁的完整实战指南。覆盖:Go并发模型、Python async/await、Node.js事件循环、Java虚拟线程、线程安全设计模式、锁策略、无锁编程、并发测试。触发词:并发、多线程、goroutine、async/await、线程安全、锁、race condition、deadlock、互斥锁、Channel、协程、同步原语"
---

# 🔄 多线程并发开发大师 — Concurrent Programming

> **版本**:v1.0 | **角色**:轩辕 CTO | **来源**:14年分布式系统沉淀

---

## 一、并发模型全景

### 1.1 各语言并发模型对比

| 语言 | 单位 | 通信方式 | 调度 | OS线程比 | 适用场景 |
|------|------|---------|------|---------|---------|
| **Go** | goroutine | Channel + select | M:N调度 | 1000:1 | 高并发服务、微服务 |
| **Python** | async/await | Future/Queue | 事件循环 | 1:1(io) | IO密集、API服务 |
| **Node.js** | Event Loop + Worker | Promise/Message | 单线程+Woker | 1:1(io)+W | IO密集、前端服务 |
| **Java** | 虚拟线程+Platform | Executor/Future | Loom | ∞:1 | 企业应用 |
| **Rust** | std::thread+async | Channel + Mutex | 1:1+无栈 | 1:1 | 系统级、性能关键 |
| **Elixir** | Actor/Process | Message passing | M:N | 10000:1 | 电信级高可用 |

### 1.2 轩辕的并发三定律

```
定律1: 能用Channel解决的问题,不要用Mutex
└── 消息传递优于共享内存(Go的核心理念)

定律2: 并发不一定是并行
└── 并发是关于结构,并行是关于执行

定律3: 先做对,再做快
└── 如果你不确定是不是线程安全,那就不是
```

---

## 二、Go并发模型(轩辕首选)

### 2.1 goroutine与Channel

```go
package main

// 基础模式:生产者-消费者
func producer(ch chan<- int) {
    for i := 0; i < 100; i++ {
        ch <- i
    }
    close(ch)
}

func consumer(ch <-chan int) {
    for v := range ch {
        fmt.Printf("消费: %d\n", v)
    }
}

func main() {
    ch := make(chan int, 10)  // 缓冲channel,防止背压
    go producer(ch)
    consumer(ch)
}

// Fan-Out / Fan-In 模式(最常用)
func fanOut[T any](input <-chan T, workers int) []<-chan T {
    channels := make([]<-chan T, workers)
    for i := 0; i < workers; i++ {
        ch := make(chan T, 100)
        channels[i] = ch
        go func(out chan<- T) {
            for v := range input {
                out <- v
            }
            close(out)
        }(ch)
    }
    return channels
}

func fanIn[T any](inputs ...<-chan T) <-chan T {
    out := make(chan T, 100)
    var wg sync.WaitGroup
    for _, ch := range inputs {
        wg.Add(1)
        go func(c <-chan T) {
            defer wg.Done()
            for v := range c {
                out <- v
            }
        }(ch)
    }
    go func() {
        wg.Wait()
        close(out)
    }()
    return out
}
```

### 2.2 高级模式

```go
// 超时控制
func doWithTimeout(fn func() Result, timeout time.Duration) (Result, error) {
    resultCh := make(chan Result, 1)
    go func() {
        resultCh <- fn()
    }()
    
    select {
    case res := <-resultCh:
        return res, nil
    case <-time.After(timeout):
        return Result{}, errors.New("timeout")
    }
}

// 并发限流(信号量模式)
type Semaphore chan struct{}

func NewSemaphore(max int) Semaphore {
    return make(Semaphore, max)
}

func (s Semaphore) Acquire()    { s <- struct{}{} }
func (s Semaphore) Release()    { <-s }

// 使用
sem := NewSemaphore(10)  // 最多10个并发
for _, task := range tasks {
    sem.Acquire()
    go func(t Task) {
        defer sem.Release()
        process(t)
    }(task)
}
```

---

## 三、Python异步实战

```python
import asyncio
import aiohttp
from asyncio import Semaphore

# 高并发HTTP请求(API网关常用)
async def fetch_url(sem: Semaphore, session: aiohttp.ClientSession, url: str):
    async with sem:  # 限流
        async with session.get(url) as resp:
            return await resp.json()

async def batch_fetch(urls: list[str], max_concurrent: int = 10):
    sem = Semaphore(max_concurrent)
    async with aiohttp.ClientSession() as session:
        tasks = [fetch_url(sem, session, url) for url in urls]
        return await asyncio.gather(*tasks)

# 生产者-消费者(asyncio Queue)
async def worker(name: str, queue: asyncio.Queue):
    while True:
        item = await queue.get()
        try:
            print(f"Worker {name}: processing {item}")
            await asyncio.sleep(1)  # 模拟耗时
        finally:
            queue.task_done()

async def main():
    queue = asyncio.Queue(maxsize=100)
    workers = [asyncio.create_task(worker(f"W{i}", queue)) for i in range(5)]
    
    # 生产100个任务
    for i in range(100):
        await queue.put(f"task-{i}")
    
    await queue.join()  # 等待所有任务完成
    for w in workers:
        w.cancel()
```

---

## 四、线程安全问题诊断

### 4.1 常见并发Bug模式

| Bug模式 | 描述 | 检测方法 | 修复方案 |
|---------|------|---------|---------|
| **Data Race** | 多线程同时读写同一变量 | `go run -race` | 加锁/用Channel |
| **Deadlock** | 两个线程互相等锁 | pprof/goroutine dump | 锁顺序统一/超时 |
| **活锁** | 线程都在让路但都没进展 | 观察吞吐量 | 随机退避 |
| **饥饿** | 低优先级线程得不到执行 | Lock Contention监控 | 公平锁/增加资源 |
| **ABA问题** | CAS时值被改回原值 | 带版本号的CAS | `atomic.Value`+计数器 |
| **伪共享** | 多核缓存行冲突 | perf cache-miss | Cache Line Padding(64B对齐) |

### 4.2 诊断工具链

```bash
# Go
go run -race main.go                    # Data Race检测
GODEBUG=gctrace=1 go run main.go        # GC追踪
pprof goroutine                         # goroutine状态
pprof mutex                             # 锁争用

# Python
python -m trace --trace main.py         # 执行追踪
faulthandler.enable()                   # 崩溃时dump线程

# Node.js
node --experimental-report main.js      # 诊断报告
node --prof main.js                     # V8性能分析

# Java
jstack <pid>                            # 线程dump
jconsole                                # 实时监控
jvisualvm                               # 性能分析
```

---

## 五、无锁编程原则

```go
// 什么时候适合无锁?

// 情况1: 原子操作足够(单变量)
var counter atomic.Int64

func update() {
    counter.Add(1)
}

// 情况2: 只读共享(不需要锁)
var cache atomic.Value

func updateCache(data map[string]string) {
    cache.Store(data)  // 替换整个map,原子操作
}

// 情况3: 需要锁(不要硬用无锁)
// ❌错误:过度使用CAS
type BadStack struct {
    top atomic.Pointer[node]
}
// ✅正确:简单用Mutex
type GoodStack struct {
    mu   sync.Mutex
    data []int
}

func (s *GoodStack) Push(v int) {
    s.mu.Lock()
    defer s.mu.Unlock()
    s.data = append(s.data, v)
}
```

---

## 六、并发测试策略

```go
// Go并发测试
func TestConcurrentWrites(t *testing.T) {
    var mu sync.Mutex
    var results []int
    
    var wg sync.WaitGroup
    for i := 0; i < 100; i++ {
        wg.Add(1)
        go func(v int) {
            defer wg.Done()
            mu.Lock()
            results = append(results, v)
            mu.Unlock()
        }(i)
    }
    wg.Wait()
    
    assert.Equal(t, 100, len(results), "应该写入100个值")
}

// Python并发测试
def test_concurrent_access():
    import threading
    results = []
    lock = threading.Lock()
    
    def worker(v):
        with lock:
            results.append(v)
    
    threads = [threading.Thread(target=worker, args=(i,)) for i in range(100)]
    for t in threads: t.start()
    for t in threads: t.join()
    
    assert len(results) == 100
```

---

## 七、能力评估更新

```
多线程并发开发:
更新前 85/100(知识级)       更新后 90/100 🚀(实战skill封装)

具体覆盖:
├─ Go并发模型:        95 → 95 (已是Expert)
├─ Python async:      90 → 90 (已是Expert)
├─ 线程安全诊断:      80 → 90 (新增诊断工具链)
├─ 并发设计模式:      85 → 90 (新增Fan-Out/Fan-In/超时/限流)
├─ 无锁编程:          70 → 80 (原则清晰,实战尚需补)
└─ 并发测试:          75 → 85 (新增测试策略模板)
```

---

**轩辕在此。** 🔧
*并发编程大师 v1.0 | 14年分布式系统 | 实战skill封装就绪*

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…