• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads...

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone explain me the working of the following code



Which method shall these two lines override

new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();

Regards
Arun
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A implements Runnable
{
public void run()
{
System.out.print(Thread.currentThread().getName());
}
}


class B implements Runnable
{
public void run()
{
new A().run();
new Thread(new A(),"T2").run();
new Thread(new A(),"T3").start();
}
}


class C
{
public static void main (String[] args)
{
new Thread(new B(),"T1").start();
}
}


/*

1. A new thread object is created, named "T1".
This thread run() method is B.run()
This thread object "T1" is started, AKA : a new thread of execution is created by the call to start() : we have now 2 threads running : the main thread and a new thread "T1".

2. Concurently, 2 things happen :
- The main thread has nothing more to do. It finish by leaving the main method.
- The new thread "T1" launch the run() method of class B.
In this run() method :
a) A new A object, subclass of Runnable, is created.
This A object launch its run() method. This will not create a new thread, since A.start() has not be called.
This will print the name of the current thread : "T1".
When A.run() is finished, the next instruction of B.run() is executed. We still are in the "T1" thread.
b) This next instruction create a new Thread object, based on Runnalbe A, named "T2". The run() method of this thread will then be A.run().
The A.run() method is launched. This will not create a new thread, since start() method of this new thread object is not called.
A.run() will print the name of the current thread, wich is still "T1".
As above, When A.run() is finished, the next instruction of B.run() is executed. We still be in the "T1" thread.
c) The next instruction create also a new thread object, based on the Runnable class A, named "T3".
The start() method of this thread object is launched, creating a new thread.
At this point, we have 2 threads running, "T1" and "T3" :
Concurently :
- "T1" will finish executing B.run() ;
- "T3" will begin executing A.run(), since "T3" is created from Runnable A.
"T1" will end and leave B.run(), then end and leave C.main(). And then "T1" thread is finished.
"T3" will execute A.run(), print the name of the current thread : "T3", and then end.
*/
 
reply
    Bookmark Topic Watch Topic
  • New Topic