Comments (7)
newTask: 当前无任务执行或信道未满时,启动协程并执行,满了则阻塞直至不满
worker: 第一次启动时执行task,然后从work信道之中取task,取到了之后继续执行,没取到就阻塞。
from golangcodingtime.
自己敲了一遍重新理解了一下,这里的worker函数中 defer func() { <-p.sem }() 语句是多余的
1、当有新任务来的时候NewTask,无缓冲work对列没有接受者,所以阻塞;会往有缓冲对列sem里面写一个数据,这时候启动了work创建了work goroutine,且一直for不关闭;这里面启动了一个无缓冲work的消费者;
2、再有新任务来的时候如果work运行,且sem缓冲空间有,会继续往sem中写继续启动work创建一个新的work goroutine
3、再有新任务来work运行中,且sem无缓冲,则阻塞着,这时候是满的work对列在运行
4、有新的任务来,但是sem缓冲满,work运行结束,会发送task到已经启动的work goroutine中去运行
from golangcodingtime.
newTask和woker两个方法没看懂,为什么要这么写呢
from golangcodingtime.
这里的defer能够生效吗
from golangcodingtime.
感觉这个例子我对channel理解多了点
from golangcodingtime.
struct {} :表示struct类型
struct {}是一种普通数据类型,一个无元素的结构体类型,通常在没有信息存储时使用。
优点是大小为0,不需要内存来存储struct {}类型的值。
struct {} {}:表示struct类型的值,该值也是空。
struct {} {}是一个复合字面量,它构造了一个struct {}类型的值,该值也是空。
from golangcodingtime.
我感觉 defer func() { <-p.sem }() 语句不是多余的。
当goroutine crash的时候用于释放sem信号量,可以创建新的goroutine。
因为task是有可能产生异常的。
from golangcodingtime.
Related Issues (20)
- aboutme HOT 3
- c07/c07_03 HOT 1
- c01/c01_06 HOT 3
- c01/c01_10 HOT 1
- c01/c01_07 HOT 1
- c01/c01_09 HOT 1
- c01/c01_13 HOT 1
- c01/c01_15 HOT 1
- c02/c02_01 HOT 2
- c04/c04_06 HOT 3
- c05/c05_02 HOT 2
- chapters/p02 HOT 2
- index HOT 1
- c02/c02_03 HOT 1
- c02/c02_04 HOT 1
- c02/c02_05 HOT 1
- c04/c04_01 HOT 1
- c04/c04_03 HOT 1
- c07/c07_04 HOT 1
- c01/c01_03 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from golangcodingtime.