200 likes | 400 Views
Threads in Java. A process that is made of one thread is known as single-threaded process . A process that creates two or more threads is called a multithreaded process . For example, any Web browser, such as Internet Explorer is a multithreaded application. Single vs. Multi-Threaded .
E N D
A process that is made of one thread is known as single-threaded process. • A process that creates two or more threads is called a multithreaded process. For example, any Web browser, such as Internet Explorer is a multithreaded application. Single vs. Multi-Threaded
Single-Thread Process • In single-threaded systems, an approach called event loop with polling is used. Polling is the process in which a single event is executed at a time. • In a single threaded application when a thread is suspended from execution because it is waiting for a system resource, the entire program stops running. • Multithreading eliminates event loop/polling mechanism in Java. The time for which a thread waits for the CPU time can be utilized elsewhere.
Using Thread • The java.lang.Thread class is used to construct and access individual threads in a multithreaded application. A thread can be created by either using the Thread class or the Runnable interface.
Main Thread • The first thread to be executed in a multithreaded process is called the main thread. • The main thread is created automatically on the start up of Java program execution. • the currentThread() method of the Thread class can be used to access main thread. public static void main(String args[]) { Thread t= Thread.currentThread(); System.out.println(" The current thread: " + t); t.setName("MainThread"); System.out.println(“The current thread after name change:”+ t); … }
Life-cycle of a Thread The various states of a thread are: • New • Runnable • Not runnable • Terminated or Dead
The New Thread State • To instantiate the Thread class: Thread newThread = new Thread(“threadName"); • A new thread is an empty object of the Thread class and no system resources, such as memory are allocated to it. The thread must be started with start() method. newThread.start(); • When the thread is in the new state, no other method except the start() method can be called, otherwise it an IllegalThreadStateExceptionexception is thrown.
The Runnable thread state • When the start() method of a thread is invoked, the thread enters the runnable state. The start() method allocates the system resources to the thread, schedules the thread, and calls its run() method. • A single processor cannot execute more than one thread at a time, therefore it maintains a thread queue. When a thread is started, a thread is queued up for the processor time and waits for its turn to get executed. As a result, the state of the thread is said to be runnable or not running.
The not Runnable Thread State A thread is not in the runnable state if it is: • Sleeping: A thread is put into the sleeping mode by calling the sleep() method. A sleeping thread enters the runnable state after the specified time has elapsed. • Waiting: A thread can be made to wait for some specified condition to be satisfied by calling the wait() method. The thread can be notified of the condition by invoking the notify() method of the Thread class. • Being blocked by another thread: When a thread is blocked by an I/O operation, it enters the not runnable state.
The Dead Thread State • A thread can either be dead or alive. A thread enters the dead state when the loop in the run() method is complete. • Assigning a null value to a thread object changes the state of the thread to dead. • The isAlive() method of the Thread class is used to determine whether a thread has been started or not. • A dead thread can not be restarted.
Creating Threads A thread can be created by instantiating an object of Thread type in either 2 ways: • Extending the Thread class • Implementing Runnable interface
Extending the Thread Class class ThreadDemo extends Thread { ThreadDemo() { super("ChildThread"); System.out.println("ChildThread:" + this); start(); } public void run() { System.out.println("The child thread started"); System.out.println("Exiting the child thread"); } }
Implementing the RunnableInterface public class NamedRunnable implements Runnable { private String name; private Thread t; public NamedRunnable(String name) { t = new Thread(this, name); this.name = name; t.start(); } public void run() { System.out.println("Greetings from thread '" + name +"'!"); } }