Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

threads

 
Greenhorn
Posts: 29
  • 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();
}}


Please explain me why the o/p here is T1T1T3
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Threads are invoked by calling a start() method not by calling run() method.
Means when you call Thread.start() new thread is created.

So You are setting the name of the thread as T1
After exection of
new Thread(new B(),"T1").start();

new thread is generated with name "T1" and B's run metod will be invoked(becz of start())
within this thread u r calling
new A().run(); //output "T1"

this causes A's run() method to call (not a seperate thread) with thread "T1".

new Thread(new A(),"T2").run(); //output "T1"

Tis line again calling A's run() method within same thread "T1".

new Thread(new A(),"T3").start(); //output "T3"

In this line u r creating new thread(becz of start()) with the name "T3".
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Its because you call directly the run()...so It doesn't create another thread and you get T1(from new Thread(new B(),"T1").start(); )&&(from new A().run(); )
T1(new Thread(new A(),"T2").run(); )
T3(from new Thread(new A(),"T3").start(); )
Im also not sure this is guaranted...but im not a thread specialist...
arno
[ May 31, 2006: Message edited by: Arno Reper ]
 
Greenhorn
Posts: 12
  • 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();
}}


Please explain me why the o/p here is T1T1T3



new A().run(); and new Thread(new A(),"T2").run(); did not create new thread, they only print out the current thread name "T1"

after new Thread(new A(),"T3").start(); new thread "T3" is created.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic