Giter Club home page Giter Club logo

Comments (7)

2429160671 avatar 2429160671 commented on July 27, 2024 1

newTask: 当前无任务执行或信道未满时,启动协程并执行,满了则阻塞直至不满
worker: 第一次启动时执行task,然后从work信道之中取task,取到了之后继续执行,没取到就阻塞。

from golangcodingtime.

tswcbyy1107 avatar tswcbyy1107 commented on July 27, 2024 1

自己敲了一遍重新理解了一下,这里的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.

AllYeah avatar AllYeah commented on July 27, 2024

newTask和woker两个方法没看懂,为什么要这么写呢

from golangcodingtime.

tswcbyy1107 avatar tswcbyy1107 commented on July 27, 2024

这里的defer能够生效吗

from golangcodingtime.

tswcbyy1107 avatar tswcbyy1107 commented on July 27, 2024

感觉这个例子我对channel理解多了点

from golangcodingtime.

ShawnQiang1 avatar ShawnQiang1 commented on July 27, 2024

struct {} :表示struct类型
struct {}是一种普通数据类型,一个无元素的结构体类型,通常在没有信息存储时使用。
优点是大小为0,不需要内存来存储struct {}类型的值。

struct {} {}:表示struct类型的值,该值也是空。
struct {} {}是一个复合字面量,它构造了一个struct {}类型的值,该值也是空。

from golangcodingtime.

Delong-Yang avatar Delong-Yang commented on July 27, 2024

我感觉 defer func() { <-p.sem }() 语句不是多余的。
当goroutine crash的时候用于释放sem信号量,可以创建新的goroutine。
因为task是有可能产生异常的。

from golangcodingtime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.