1 / 13

Threads in Java

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 .

elkan
Télécharger la présentation

Threads in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Threads in Java

  2. 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

  3. 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.

  4. 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.

  5. 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); … }

  6. Life-cycle of a Thread The various states of a thread are: • New • Runnable • Not runnable • Terminated or Dead

  7. 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.

  8. 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.

  9. 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.

  10. 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.

  11. 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

  12. 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"); } }

  13. 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 +"'!"); } }

More Related