File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Doubts

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
ahmed yehia
Ranch Hand
Posts: 424
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response Ahmed. Actually my 2nd question was different from your answer.
 
ahmed yehia
Ranch Hand
Posts: 424
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic