This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
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 Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "simple Thread creation" Watch "simple Thread creation" New topic

simple Thread creation

rekha devan

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

class RunnableThread implements Runnable
private int iThreadCount;

private Thread threadObj;

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


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?

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);


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

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
I agree. Here's the link:
subject: simple Thread creation
It's not a secret anymore!