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

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -