This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Thread Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread Doubts" Watch "Thread Doubts" New topic
Author

Thread Doubts

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
1. Whats the priority of main thread (main method)?

The time specified in sleep method is the minimum duration in which the thread won't run, but its not the exact duration in which the thread won't run.

2. Does the above sentence mean the thread can wake up even before the duration mentioned in the sleep method?


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Thread 'main' has priority of Thread.NORM_PRIORITY which is 5, you can check by inserting this line in the main().
System.out.println(Thread.currentThread().getPriority());

A sleeping thread could be interrupted while sleeping if other thread called interrupt() on the sleeping thread, from which it would become runnable again.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Thanks for your response Ahmed. Actually my 2nd question was different from your answer.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

The time specified in sleep method is the minimum duration in which the thread won't run, but its not the exact duration in which the thread won't run.

When the thread executes its sleep method, immediately it goes from the running state to waiting/blocked state, and waits there until the time duration expires or if other thread invoked interrupt() on it.

However after that it enters the runnable state in which it has to stay in the runnable pool until it is selected to run again. So the exact time in which the thread won't run can't be determined.
 
GeeCON Prague 2014
 
subject: Thread Doubts