aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes simple Thread creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple Thread creation" Watch "simple Thread creation" New topic
Author

simple Thread creation

rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
// Implementing one thread.

class RunnableThread implements Runnable
{
private int iThreadCount;

private Thread threadObj;

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

}

public void run()
{

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

}


}

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

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

}

}

In the above program the run method is never executed can anyone explain why?

What I understand is the main Thread exits after creation of the child thread. But the child thread would continue to execute till its process is complete. Then why is the control not going to the run method?


Thanks,
Rekha
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
i feel that threadObj = new Thread("Thread" + iThreadCount);
has problem as we need to pass the runnablethread instance inorder to execute the run method..instead ur passing a class variable...

/****
Runnablethread rt= new Runnablethread();
Thread t=new Thread(rt);
t.start();
/***


thanks
sri


Thanks & Regards<br /> <br />-Srikanth
rekha devan
Greenhorn

Joined: Oct 10, 2005
Posts: 26
Thanks, it is working!!

Yes i forgot to mention the runnable object in the thread constructor.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
threadObj = new Thread("Thread" + iThreadCount);

This line creates a new instance of a threadclass & has it's own run method ( default method ).
But if you pass a runnable object to the thread constructor then the Runnable becomes the target of this thread.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
 
Consider Paul's rocket mass heater.
 
subject: simple Thread creation
 
Similar Threads
clarification on synchronization
Daemon Thread
Thread synchronization problem
Thread creation using Runnable
Thread synchronisation - Using a string literal for locking