Threads in java


Published on

P2Cinfotech is one of the leading, Online IT Training facilities and Job Consultant, spread all over the world. We have successfully conducted online classes on various Software Technologies that are currently in Demand.

Published in: Education, Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Threads in java

  1. 1. THREADS IN JAVA  What Is a Thread A “thread” is a single sequential flow of control within a program. When multiple threads execute byte-code instruction sequences in the same program, that action is known as “Multithreading”. Multithreading benefits a program in different ways: Multithreaded GUI (graphical user interface)-based programs remain responsive to users while performing other tasks, such as repaginating or printing a document. Threaded programs typically finish faster than their nonthreaded counterparts. This is especially true of threads running on a multiprocessor machine, where each thread has its own processor. Java accomplishes multithreading through its java.lang.Thread class. Each Thread object describes a single thread of execution. That execution occurs in Thread's run() method. Because the default run() method does nothing, you must subclass Thread and override run() to accomplish useful work. For a taste of threads and multithreading in the context of Thread.  Process vs. threads A process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process. Ex: memory and CPU time, are allocated to it via the operating system. A thread is a so called lightweight process. It has its own call stack, but can access shared data of other threads in the same process. Every thread has its own memory cache. If a thread reads shared data it stores this data in its own memory cache. A thread can re-read the shared data. A Java application runs by default in one process. Within a Java application you work with several threads to achieve parallel processing or asynchronous behavior.  create using Thread class An application that creates an instance of Thread must provide the code that will run in that thread. +1-732-546-3607
  2. 2. THREADS IN JAVA There are two ways to create a new thread 1) Extend the Thread class and override the run() method in your class. Create an instance of the subclass and invoke the start() method on it, which will create a new thread of execution. Example: public class NewThread extends Thread{ public void run(){ // the code that has to be executed in a separate new thread goes here } public static void main(String [] args){ NewThread c = new NewThread(); c.start(); } } 2) Implements the Runnable interface. The class will have to implement the run() method in the Runnable interface. Create an instance of this class. Pass the reference of this instance to the Thread constructor a new thread of execution will be created. Example: public class NewThread implements Runnable{ public void run(){ // the code that has to be executed in a separate new thread goes here } public static void main(String [] args){ NewThread c = new NewThread(); Thread t = new Thread(c); t.start(); } } Synchronization: Synchronization is the process of allowing threads to execute one after another. +1-732-546-3607
  3. 3. THREADS IN JAVA Synchronization control the access the multiple threads to a shared resources. Without synchronization of threads, one thread can modify a shared variable while another thread can update the same shared variable, which leads to significant errors. To make a method synchronized, simply add the synchronized keyword to its declaration. The only real difference is that a synchronized block can choose which object it synchronizes on. A synchronized method can only use 'this' (or the corresponding Class instance for a synchronized class method).  Life Cycle of Thread The different states of threads are as follows: 1) New : When a thread is instantiated it is in New state until the start() method is called on the thread instance. In this state the thread is not considered to be alive. +1-732-546-3607
  4. 4. THREADS IN JAVA 2) Runnable : The thread enters into this state after the start method is called in the thread instance. The thread may enter into the Runnable state from Running state. In this state the thread is considered to be alive. 3) Running : When the thread scheduler picks up the thread from the Runnable thread’s pool, the thread starts running and the thread is said to be in Running state. 4) Waiting/Blocked/Sleeping : In these states the thread is said to be alive but not runnable. The thread switches to this state because of reasons like wait method called or sleep method has been called on the running thread or thread might be waiting for some i/o resource so blocked. 5) Dead : When the thread finishes its execution i.e. the run() method execution completes, it is said to be in dead state. A dead state can not be started again. If a start() method is invoked on a dead thread a runtime exception will occur. +1-732-546-3607