Code Monkey home page Code Monkey logo

Comments (5)

jdantonio avatar jdantonio commented on May 22, 2024

You're right, changing the API isn't an easy choice, but that's better than having a serious bug. I never gave any thought to subclassing Future because, in my mind, I view it like a concurrency primitive. But it isn't and you are correct that subclassing is a problem. What do you think about supporting execute as both an instance and a class method?

f = Future.new{ sleep(10); print "foo\n" }
f.state #=> :unscheduled
f.execute
f.state #=> :pending

f = Future.new{ sleep(10); print "foo\n" }.execute

f = Future.execute{ sleep(10); print "foo\n" }

In the both cases initialize raises an ArgumentError if no block is given. The instance method execute returns self to support the single-line chained call, but does nothing once the Future is in any state other than :unscheduled. The class method execute, as you suggested, creates a new Future, calls execute on the object, then returns it.

Thoughts?

from concurrent-ruby.

mighe avatar mighe commented on May 22, 2024

I like this solution, because new keeps the current signature and the whole class interface is nicer.
execute could take an optional thread_pool parameter, to make it more flexible: at the moment is not easy to change it, so in some situations we have a little messy code (like in tests)

from concurrent-ruby.

jdantonio avatar jdantonio commented on May 22, 2024

I've updated Future with the execute methods and updated the specs. I didn't implement a thread_pool parameter yet. I've struggled some with how best to handle internal thread pools. I'd like to handle that consistently for all abstractions that should reasonably support them. An optional parameter makes sense for Future but I'd like to look at the rest of the library and see how many of the abstractions that approach makes sense with.

from concurrent-ruby.

jdantonio avatar jdantonio commented on May 22, 2024

I noticed that ScheduledTask has the same issue as Future so I implemented the same execute methods on that class, too.

from concurrent-ruby.

jdantonio avatar jdantonio commented on May 22, 2024

I've also updated Future and ScheduledTask to have identical construction behavior. Both support Dereferenceable and take an options hash as a parameter to new and class execute.

from concurrent-ruby.

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.