Hello, I have found invariably 1 or more questions in all the mock exams over the calls which stops the thread. While performing search in Javaranch I have found a nice discussion over this "InterruptedException" july 26, 2000). Explanation in this posting have cleared most of my doubts but still am worried about my answer choice in real exam. I am consolidating all the choices. 1. sleep 2. stop 3. wait 4. yield 5. interrupt 6. suspend 7. waitforId/waitforData 8. blocked for io 9. If the current thread starts the new thread 10. Thread with higher priority is in the pool. Ajith has explained that that sleep() does not stop the thread whereas interrupt stops the thread. While Kai's notes and Hunt's mock exam says just the opposite. Also waitforId/waitforData does not stop the thread but blocks. However, all the mock exam includes that as answer which will stops the thread. Please advice me the answer which should be choosen in the exam. Please suggest the answer for all the 10 situations given above. Regards Sanjay PS: This is a repost. Moderators please excuse me and help me in getting the answer.
Joined: Jul 08, 2000
Anyone please answer.... I will be highly grateful. Regards Sanjay
I think it should be like this:- 1. sleep -- The thread sleeps for sometime. Not a stop. 2. stop -- Stop the thread. 3. wait -- It is a wait to be notified. So not a stop. 4. yield -- It causes the currently executing thread object to temporarily pause and allow other threads to execute. Not a stop 5. interrupt -- yes it will stop the thread. 6. suspend -- No it will not stop the thread but will suspend it for sometime and resume it later.. These methods are deprecated now. 7. waitforId/waitforData 8. blocked for io No the thread processes the rest of it once the blocked IO is complete. Like reading a file. 9. If the current thread starts the new thread No this will not stop the thread as the new thread will run in as a seperate entity. 10. Thread with higher priority is in the pool. Usually the HIGH Priority thread waits till CPU gets enough time for it to process this thread. It will not stop the running low priority thread. Can someone put more thoughts to it...
1. sleep 2. stop 3. wait 4. yield 5. interrupt 6. suspend 7. waitforId/waitforData 8. blocked for io 9. If the current thread starts the new thread 10. Thread with higher priority is in the pool. 1.sleep makes the thread to sleep for a specified time interval.after the interval, thread starts executing.so,sleep doesn't stop the thread. 2.stop makes the thread stop. 3.wait makes the thread to wait until some other thread notifies it.so it also doesnot stop the thread. 4.yield makes the thread to leave the monitor and adds to the queue .so it also doesnot stop the thread. 5.interrupt interrupts the thread.but it will not stop the thread. 6.suspend pauses the execution of the thread until it is resumed. 7.waitfor id/waitfordata thread waits for getting id and data,it is not for stopping the thread. 8.blocked for IO thread waits until the IO operation is over.againi it starts execution. 9. 10.if the thread with higher priority is in pool ,then the current thread waits until the higher priority thread's execution completes.it doesn't mean that the thread stop.
Joined: Aug 16, 2000
To stop the thread which is blocked waiting for I/O, use the interrupt() method on the thead that is blocked (e.g. the Thread object created when the thread was started). This will cause the thread to throw an Interrupted exception. Note that if the I/O thread is NOT blocked waiting, the interrupt() method will NOT cause it to throw an exception or interrupt its sequence of execution; in this case the thread itself has to check using isInterrupted() and take action as appropriate. (There is no way in Java to interrupt the flow of execution of a running thread). ------------------ Raj