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
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
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.
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad: