• 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

Doubt in the output of the thread program

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


OUTPUT:
5
4
3
2
1
New name is:Thread[Second Thread,5,main]

In the above line, how 5 comes. It is given that ,its priority is 5,which is the default value. I don't understand this statement. Can you please explain how the result came.

[Edit: added code tags - MB]

 
Ranch Hand
Posts: 287
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.toString() method is called and .toString() on a thread object

Returns a string representation of this thread, including the thread's name, priority, and thread group.

instead of Thread.sleep() use java.util.concurrent.TimeUnit. increases the readability of your code greatly

Ex: TimeUnit.SECONDS.Sleep(1);
 
Dhivya rajagopal
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you smith.I asked how 5(priority) comes in the output. That is in New name is:Thread[Second Thread,5,main]. Still i cant understand . Thank you for telling about TimeUnit
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's simply how Thread's toString() method is written - it prints the name, priority and thread group name.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob and Harsha Smith have told you that is what toString on a Thread returns. You can see for yourself if you find the documentation.
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

.toString() method is called and .toString() on a thread object

Returns a string representation of this thread, including the thread's name, priority, and thread group.



Default priority for any thread object is 5; thats how it appeared in the output.

try printing threadMain.getPriority();

you can set priorities to the threads using setPriority(int priority) method. Always use priority constants provided by the class Thread(Ex: Thread.MAX_PRIORITY)

try printing the following lines of code

System.out.println(Thread.NORM_PRIORITY);//default priority 5
System.out.println(Thread.MAX_PRIORITY);//returns 10
System.out.println(Thread.MIN_PRIORITY);//returns 1


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic