Free Guides
Language Tutorials

SUN
Index

Threads
-
Defined by either extending java.lang.Thread or by implementing java.lang.Runnable interface. The run() method needs to be overridden. This method must be public and void and should take no arguments.
-
Extending
class MyThread extends Thread {
public void run() {/* ... */}
}
You never call run() directly. Instead you call the start() method of the Thread class.
MyThread my = new MyThread();
mt.start(); -
Implementing
class MyRunnable implements Runnable {
public void run() {/* ... */}
}
Here you construct a thread by passing the instance of the Runnable class as an argument.
MyRunnable mc = new MyRunnable();
Thread t = new Thread(mc);
t.start(); -
Thread Constructors available
-
Thread()
-
Thread(String name)
-
Thread(Runnable runnable)
-
Thread(Runnable runnable, String name)
-
Thread(ThreadGroup g, Runnable runnable)
-
Thread(ThreadGroup g, Runnable runnable, String name)
-
Thread(ThreadGroup g, String name)
-
-
wait(), notify and notifyAll() have to be called from a synchronized context.
-
A dead thread can never enter any other state, even if the start() method is called on it.
-
yield() method causes the current thread to move from the running to the runnable state to give other threads a chance to run.
-
When a thread calls join() on another thread, the currently running thread will wait until the thread it joins with has completed.
-
The argument to a synchronized keyword (in the case of blocks) is the reference of the object to be synchronized on. In the case of methods, the synchronized keyword merely precedes the method.
-
Invoking start() twice on the same thread will throw an IllegalThreadStateException at runtime.