• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interrupt and wait

 
Michael Lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
42. There are 20 threads are waiting in the waiting pool with same priority, how can you invoke 15th thread from the waiting pool?.
A) By calling resume() method
B) By calling interrupt() method
C) Calling call() method
D) By calling notify(15) method on the thread instance
E) None of the above
//I think the answer can be B.
Can someone make it sure for me??
thanks!
as I provide a code as following:
class Th extends Thread{
String na="";
Th(String name) { super(name); }

public void run() {
countFrom1To100();
} // method run

public void countFrom1To100(){

System.out.println("Th start waiting: ");
synchronized(na){
try { na.wait();
}catch (InterruptedException e){
System.out.println("interrupted... ");
}
}///


System.out.println("th finish. ");

} // method called by run

public static void main(final String args[]) {

Th th= new Th("worker");
th.start();
try { sleep(2000);
}catch (InterruptedException e){}
th.interrupt(); //wake up the waiting thread!!!

System.out.println("main done: " );
}//
} // class CounterHandler
 
Zheng Huang
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer should be E. Correct me if I am wrong.
 
atin sehgal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too think answer shd be E-None of the above.
Bcoz we can never be sure if we notify(), which of the thread may be notified..
Thats why its is preffred to use NotifyAll()

Correct me if I am wrong...
Regards..
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
The answer is E ... there is NO WAY to pass control to a specific thread; <code>notifyAll()</code> alerts all the related waiting threads and then they compete for the lock.
Hope that helps.
Jane
[This message has been edited by Jane Griscti (edited January 14, 2001).]
 
Michael Lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Jane!
but my question is: why can't I use the interrupt() as in my example code???
Originally posted by Jane Griscti:
Hi Michael,
The answer is E ... there is NO WAY to pass control to a specific thread; <code>notifyAll()</code> alerts all the related waiting threads and then they compete for the lock.
Hope that helps.
Jane
[This message has been edited by Jane Griscti (edited January 14, 2001).]

 
Son Le
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
I think the answer is E. There is no way you can single out the 15th thread. The order of the threads are totally up to the Thread schuduler.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Micahel,
You can call <code>interrupt()</code> on a thread; but there's no way to know if it's the 15th or any other number thread. They are all handled by the scheduler.
Jane
 
Raj J
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ans. E is correct..
bcoz the developer or user has no explicit control to start the thread which is in runnable state..
It is decided by the processor which thread is to put to run from runnable state , and and even one thread at a time..
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Michael Lin,
You want to know that why you can nto use interrupt() in your code to call a particular waiting thread. I will try to explain you some points and hope it will solve your query.
First, different monitor states are: READY , RUNNING, SEEKING LOCK, A running thread can go to WAITING.
A thread that calls wait() releases the virtual CPU; at the same time, it releases the lock. It enters a pool of waiting threads, which is managed by the object whose wait() method got called. Every object has such pool.
It remains in WAITING until it has been notify() or notifyAll() or timeout or interrupt. When any one of these thing happen thread goes into SEEKING LOCK stage. Waiting their to obtain a lock. Once it obtain a lock it goes into READY state and when CPU gives time it start RUNNING.
Question is What happens when there is more than one thread waiting for notification/lock? Which thread actually gets the notification when notify(), notifyAll()is called?
The answer is that it depends: the java specification dosen't define which thread gets notified. Which thread actually receives the notification varies based on several factors, including the implementation of the Java virtual machine and scheduling and timing issues during the execution of the program. There is no way to determine, even on a single platform, which of multiple threads receives the notification.

The interrupt() method is a method of the Thread class, and it is used by one thread to signal another thread: it is possible (although it dosen't really make sense) for a thread to interupt itself.
Calling interrupt() causes a thread to go into pool of waiting thread.
Therefor E is the correct answer because calling notify(), notifyAll() or interrupt() just put the waiting thread into SEEKING LOCK stage. And their is no way to specify which thread will get the lock.
Regards
Raj.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic