4

I am looking for something like the JavaScript setTimeout, but with a Runnable, with the following restrictions:

  • Does not require individual dedicated threads per timeout.
  • Already developed and thought through.
  • Maybe even including additional features. (cancel timeout?, wait for something?, async I/O?)
  • Does not require any GUI libraries. (Java FX/Swing/AWT all have event loops built in)

Do you have any suggestions?

Edit: I have found what I am looking for. A plus would be if there was a library that also included something related to either non-blocking or asynchronous I/O.

700 Software
  • 85,281
  • 83
  • 234
  • 341
  • I think it's called "XNIO" or "Mina". More async-IO based, and not as general as Twisted/POE (?), but otherwise what is asked for. (I wonder if there *is* a Java replacement for Twisted/POE?) –  Dec 30 '11 at 19:42
  • Oh, with the right terms: see http://stackoverflow.com/questions/675093/twisted-in-java a response recommends Mina. Also http://stackoverflow.com/questions/4033271/can-twisted-be-implemented-in-java –  Dec 30 '11 at 19:45
  • 1
    Interesting, not directly related: http://stackoverflow.com/questions/743008/should-all-event-driven-frameworks-be-single-threaded –  Dec 30 '11 at 19:48

5 Answers5

3

You're probably looking for ScheduledThreadPoolExecutor :

  • you decide how many threads are used
  • standard JDK class
  • cancellable tasks
  • not related to any GUI

I don't really understand the link between scheduled runnables and an event loop, but maybe you'll find what you're looking with this class.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • There would probably be a thread that waits until the scheduled time arrives, an event loop would do that, and more. Does that clarify? – 700 Software Dec 30 '11 at 20:14
  • It is too old to answer, [ReactiveX](https://github.com/ReactiveX/RxJava) project built under the hood of Netflix. A library for composing asynchronous and event-based programs by using observable sequences. – Subhrajyoti Majumder Dec 25 '15 at 12:18
2

You can use java.util.Timer

http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html http://docs.oracle.com/javase/6/docs/api/java/util/TimerTask.html

You can set the task run only once or periodically. You can also stop/cancel individual TimerTask or all tasks.

evanwong
  • 5,054
  • 3
  • 31
  • 44
1

If you're looking for a simple node.js style event loop in Java, ThreadPoolExecutor is a good start.

Take a look at the Executors factory mentioned in the javadoc for ThreadPoolExecutor, particularly Executors.newSingleThreadExecutor(). This gives you a single background thread (like the Node event loop: see the answer to this question) to which you can submit tasks.

For async IO, tasks handling blocking activity need to split of a Thread (or Executor) and use a Future to submit the result back to the event loop.

Community
  • 1
  • 1
David Carboni
  • 1,556
  • 23
  • 24
1

I think Vert.x is what you need, it is event driven and non blocking.

Warren Zhou
  • 284
  • 3
  • 5
0

Have you thought of using awaitTermination API of ThreadPoolExecutor

tpe.awaitTermination(50, TimeUnit.SECONDS);

might be useful to you.

kunal
  • 779
  • 6
  • 25