multithreading - How Executorservice works in java? -
i familiar traditional thread implementation not sure executorservice. know it's handy when dealing threads using executor service. little confuse it's implementation.
let's assume have parent thread/main thread , executor service has max thread pool size 10. want perform task on jms queue if it's size less 1000. right can think implement in 2 ways.
case 1:
class mythreadclass inmplements runnable { public static void main(string args[]){ while(true){ int checkqueuesize = jmsqueue.getsize(); while(checkqueuesize<1000){ threadpooltaskexecutor.execute(this); ++checkqueuesize; } } } public void run(){ jmsqueue.pushmessage("hello thread"); } }
case 2:
class mythreadclass inmplements runnable { public static void main(string args[]){ threadpooltaskexecutor.setwaitfortaskstocompleteonshutdown(true); threadpooltaskexecutor.execute(this); } public void run(){ while(true){ int checkqueuesize = jmsqueue.getsize(); while(checkqueuesize<1000){ jmsqueue.pushmessage("hello thread"); ++checkqueuesize; } } } }
my understanding case 2 not span more 1 thread. correct or not? or there other way perform task?
"case 2 not span more 1 thread" yes. correct. because call execute
once.
in first scenario may call execute
many times. , in case of previous task isn't finished additional thread runed.
Comments
Post a Comment