标签:代码 核心 sdn 原来 使用 内核管理 pack 实现 目的
如果你的计算机是双核的,那就相当于你叫了一个哥们来帮你一起做这三件事,他切肉,你打扫卫生,事情会更快的结束,计算机也是这样,由原来的多个进程等待一个cpu的时间段,变成多个进程等待两个cpu的时间段,只要进程获取任一个cpu的时间段就可以继续执行
更高效地解决问题
如果多个进程之间在业务逻辑上有联系,那么就会涉及到的关键词就是:关键资源使用权的分配
现在有一个银行,银行里有一个账户,账号对应一张卡,通过这张卡可以对其账户进行存款,取款,查询操作。这个业务模型是单进程的
//账户先抽象出一层接口
type Account interface {
Withdraw(uint)
Deposit(uint)
Balance() int
}
// 银行类和方法
type Bank struct {
account Account //银行里有一个账户
}
func NewBank(account Account) *Bank {
return &Bank{account: account}
}
func (bank *Bank) Withdraw(amount uint, actor_name string) {
fmt.Println("[-]", amount, actor_name)
bank.account.Withdraw(amount)
}
func (bank *Bank) Deposit(amount uint, actor_name string) {
fmt.Println("[+]", amount, actor_name)
bank.account.Deposit(amount)
}
func (bank *Bank) Balance() int {
return bank.account.Balance()
}
//实现Account接口
ype SimpleAccount struct{
balance int
}
func NewSimpleAccount(balance int) *SimpleAccount {
return &SimpleAccount{balance: balance}
}
func (acc *SimpleAccount) Deposit(amount uint) {
acc.setBalance(acc.balance + int(amount))
}
func (acc *SimpleAccount) Withdraw(amount uint) {
if acc.balance >= int(amount) {
acc.setBalance(acc.balance - int(amount))
} else {
panic("杰克穷死")
}
}
func (acc *SimpleAccount) Balance() int {
return acc.balance
}
func (acc *SimpleAccount) setBalance(balance int) {
acc.add_some_latency() //关键:增加一个延时函数,方便演示
acc.balance = balance
}
func (acc *SimpleAccount) add_some_latency() {
<-time.After(time.Duration(rand.Intn(100)) * time.Millisecond)
}
//主函数调用
func main() {
balance := 80
b := NewBank(NewSimpleAccount(balance))
fmt.Println("初始化余额", b.Balance())
b.Withdraw(30, "马伊琍")
fmt.Println("-----------------")
fmt.Println("剩余余额", b.Balance())
}
//结果
初始化余额 80
[-] 30 马伊琍
-----------------
剩余余额 50
func main() {
balance := 80
b := NewBank(NewSimpleAccount(balance))
fmt.Println("初始化余额", b.Balance())
done := make(chan bool)
go func() { b.Withdraw(30, "马伊琍"); done <- true }()
go func() { b.Withdraw(10, "姚笛"); done <- true }()
//等待 goroutine 执行完成
<-done
<-done
fmt.Println("-----------------")
fmt.Println("剩余余额", b.Balance())
}
//结果
初始化余额 80
[-] 30 马伊琍
[-] 10 姚笛
-----------------
剩余余额 70
type LockingAccount struct {
lock sync.Mutex //关于锁的扩展
account *SimpleAccount //继承
}
//封装一下 SimpleAccount
func NewLockingAccount(balance int) *LockingAccount {
return &LockingAccount{account: NewSimpleAccount(balance)}
}
func (acc *LockingAccount) Deposit(amount uint) {
acc.lock.Lock()
defer acc.lock.Unlock()
acc.account.Deposit(amount)
}
func (acc *LockingAccount) Withdraw(amount uint) {
acc.lock.Lock()
defer acc.lock.Unlock()
acc.account.Withdraw(amount)
}
func (acc *LockingAccount) Balance() int {
acc.lock.Lock()
defer acc.lock.Unlock()
return acc.account.Balance()
}
//主函数调用
func main() {
balance := 80
b := NewBank(NewLockingAccount(balance))
fmt.Println("初始化余额", b.Balance())
done := make(chan bool)
go func() { b.Withdraw(30, "马伊琍"); done <- true }()
go func() { b.Withdraw(10, "姚笛"); done <- true }()
//等待 goroutine 执行完成
<-done
<-done
fmt.Println("-----------------")
fmt.Println("剩余余额", b.Balance())
}
//结果
初始化余额 80
[-] 30 马伊琍
[-] 10 姚笛
-----------------
剩余余额 40
//实际流程
________________
_马伊琍_|__姚笛__
加锁 ><
得到余额 80 |
取钱 -30 |
当前余额 50 |
... |
设置余额 50 |
解除锁 <>
|
当前余额 50
|
加锁 ><
得到余额 | 50
取钱 | -10
当前余额 | 40
| ...
设置余额 | 40
解除锁 <>
________________
剩余余额 40
package main
import (
"fmt"
"time"
"math/rand"
)
// 主函数调用
func main() {
balance:=80
b:=NewBank(NewConcurrentAccount(uint8(balance)))
fmt.Println("初始化余额", b.Balance())
donechan:=make(chan bool)
go func() {
b.Withdraw(uint8(30),"daughter")
donechan<-true
}()
go func() {
b.Withdraw(uint8(10),"son")
donechan<-true
}()
<-donechan
<-donechan
fmt.Println("________________")
fmt.Println("剩余钱",b.Balance())
}
type Account interface {
Deposit(uint82 uint8) //存钱
Withdraw(uint82 uint8) //取钱
Balance()uint8 //查看钱
}
type Bank struct {
account Account
}
func NewBank(account Account)*Bank {
return &Bank{account:account}
}
func (bank *Bank)Deposit(amount uint8,name string) {
fmt.Println("[+]",amount,name)
bank.account.Deposit(amount)
}
func (bank *Bank)Withdraw(amount uint8,name string) {
fmt.Println("[-]",amount,name)
bank.account.Withdraw(amount)
}
func (bank *Bank)Balance()uint8 {
return bank.account.Balance()
}
type SimepleAccount struct {
balance uint8
}
func NewSimepleAccount(balance uint8)*SimepleAccount {
return &SimepleAccount{balance:balance}
}
func (account *SimepleAccount)setBalance(balance uint8) {
account.add_some_latency()
account.balance=balance
}
func (account *SimepleAccount) add_some_latency() {
<-time.After(time.Duration(rand.Intn(100)) * time.Millisecond)
}
func (account *SimepleAccount)Deposit(amount uint8) {
account.setBalance(account.balance+amount)
}
func (account *SimepleAccount)Withdraw(amount uint8) {
account.setBalance(account.balance-amount)
}
func (account *SimepleAccount)Balance() uint8 {
return account.balance
}
//扩展SimpleAccount,增加通信功能
type ConcurrentAccount struct {
account *SimepleAccount
deposit chan uint8
withdraw chan uint8
balance chan chan uint8
}
func NewConcurrentAccount(amount uint8)*ConcurrentAccount {
acc:=&ConcurrentAccount{
account:NewSimepleAccount(amount),
deposit:make(chan uint8),
withdraw:make(chan uint8),
balance:make(chan chan uint8),
}
acc.listen()
return acc
}
func (account *ConcurrentAccount)Balance() uint8 {
ch:=make(chan uint8)
account.balance<-ch //第一层阻塞
return <-ch //第二层阻塞
}
func (account *ConcurrentAccount)Withdraw(amount uint8) {
account.withdraw<- amount
}
func (account *ConcurrentAccount)Deposit(amount uint8) {
account.deposit<- amount
}
func (account *ConcurrentAccount)listen() {
go func() {
for {
select { //叫号机,每一个case都是注册的消息
case amt:=<- account.deposit:
account.account.Deposit(amt)
case amt:=<-account.withdraw:
account.account.Withdraw(amt)
case ch:=<-account.balance:
ch<-account.account.Balance()
}
}
}()
}
github源码:git@github.com:Frankltf/concurrency_go.git
参考:http://ju.outofmemory.cn/entry/73895
https://www.cnblogs.com/biyeymyhjob/archive/2012/11/03/2751593.html
https://blog.csdn.net/u010853261/article/details/53464053
标签:代码 核心 sdn 原来 使用 内核管理 pack 实现 目的
原文地址:https://www.cnblogs.com/frankltf/p/9061407.html