Code Monkey home page Code Monkey logo

Comments (5)

andradei avatar andradei commented on June 12, 2024

It would always be the same. Even though there is a part of the code that is run on a separate thread, it runs in sync with the main thread.

from gobyexample.

0x457 avatar 0x457 commented on June 12, 2024

I carefully check the code, and the "GitHub" code is the same, but why I always get the following results:
[ go run closechan.go | done: 307.238771ms ]
sent job 1
sent job 2
sent job 3
sent all jobs
received job 1
received job 2
received job 3
received all jobs

from gobyexample.

pawanrawal avatar pawanrawal commented on June 12, 2024

I think this is because the channel is buffered, hence all jobs are sent first and then received. IMHO , its a bug @mmcgrana

from gobyexample.

andradei avatar andradei commented on June 12, 2024

Also, sometimes the execution of so little code is "too fast" and the result always comes out in order as though it didn't run on separate threads. But yeah, it could also be a bug.

from gobyexample.

mmcgrana avatar mmcgrana commented on June 12, 2024

The exact order observed when you run the code is the result of preemption behavior that may vary by environment / version / platform etc. The code as written certainly could produce the interleaving documented on the site. If you want to see specific interleavings in action, you can try adding sleeps. For example: https://play.golang.org/p/JJaNaB5-h8.

I'm not going to try to tweak the code on the site further to produce specific interleavings though, since as mentioned above it depends on things that may vary over time like how the Go scheduler works.

from gobyexample.

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.