Java Threads & Synchronization
Java Threads & Synchronization
Which to choose:
If you extend the Thread Class, that means that subclass
cannot extend any other Class, but if you implement Runnable
interface then you can do this.
And the class implementing the Runnable interface can avoid
the full overhead of Thread class which can be excessive.
Else use Thread
Priority
The priority of a thread tells the scheduler how
important this thread is.
The thread scheduler can use the thread priorities
to determine the execution schedule of threads.
Priorities are integer values
Thread.MIN_PRIORITY: 1
Thread.MAX_PRIORITY: 10
Thread.NORM_PRIORITY: 5
Joining
One thread may call join( ) on another thread to
wait for the second thread to complete before
proceeding.
Yielding
Causes the currently executing thread to pause and
allow other threads to execute.
This hint (and it is a hint—there’s no guarantee your
implementation will listen to it) takes the form of the
yield() method.
In general, yield() is useful only in rare situations
and you can’t rely on it to do any serious tuning of
your application
Sleeping
Causes the currently executing thread to pause for a given
number of milliseconds.
When you call sleep( ), it must be placed inside a try block
because it’s possible for sleep( ) to be interrupted before it
times out.
It just stops the execution of the thread for a while.
There is no guaranty that thread will resume the execution after
the given number of milliseconds.
Not to use in real-time application.
Interrupting
Interruption is a mechanism whereby a thread that is waiting
(or sleeping) can be made to prematurely stop waiting.
Volatile Variable: Every time the variable is used it must be read from
main memory. Similarly, every time the variable is written, the value
must be stored in main memory.
Synchronization (Cont…)
Only methods (or blocks) can be synchronized, Classes and variable
cannot be synchronized.
If two threads wants to execute a synchronized method in a class, and
both threads are using the same instance of the class to invoke the
method then only one thread can execute the method at a time.
stop() method doesn’t release the locks. So use a flag to tell the
thread when to terminate itself by exiting its run( ) method.
notifyAll()
Interrupting a blocked thread
There are times when a thread blocks—such as when
it is waiting for input—and it cannot poll a flag as it
does in the previous example. In these cases, you
can use the Thread.interrupt( ) method to break out
of the blocked code.
Other threads that define the same variable create their own
copy of the variable. This means that thread local variables
cannot be used to share state between threads.
public class ThreadLocal<T>
{
protected T initialValue ( );
public T get( );
public void set (T value);
public void remove( );
…
}
InheritableThreadLocal
InheritableThreadLocal is a subclass of
ThreadLocal and allows a thread-specific
value to be inherited from the parent thread
to the child thread.
There are not any public methods on
InheritableThreadLocal. It has one protected
method childValue();
New Java API for Concurrency
Time out…
Discuss later
Java Threads &
Synchronization
Introduction
Exploring Threads
Sharing Resources
Inter-Thread Communication
Deadlock
Other Stuff
Assignment
Assignment
Modify Restaurant.java so that multiple Customers will place order
requests with WaitPersons, who give the requests to the Chefs, who
fulfill the orders and notify the appropriate WaitPerson, who gives it to
the appropriate Customer.