wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread creation using Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread creation using Runnable" Watch "Thread creation using Runnable" New topic
Author

Thread creation using Runnable

rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
I have two simple programs to create a thread using runnable interface. What I observed is that in case of program -1 the main thread exits only after the execution of the child thread, whereas in case of Program-2 the main exits soon after the creation of the child thread and the child thread continues to execute the process is complete.

Program -1
---------------


// Implementing one thread.

class RunnableThread implements Runnable
{
private static int iThreadCount;

private Thread threadObj;

RunnableThread()
{
iThreadCount++;
threadObj = new Thread(this, ("Thread" + iThreadCount));
System.out.println("threadObj : " + threadObj);
threadObj.run();

}

public void run()
{
System.out.println("In the run method for " + threadObj.getName());
for(int i = 0; i<5; i++)
{
System.out.println(i + " : " + threadObj.getName());

}
System.out.println("***Exiting from " + threadObj.getName());
}
}

public class TestRunnableThread
{
public static void main(String args[])
{

RunnableThread simpleThread1 = new RunnableThread();
System.out.println("***Exiting main method***");
}

}


Program -2
------------------
class Counter implements Runnable
{
private int currentValue;
private Thread worker;

public Counter(String threadName)
{
System.out.println("***In the constructor Counter*** - 2");
currentValue = 0;
worker = new Thread(this, threadName);//1
System.out.println(worker);
worker.start();//2
}
public void run()//3
{
System.out.println("***In the method run()***");
while(currentValue < 5)
{
System.out.println(worker.getName() + ": " + (currentValue++));

}
System.out.println("Exit from thread: " + worker.getName());
}
}

public class Client
{
public static void main(String args[])
{
System.out.println("***In the main Method*** - 1");
Counter counterA = new Counter("Counter A");//5
System.out.println("Exit from main() method.");
}
}

Can you please explain why this difference in behaviour?

I expected the main Thread to exit soon after the creation of the child thread and child continues till its process is complete.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Program 1 never starts a new thread. It merely calls the run() method on the Thread object (which does not create a new thread of execution).

Hope this helps.
rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
I tried calling the start method too.

I guess the behaviour is unpredictable. Sometimes main thread exits before the child thread and sometimes after the child thread.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

P.S. Never rely on the thread scheduler for behavior...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread creation using Runnable