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 -~----------~----~----~----~------~----~------~--~---
