Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

thread

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


when this code is run it gives answer as
thread main name :main
thread name :Thread-0
thread name :Thread-0


When i put 2 i place of 1 then the answer becomes

thread main name :main
thread name :Thread-1
thread name :Thread-1

So my question is why it is not showing both thread name i.e thread name :Thread-0 & thread name :Thread-1
i have also made run method synchronized.

thanks in advance
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you run the code as it is you get this :

thread main name :main
thread name :Thread-1
thread name :Thread-1

The first line is the System.out.println statement you have at line 24 in your source code, it shows the current Thread which is main. Note that to be correct and avoid warnings must use Thread.currentThread() , because this method is static for class Thread.

The other two lines are generated by the System.out.println statement inside the for loop in the run method.

In this code you are launching 2 threads:
- 1 thread instance of Thread class, which doesn't print anything to the screen.
- 1 thread instance of ThreadA class

If you comment out the line 19 and 20 you get this:
thread main name :main
thread name :Thread-0
thread name :Thread-0

Because you are launching just one thread, a instance of ThreadA class.

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Check below :-


There at line //1 you are just creating a thread that has no code in its run() method. As you have started that one first, it is given number Thread-0. Next you made a thread object of a class that extends Thread with code to be executed inside run() method. If you change the line as I mentioned above then you'll see what you have asked for.
Main point is the Thread class implemets Runnable interface but provides no code inside run() method.

Regards,
Samrat.
 
sweety sinha
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks both of you for the answer.
now i got it
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic