The moose likes Beginning Java and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread" Watch "Thread" New topic


sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
I have a question in this program.
public class TwoThread extends Thread

public void run()
for(int i=0;i<10;i++)
public void printMsg()
Thread t=Thread.currentThread();
String name=t.getName();


public static void main(String args[])
TwoThred tt=new TwoThread();
tt.setName("Child Thread");

for(int i=0;i<10;i++)

when tt.printMsg() from main is executed the main htread is being displayed. Why is this so.... why is not the child thread being displayed since thread ojext tt creates a new thread.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Threads execute code. What thread executes main? The thread named main. That thread executes tt.printMsg. In that call, the current thread is main!

There is no emoticon for what I am feeling!
marc weber

Joined: Aug 31, 2004
Posts: 11343

In the loop in the main thread, the reference to tt is simply being used to call the method printMsg(). Calling this method on a Thread object is no different than calling a method on a non-Thread object -- it's just an instance method.

The separate thread is initiated by calling start(), and the code that executes in that thread is contained within the run() method.

(Note: Please use Code Tags to keep your code formatting intact.)
[ March 28, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Thread
It's not a secret anymore!