Controlling Individual Threads Threading Concepts

10. next method is from Money object. This is the metho d called by the previous 11. line marked . 12. 13. public void subtractMoney otherMoney { 14. _cents --= otherMoney.getCents ; 15. } } 16. The thread that becomes active is the one handling Rachels request to withdraw 900. This transaction completes, and _balance is now 500. 17. Marys request now completes since _balance.subtract doesnt actually validate its argument. At the end of this, Mary and Rachel have withdrawn 2,100 from their account, and their account shows a balance of -700. Actually, the bank example already is a multithreaded application. RMI automatically allocates a set of threads roughly, one per open socket, which listen for remote method invocations. The scenario I described can actually happen if you try to run the bank example, as it was implemented in Chapt er 9 , with more than one client. As these examples show, what is needed is a way to coordinate the actions undertaken by distinct threads. In particular, we need four pieces of functionality: • A way to control individual threads • A way to coordinate thread activities • A way to manage a threads cache • A way to assign priorities to threads Ill now drill down on each of these four categories, explaining exactly what they mean and why theyre required. Then, in the next section, Ill explain how Java implements each of these pieces of functionality.

11.3.1 Controlling Individual Threads

A thread has a lifecycle. From when it is first created until it is destroyed, it is in one of the following states: New The thread exists, but hasnt begun to do anything. Running and active A thread is running if it is in the middle of performing operations. It is active if it is running and actually occupies a processor at the current time. We also refer to this state as, simply, active. Running and inactive A thread is running and inactive if it is in the middle of performing operations but does not actually occupy a processor at the current time. We also refer to this state as, simply, inactive. Suspended A thread is suspended if it cannot simply start processing. That is, it isnt running and wouldnt be able to use the processor even if it were given some CPU time. But it will, at some point in the future, run once again. Dead A thread is dead if it has no further operations to perform. It is very hard, inside a program, to distinguish between active and inactive threads. The problem is this: suppose you had some way of determining whether a thread was active. By the time you got around to taking action based on this information, the answer might very well have changed. From the point of view of thread management, the important distinction is between running and suspended. The three operations you need most are: • Suspend a thread for a period of time and then have it resume running. This is frequently useful when applications are polling a source of information. For example, an email client will occasionally check to see whether more email has arrived. It doesnt need to do so very often, so suspending the thread that checks for new email can be a useful thing to do. • Suspend a thread until a particular mutex becomes available, and then have it grab the mutex and resume running. This is exactly what we want for our bank example. • Suspend a thread until another thread has died. This is often used for reporting back on the outcome of a task. For example, generating a monthly report on a database might involve two threads: one that actually does the computation and another that waits for that thread to finish so it can tell the world that the monthly report is available.

11.3.2 Coordinating Thread Activities