File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interrupt and wait Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "interrupt and wait" Watch "interrupt and wait" New topic
Author

interrupt and wait

Michael Lin
Ranch Hand

Joined: Dec 04, 2000
Posts: 31
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

Joined: Dec 20, 2000
Posts: 49
I think the answer should be E. Correct me if I am wrong.
atin sehgal
Ranch Hand

Joined: Nov 12, 2000
Posts: 32
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

Joined: Aug 30, 2000
Posts: 3141
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).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Michael Lin
Ranch Hand

Joined: Dec 04, 2000
Posts: 31
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

Joined: Jan 15, 2001
Posts: 21
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.

-LHS
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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

Joined: Jan 16, 2001
Posts: 8
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

Joined: Aug 26, 2000
Posts: 126
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.

Regards,<P>Raj.<BR>-------------------------<BR>Afforts should be Appriciated.<BR>-------------------------
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: interrupt and wait
 
Similar Threads
Thread interrupt confusion
Threads- interrupt()
Q on thread at Jxam
stopping a thread
threads