Q5. Given that a static method doIt() in a class Work represents work to be done, what block of code will succeed in starting a new thread that will do the work?
When the thread gets hold of the CPU again, it checks its interrupt status and because it was set its throws the interrupt exception and clears the interrupt status.
If a thread is running and gets interrupted, only its interrupt status is affected and won't throw the interrupt exception.
interrupt(), InterruptedException and other thread exceptions
If you look at the get() method, there is some exception handling code, due to the wait() method. When a thread is put in the wait() queue, conditions can occur that remove it from this queue. For example, there might be a situation where a thread is wait()ing, and another thread comes along and interrupt()s it. This can happen if a reference to the wait()ing thread is held in an object, and another thread runs through this object. This kind of behavor is useful when creating server side objects whose lifecycles you have to control. I will give examples of this in the next section. Now, the strange thing to remember is that when the wait()ing thread gets interrupt()ed, it resumes execution by throwing an exception in the code block of the wait() method. Once the InterruptedException is dealt with, the wait()ing thread is alive again, and if the exception is dealt with properly, should resume execution as it was intended, rather than just throwing an unhandled exception and freezing the thread. An example of this can be seen in the following example. This might be confusing if you are not that familiar with threads; things can get very confusing in your code, when a thread, which was created in one object, runs through other objects.
Let's say that the Consumer object has its own thread, and the Producer object has its own thread, and that they both share the Queue object. Now, after they are running for sometime, let's say that you want to shut down the Consumer, which might happen in a server side environment, where you might have to replace the Consumer with a different object (for example, if the class gets updated in a Servlet environment, or a new version of an object is deployed). In order to shut and take down this Consumer object at runtime, its thread has to be taken off of the wait()ing queue (if it is currently waiting). The best way to do this, is to have the server thread invoke a shutdown method of some sort on the Consumer object. The shutdown method should then invoke the interrupt() method on the Consumer's thread. Once this happens, an exception gets generated in the Queue's get() method (where the Consumer's thread was waiting). Phew! So, the Consumer's thread was interrupted in the Queue's get() method, and it returns null. The Consumer thread's run() method should have logic in it to determine when this has happened, and it should self terminate. Please note that complicated logic occurs because every thread has to have a run() method in which it all happens. This will be illustrated in the next section.
Originally posted by Alfred Kemety:
3- How would it get interrupted? Will another thread call the interrupt() method? How will this happen while the thread is actually in the running state, meaning no other thread is running.. or can 2 threads run at the same time? I don't think so....
Well I have a doubt about concurently, you mean 2 threads are running at the same time? in a multithreading envirnoment, or running in a multiprocessing envirnoment...
1- CPU - as I understand - can handle one process at a time, so if one thread has the CPU, another can't, unless you have multi-processor envirnoment? am I not right?
2- If a thread has the CPU then it's said to be in the running state
3- According to my understanding, there can't be 2 threads in the running state at the same time
4- If all what I said is true, then you mean that a thread invokes interrupt() method on another thread while it's in the ready-to-run state and NOT in the waiting state or sleeping state or actually "Running" state, since both can't be "Running" at the same instance... is that correct?
public void interrupt()Interrupts this thread.
First the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
If none of the previous conditions hold then this thread's interrupt status will be set.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your databasehttps://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database