jQuery in Action, 3rd edition
The moose likes Threads and Synchronization and the fly likes What are those two threads??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "What are those two threads???" Watch "What are those two threads???" New topic

What are those two threads???

Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi friends i am new to Threads.When i run this program i can see one thread created by me and another one is the "main" thread.When i call t.start()m my thread starts.In the docs for start they gave as

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

In the above my new thread is current thread.Please say me the other thread which executes the run method

System.out.println("Main Thread: " + i);
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
System.out.println("Main thread exiting.");

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm not sure what you're asking ... maybe why your program displays something like "Child thread: Thread[Demo Thread,5,main]"? Thread's toString() method "Returns a string representation of this thread, including the thread's name, priority, and thread group."

I haven't seen Runnable() classes that start themselves the way you did very often. I'm not sure if that's kind of slick or if it is risky to start the thread before the constructor is well and truly done. I'm pretty sure it's risky if your class is not final. This is more common, I think:

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
dave zebatinsky

Joined: Jul 07, 2007
Posts: 6
Hi Ganesh,

The current thread would be the "main" thread used to start your program, i.e. public static void main() and anything else it calls.

The second thread is then created from your line "t = new Thread(this, "Demo Thread");"
In this line you have created a new thread t that has a name of "Demo Thread" and has the run() method of your "NewThread" object which implements the Runnable interface registered as the run method to be invoked when the thread t is started.

When you implement the Runnable interface you also have to override the public void run() method.

Hope this makes things a little clearer.


I agree. Here's the link: http://aspose.com/file-tools
subject: What are those two threads???
It's not a secret anymore!