Hi Yes, if I call logger_thr.shutDown() after shutting down the msg_thr and using the break instead of System.exit() , produces the expected results as you told. Still ,all this happens only if there is a println statement within the while() in the main method.Only then does the "if condition get statisfied and the msg_thr ends and then the logger_thr ends. If there is no , println statement then , the logger thread keeps waiting for the msg_thr to end and the msg_thr never shuts downs,coz the if condition is never satisfied. I think , the main thread becomes idle and does not get the updated value of numThread from the other threads when there is no println statement, so the if condition never gets satisfied and hence the infinite loop behaviour. I tried printing something like"inside while loop "instead of the numThreads value in the while loop ,and the output doesnt change ,it works fine. But without the print statement its an infinite loop.
I have pasted the o/p below for both the cases. 1) case with println statement in the while loop (I have just pasted the relevant o/p , coz the actual o/p runs to 30 pages coz of the while loop) 0 Message in queue is MESSAGE ID: 1 MESSAGE NO: 0 0 1 1 Message in queue is MESSAGE ID: 2 MESSAGE NO: 0 Completed Message ID:2 number of msg threads: 2 Message in queue is MESSAGE ID: 3 MESSAGE NO: 0 Message in queue is MESSAGE ID: 4 MESSAGE NO: 0 Message in queue is MESSAGE ID: 2 MESSAGE NO: 1 Message in queue is MESSAGE ID: 1 MESSAGE NO: 1 Message in queue is MESSAGE ID: 4 MESSAGE NO: 1 Message in queue is MESSAGE ID: 3 MESSAGE NO: 1 Message in queue is MESSAGE ID: 2 MESSAGE NO: 2 Message in queue is MESSAGE ID: 1 MESSAGE NO: 2 Message in queue is MESSAGE ID: 4 MESSAGE NO: 2 Message in queue is MESSAGE ID: 3 MESSAGE NO: 2 Message in queue is MESSAGE ID: 3 MESSAGE NO: 3 Message in queue is MESSAGE ID: 2 MESSAGE NO: 3 Message in queue is MESSAGE ID: 4 MESSAGE NO: 3 Message in queue is MESSAGE ID: 4 MESSAGE NO: 4 Message in queue is MESSAGE ID: 3 MESSAGE NO: 4 1 2 2 3 3 Completed Message ID:1 number of msg threads: 1 3 3 Message in queue is MESSAGE ID: 4 MESSAGE NO: 5 Completed Message ID:3 number of msg threads: 3 3 3 Completed Message ID:4 number of msg threads: 4 Completed all msg sending threads Message in queue is null Msg threads are shut down , so logger thread is also shutting down BUILD SUCCESSFUL (total time: 0 seconds) 2)In case of no println statement , the program never ends run: Message in queue is MESSAGE ID: 1 MESSAGE NO: 0 Message in queue is MESSAGE ID: 2 MESSAGE NO: 0 Message in queue is MESSAGE ID: 3 MESSAGE NO: 0 Message in queue is MESSAGE ID: 4 MESSAGE NO: 0 Message in queue is MESSAGE ID: 2 MESSAGE NO: 1 Message in queue is MESSAGE ID: 4 MESSAGE NO: 1 Message in queue is MESSAGE ID: 3 MESSAGE NO: 1 Message in queue is MESSAGE ID: 1 MESSAGE NO: 1 Message in queue is MESSAGE ID: 4 MESSAGE NO: 2 Message in queue is MESSAGE ID: 1 MESSAGE NO: 2 Message in queue is MESSAGE ID: 2 MESSAGE NO: 2 Message in queue is MESSAGE ID: 3 MESSAGE NO: 2 Completed Message ID:1 number of msg threads: 1 Message in queue is MESSAGE ID: 3 MESSAGE NO: 3 Message in queue is MESSAGE ID: 2 MESSAGE NO: 3 Message in queue is MESSAGE ID: 4 MESSAGE NO: 3 Completed Message ID:2 number of msg threads: 2 Message in queue is MESSAGE ID: 4 MESSAGE NO: 4 Message in queue is MESSAGE ID: 3 MESSAGE NO: 4 Completed Message ID:3 number of msg threads: 3 Message in queue is MESSAGE ID: 4 MESSAGE NO: 5 Completed Message ID:4 number of msg threads: 4 Message in queue is null Message in queue is null Message in queue is null Message in queue is null On Wed, Aug 12, 2009 at 3:42 PM, user001 <[email protected]> wrote: > > Post the outputs you are getting on both cases and point what makes > you think that numThreads is giving troubles. > I haven't been able to recreate the lock unless I change: > > System.exit(0); // itsn't meant to end threads but the whole program > so check if you really need this here > > at logger.java with: > > break; // now the program isn't terminated here and the lock can > happen > > I get such behaviour without the trouble that you pointed about > numThreads variable. > > I haven't read the topic yet so I'm not that good with ExecutorService > class (I'm used to work with Thread and SwingWorker ones for > concurrency) but as for me it seems that the trouble is that it's > instances must be "closed" for the main thread to end. In other words > I called logger_thr.shutdown() after msg_trd.shutdown() to fix it. > > I encourage you to explain what makes you think that numThreads is > giving troubles (with the outputs or whatever you used) since it could > just be some misleading (once you do and if no-one answer send me an e- > mail and I'll give you a hand - since I won't be checking the forum > anytime soon). > > > Diego > > On Aug 11, 8:11 pm, sharmi n <[email protected]> wrote: > > Hi > > I have a doubt and would appreciate if anyone of you could explain the > > scenario to me. > > Here is what my sample application will do: > > 1) There are 4 threads that will continuously send messages > > 2)There is a logger class that will take these messages > posted > > by the threads and print them to the std out > > 3) Iam using a Executors factory method to create and > manage > > the thread pool (consisting of 4 threads) :let me call this "msg_thr" in > my > > program > > 4) Similarly for the logging action also , I have a > > singlethreadpool called : "logger_thr" > > 5)Then I have the main thread , where i create the above > > mentioned thread pools > > 6)Now I want to clean up the thread pools once all actions > are > > done , so I call shutdown() of the ExecutorService for the msg_thr > > > > This program executes as expected when there is a println statement in > the > > while loop of the main thread and otherwise , it never completes and > keeps > > running . > > I think that happens because "if(BlockingQueueSample.numThreads == 4) " > > statement inside the while loop never becomes true , even though the > > numThreads value is 4. > > On the other hand , if there is a println statement , then the condition > is > > true and it exits gracefully. > > > > I want to know why the numThreads variable , though equal to 4 ,as seen > from > > the run() functions println statement , is not reflected in the main > thread > > . > > > > I have pasted the code for both the classes below: > > > > *BlockingQueueSample.java* > > > > package blockingqueuesample; > > import java.util.concurrent.*; > > public class BlockingQueueSample implements Runnable { > > int count;// number of times to send msges > > int pause;// time to sleep before sending next msg > > int id;//msg id > > BlockingQueue<String> msgQueue; > > public static int numThreads = 0; > > public BlockingQueueSample(BlockingQueue<String> queue,int cnt,int > > ps,int id) > > { > > this.msgQueue = queue; > > this.count = cnt; > > this.pause = ps; > > this.id = id; > > } > > public void run() > > { > > > > for(int i=0;i<count;i++) > > { > > try > > { > > msgQueue.put("MESSAGE ID: "+id+" MESSAGE NO: "+i); > > Thread.sleep(pause); > > } > > catch(InterruptedException ie) > > { > > System.out.println("Unable to put msg in queue /or thread > > interrupted"); > > } > > } > > System.out.println("Completed Message ID:"+id+" number of msg > > threads: "+(++numThreads)); // it prints "4" after completing 4 threads > > } > > /** > > * @param args the command line arguments > > */ > > public static void main(String[] args) { > > // TODO code application logic here > > BlockingQueue<String> qu = new ArrayBlockingQueue<String>(20); > > > > //multiple msg threads > > ExecutorService msg_thr = Executors.newFixedThreadPool(4); > > msg_thr.execute(new BlockingQueueSample(qu,3,200,1)); > > msg_thr.execute(new BlockingQueueSample(qu,4,200,2)); > > msg_thr.execute(new BlockingQueueSample(qu,5,200,3)); > > msg_thr.execute(new BlockingQueueSample(qu,6,200,4)); > > > > ExecutorService logger_thr = Executors.newSingleThreadExecutor(); > > Logger my = new Logger(qu,msg_thr); > > logger_thr.execute(my); > > //clean up > > while(true) > > { > > //System.out.println("Inside while loop of main thread"); > > System.out.println(numThreads); // this statement shd be > > present for the if loop to go through , although numThreads is 4 (as seen > > from run() > > if(BlockingQueueSample.numThreads == 4) > > { > > System.out.println("Completed all msg sending threads"); > > msg_thr.shutdown(); > > break; > > } > > } > > } > > > > } > > > > *Logger.java* > > package blockingqueuesample; > > import java.util.concurrent.BlockingQueue; > > import java.util.concurrent.ExecutorService; > > import java.util.concurrent.TimeUnit; > > public class Logger implements Runnable{ > > BlockingQueue<String> logQueue; > > ExecutorService exec; > > public Logger(BlockingQueue<String> qu,ExecutorService es) > > { > > this.logQueue = qu; > > this.exec = es; > > } > > public void run() > > { > > > > while(true) > > { > > //System.out.println("In logger thread"); > > try > > { > > if(exec.isShutdown()) > > { > > System.out.println("Msg threads are shut down , so > > logger thread is also shutting down"); > > System.exit(0); > > } > > > > System.out.println("Message in queue is > > "+logQueue.poll(500,TimeUnit.MILLISECONDS)); > > } > > catch(InterruptedException ie) > > { > > System.out.println(ie.getMessage()); > > } > > > > } > > } > > > > > > > > }- Hide quoted text - > > > > - Show quoted text - > > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
