Documente Academic
Documente Profesional
Documente Cultură
Main Thread
When java program starts it usually called the main thread of the program. All other threads are spawned from the main thread. The main thread must be the last thread to finish execution. When the main thread stops, the program terminates.
10
Creating a Thread
There are two ways to create a thread. 1. By implementing the Runnable interface. 2. By extending the Thread class.
11
Creating a Thread
public class RunnableDemo implements Runnable { Thread t; public RunnableDemo() { t=new Thread(this,Runnale Demo); t.start(); } public void run() { // thread code } } The Thread Constructor Thread(Runnable threadObj, String threadName) Example:
13
is responsible for two things: Instructing the JVM to create a new thread Call your Thread objects run() method in the new thread
14
Creating a Thread
public class ThreadDemo extends Thread { public ThreadDemo() { super(demo thread); start(); } public void run() { // thread code } }
The super() used inside the ThreadDemo class take the form of Thread constructor public Thread (String threadName) Example:
15
Thread methods
void start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. static void sleep(long millis) Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. static void sleep(long millis, int nanos) Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds.
17
Thread methods
public final void setName(String name) Changes the name of this thread to be equal to the argument name. public final String getName() Returns this thread's name. public final ThreadGroup getThreadGroup() Returns the thread group to which this thread belongs. This method returns null if this thread has died (been stopped).
18
Thread methods
public static int activeCount() Returns the number of active threads in the current thread's thread group. public String toString() Returns a string representation of this thread, including the thread's name, priority, and thread group. public static Thread currentThread() Returns a reference to the currently executing thread object.
19
Thread methods
public void destroy() Destroys this thread, without any cleanup public final boolean isAlive() Tests if this thread is alive. A thread is alive if it has been started and has not yet died. public final void join(long millis) throws InterruptedException Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
20
Synchronization
When two or more threads needs to access the shared resources, that resource will be used by only one thread at a time. The process by which this is achieved is called synchronization. Java uses the concept called monitor to achieve synchronization. Monitor is a object that is used as a mutually exclusive lock.
21
Synchronization
Only one thread can enter a monitor at a time. All other threads attempting to enter in to the monitor will be suspended until the first thread exits the monitor.
22
Synchronization
Java allows to use a keyword called synchronized. class SyncDemo { synchronized void mathod() { // method body } } Example:
23
Interthread communication
Java provides the interthread communication via the wait(), notify() and notifyAll() methods. All the three methods can be called only from within a synchronized method Void wait(long timeout) Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
24
Interthread communication
Void wait() Causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. void notify() Wakes up a single thread that is waiting on this object's monitor. void notifyAll() Wakes up all threads that are waiting on this object's monitor.
25