标签:fatal error: 两种 import class erro 异常 opera main
golang 中的 sync 包实现了两种锁:
package main
import (
"time"
"fmt"
"sync"
)
func main() {
var mutex sync.Mutex
fmt.Println("Lock the lock")
mutex.Lock()
fmt.Println("The lock is locked")
channels := make([]chan int, 4)
for i := 0; i < 4; i++ {
channels[i] = make(chan int)
go func(i int, c chan int) {
fmt.Println("Not lock: ", i)
mutex.Lock()
fmt.Println("Locked: ", i)
time.Sleep(time.Second)
fmt.Println("Unlock the lock: ", i)
mutex.Unlock()
c <- i
}(i, channels[i])
}
time.Sleep(time.Second)
fmt.Println("Unlock the lock")
mutex.Unlock()
time.Sleep(time.Second)
for _, c := range channels {
<-c
}
}
程序输出:
Lock the lock
The lock is locked
Not lock: 1
Not lock: 2
Not lock: 0
Not lock: 3
Unlock the lock
Locked: 1
Unlock the lock: 1
Locked: 2
Unlock the lock: 2
Locked: 3
Unlock the lock: 3
Locked: 0
Unlock the lock: 0
package main
import (
"fmt"
"sync"
)
func main(){
var mutex sync.Mutex
mutex.Lock()
fmt.Println("Locked")
mutex.Lock()
}
程序输出:
Locked
fatal error: all goroutines are asleep - deadlock!
package main
import (
"sync"
"fmt"
"time"
)
func main() {
var mutex *sync.RWMutex
mutex = new(sync.RWMutex)
fmt.Println("Lock the lock")
mutex.Lock()
fmt.Println("The lock is locked")
channels := make([]chan int, 4)
for i := 0; i < 4; i++ {
channels[i] = make(chan int)
go func(i int, c chan int) {
fmt.Println("Not lock: ", i)
mutex.Lock()
fmt.Println("Locked: ", i)
fmt.Println("Unlock the lock: ", i)
mutex.Unlock()
c <- i
}(i, channels[i])
}
time.Sleep(time.Second)
fmt.Println("Unlock the lock")
mutex.Unlock()
time.Sleep(time.Second)
for _, c := range channels {
<-c
}
}
程序输出:
Lock the lock
The lock is locked
Not lock: 0
Not lock: 1
Not lock: 2
Not lock: 3
Unlock the lock
Locked: 0
Unlock the lock: 0
Locked: 2
Unlock the lock: 2
Locked: 3
Unlock the lock: 3
Locked: 1
Unlock the lock: 1
package main
import (
"sync"
"fmt"
"time"
)
func main() {
var mutex *sync.RWMutex
mutex = new(sync.RWMutex)
fmt.Println("Lock the lock")
mutex.Lock()
fmt.Println("The lock is locked")
channels := make([]chan int, 4)
for i := 0; i < 4; i++ {
channels[i] = make(chan int)
go func(i int, c chan int) {
fmt.Println("Not read lock: ", i)
mutex.RLock()
fmt.Println("Read Locked: ", i)
fmt.Println("Unlock the read lock: ", i)
time.Sleep(time.Second)
mutex.RUnlock()
c <- i
}(i, channels[i])
}
time.Sleep(time.Second)
fmt.Println("Unlock the lock")
mutex.Unlock()
time.Sleep(time.Second)
for _, c := range channels {
<-c
}
}
程序输出:
Lock the lock
The lock is locked
Not read lock: 2
Not read lock: 3
Not read lock: 1
Not read lock: 0
Unlock the lock
Read Locked: 2
Read Locked: 1
Unlock the read lock: 2
Unlock the read lock: 1
Read Locked: 0
Read Locked: 3
Unlock the read lock: 0
Unlock the read lock: 3
package main
import (
"sync"
)
func main(){
var rwmutex *sync.RWMutex
rwmutex = new(sync.RWMutex)
rwmutex.Unlock()
}
程序输出:
panic: sync: Unlock of unlocked RWMutex
示例1:
package main
import (
"sync"
)
func main(){
var rwmutex *sync.RWMutex
rwmutex = new(sync.RWMutex)
rwmutex.Lock()
rwmutex.Lock()
}
程序输出:
fatal error: all goroutines are asleep - deadlock!
示例2:
package main
import (
"sync"
)
func main(){
var rwmutex *sync.RWMutex
rwmutex = new(sync.RWMutex)
rwmutex.Lock()
rwmutex.RLock()
}
程序输出:
fatal error: all goroutines are asleep - deadlock!
package main
import (
"sync"
)
func main(){
var rwmutex *sync.RWMutex
rwmutex = new(sync.RWMutex)
rwmutex.RUnlock()
}
程序输出:
panic: sync: RUnlock of unlocked RWMutex
package main
import (
"sync"
)
func main(){
var rwmutex *sync.RWMutex
rwmutex = new(sync.RWMutex)
rwmutex.RLock()
rwmutex.RLock()
rwmutex.RUnlock()
rwmutex.RUnlock()
rwmutex.RUnlock()
}
程序输出:
panic: sync: RUnlock of unlocked RWMutex
golang 中 sync.Mutex 和 sync.RWMutex
标签:fatal error: 两种 import class erro 异常 opera main
原文地址:https://www.cnblogs.com/ExMan/p/12396766.html