Introduction to Threads


Why Threads

if ( balance >= withdrawalRequest )
balance -= withdrawalRequest;

Example

Advantages of threads vs. process-level multitasking

The main thread

The JRE distinguishes between 2 kinds of threads: user & daemon.
User thread:
As long as a user thread is alive, execution does not terminate.
Daemon thread:
Daemon threads serve user threads.  If their parent thread, the thread that spawned them, dies, so do they.

Thread Creation

Threads are implemented by either:

Implementing the Runnable Interface

Extending the Thread Class

Synchronization

Monitors

Synchronized Methods

  • If a method is atomic (i.e., should be executed in its entirety or not at all), then synchronize it.
  • For example, if a data structure is operated on by several threads, you can synchronize them.

  • One way is to synchronize the data structure's methods.
     
    class StackImpl
    {
        private Object stackArray[ ];
        private int topOfStack;
           . . .
        public synchronized Object pop( )
        {
            Object obj = stackArray[topOfStack];
            stackArray[topOfStack--] = null;     // No underflow check!
            return obj;
        }

        public synchronized void push( Object element )
        {
            stackArray[++topOfStack] = element;    // No overflow check!
        }
    }

     
  • When a thread is executing a synchronized method:
  • You can declare a static method synchronized.
  • Correctness heuristic: In a class intended to be thread safe, all methods are synchronized.
  • Synchronized Blocks

    Thread Transitions

    Thread States


     

    Thread Priorities

    Thread Scheduler

    Actual implementations are platform-dependent.
    Write once, run everywhere, but not necessarily with the same thread scheduling semantics :(

    Running & Yielding

    Sleeping & Waking Up

    Thread.sleep() often is used to control an animation, although this can be done via the repaint( sleeptime ) method.

    Waiting & Notifying

    Miscellaneous Methods in the Thread Class

                This method returns true if and only if the thread is alive (i.e., not dead, not terminated).         Causes invoker to wait until thread has terminated.            Thread[Counter A,5,main]
                Thread[Counter B,5,main]
                Wait for the child threads to finish.
                Counter A: 0
                Counter B: 0
                Counter A: 1
                Counter B: 1
                Counter A: 2
                Counter B: 2
                Counter A: 3
                Counter B: 3
                Counter A: 4
                Counter B: 4
                Exit from Counter A.
                Exit from Counter B.
                Counter A not alive.
                Counter B not alive.
                Exit from Main Thread.

    Document thread-safety

    If a class's objects are mutable, but its methods guarantee to leave it in a consistent state regardless of how many threads have references to it, then indicate that the class is thread-safe in its Javadoc.