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());
}
}
}
}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---