Runnable interface
- void run() method executed by the new thread
Thread class
implements the Runnable interface
- Thread() construct a new Thread
- Thread(Runnable target) construct a new Thread object
associated with the given Runnable object
- Thread(String name) construct a new Thread with
a given
name
- Thread(Runnable target, String name) construct a new Thread
with a given name and associated with the given Runnable
object
- void setName(String name) set the name of the Thread
- String getName() return the name of the Thread
- void start() create a new thread and execute the run()
method in it
- void run() method executed by the new thread
- static void sleep(long milliseconds
static void sleep(long milliseconds, int nanoseconds) put
the current executing thread to sleep for the specified duration - void
stop() terminate an already running thread
(deprecated because
all the monitors locked by the thread are unlocked, the locked objects
will now be available to the other threads, but they may be damaged -
i.e.
in an inconsistent state - because the stopped thread has not finished
its jobs on them)
- void suspend() suspend a thread (deprecated because
the
suspended
thread may lock an object that will be required by the thread that
should
calls resume(), this results in a deadlock)
- void resume() resume a suspended thread (deprecated as
for suspend())
- boolean isAlive() test if a Thread is alive, a thread
is
alive
sometime before it is started to sometimes after it is stopped
- void join() wait that the thread is not alive
anymore
- void join(long timeout) and void join(long
timeout,
int nanos) wait that the thread is not alive anymore, but no
longer
than the specified duration
- static Thread currentThread() return the current
thread
- static int enumerate() get all the Threads in the
program,
the
return value (equal to the number of Threads stored into the array) is
the minimum of the array size and of the total number of threads
- static int activeCount() return the number of threads
in
the program
Two ways to create a new thread
- create a class FooBar deriving from Thread
FooBar fb = new FooBar();
fb.start();
- create a class FooBar implementing the Runnable interface
Runnable fb = new FooBar();
Threadth = new Thread(fb);
th.start();
synchronized
A synchronized instance method (i.e. public synchronized int
fooBar()
{...}) or a synchronized block (i.e. synchronized(obj) {...})
blocks until the mutex of the object is acquired.
A synchronized class method (i.e. public synchronized static int
fooBar() {...}) blocks until the mutex of the Class object
corresponding
to the class is acquired.
wait()/notify()
These methods are methods of the Object class,
They must be called from a block or method where the object is
synchronized
- void wait() free the object mutex, wait for a
condition to
occur,
acquire the mutex and return
- void wait(long timeout) and void wait(long
timeout,
int nanos) wait for a condition to occur, but no longer than the
specified
duration
- void notify() notify one of the waiting thread that a
condition
has occurred
- void notifyAll() notify all the waiting threads that a
condition
has occurred
notifyAll must be used when
- the condition can satisfy several threads
- several threads are waiting for several conditions and the one
receiving
awoken by notify is not the right one
example:
in some part of the code
synchronized (lock) {
while (lock.getLock()) {
try {
lock.wait();
} catch (Exception e) {}
}
}
somewhere else
synchronized (lock) {
someComputation();
lock.notify();
}
Last update: July 26th,
2003 - Laurent