Java 中的锁例子
![在这里插入图片描述](https://img.php1.cn/3cd4a/1eebe/cd5/443b30bb45e66690.webp?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dhbmdtaW5nNTIwbGl3ZWk=,size_16,color_FFFFFF,t_70)
Go 中的锁概念
package mainimport ("sync""testing""time"
)func TestCounter(t *testing.T) {counter := 0for i := 0; i <5000; i++ {go func() {counter++}()}time.Sleep(1*time.Second)t.Logf("counter = %d", counter)
}func TestCounterThreadSafe(t *testing.T) {counter := 0var mut sync.Mutexfor i := 0; i <5000; i++ {go func() {defer func() {mut.Unlock()}()mut.Lock()counter++}()}time.Sleep(1 *time.Second)t.Logf("counter = %d", counter)
}func TestCounterWaitGroup(t *testing.T) {var wg sync.WaitGroupvar mut