multithreading - Java make sleeping threads do work in a thread pool -
i have thread pool fixed number of working threads (say 4). continiously fetch new runnables executor. of these runnables have long period sleep call, waiting thread interrupted it:
runnable runnable = new runnable() { @override public void run() { //do preparation doprework(); //wait other runnable interrupt me try { thread.sleep(40000); } catch(interruptedexception e) { } //finish work doafterwork(); } }
so question is: when fetch first 4 runnables executor, working threads sleeping , other incoming runnables(a lot of them, because continiously incoming) queued , have wait available threads. there way, can use sleeping threads execute new incoming runnables, maintaining others sleeping?
no, sleeping thread sleeping. can't make else.
what should add scheduled task delayed amount of time want. free current thread , allow else.
scheduledexecutorservice ses = ... ses.submit(new runnable() { @override public void run() { //do preparation doprework(); ses.schedule(new runnable() { @override public void run() { //finish work doafterwork(); } }, 40000); } }
imho, sending signals via interrupts unreliable. don't write code depends on it.
Comments
Post a Comment