File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubt in the output of the thread program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in the output of the thread program" Watch "Doubt in the output of the thread program" New topic
Author

Doubt in the output of the thread program

Dhivya rajagopal
Ranch Hand

Joined: Dec 15, 2010
Posts: 42


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]

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
.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

Joined: Dec 15, 2010
Posts: 42

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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

That's simply how Thread's toString() method is written - it prints the name, priority and thread group name.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: Jul 18, 2011
Posts: 287
.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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in the output of the thread program