Threading concept is very important in Java Programming language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetime
Threading concept is very important in Java Programming language. A thread is a sequential path of code execution within a program. And each thread has its own local variables, program counter and lifetimeIntroduction:
Threading
concept is very important in Java Programming language. A thread is a sequential
path of code execution within a program. And each thread has its own local
variables, program counter and lifetime. In single threaded runtime environment,
operations are executes sequentially. That means next operation can execute only
when the previous one is complete. But a Multithreading term allows more than
one thread to be running concurrently with in a program. Sometimes thread can be
referred as lightweight processes because in a program they exist in a common
memory space and that?s why they can share both data and code. By using
threading we can increased the speed of any application.
Parallel Processing:
Parallel
processing simultaneously and concurrently execution of two or more processes of
same computer program by using two or more processors. Parallel processing makes
a program execution faster because there are more CPUs running it. Parallel
processing requires two or more interconnected processors, each of which
executes a different portion of task. Parallel processing differs from
multitasking, in which a single CPU executes several programs at once.
Multitasking & Multithreading:
Multitasking
allow to execute more than one tasks at the same time, a task being a program.
In multitasking only one CPU is involved but it can switches from one program to
another program so quickly that's why it gives the appearance of executing all
of the programs at the same time. Multitasking allow processes (i.e. programs)
to run concurrently on the program. For Example running the spreadsheet program
and you are working with word processor also.
Multitasking is running heavyweight processes by a single OS.
Multithreading is running multiple lightweight processes in a single process/
task or program. For Example, When you used a word processor you performs a many
different tasks such as printing, formatting text, spell checking and so on.
Multithreaded software treats each process as a separate program.
Advantages of multithreading over multitasking: Some advantages of multithreading over multitasking are :
Main Thread: When any standalone application is running, it firstly execute the main() method runs in a one thread, called the main thread. If no other threads are created by the main thread, then program terminates when the main() method complete its execution. The main thread creates some other threads called child threads. The main() method execution can finish, but the program will keep running until the all threads have complete its execution.
Thread Creation: In Java, an object of the Thread class can represent a thread. Thread can be implementing by one of two ways:
For creating a thread a class have to extend the Thread Class. For creating a thread by this procedure you have to follow these steps:
The following program demonstrates the thread creation way by extending Thread Class:
class MyThread extends Thread{ MyThread(String s){ super(s); start(); } public void run(){ for(int i=0;i<10;i++){ System.out.println("Thread Name :"+Thread.currentThread().getName()); try{ Thread.sleep(100); }catch(Exception e){} } } } public class RunThread{ public static void main(String args[]){ System.out.println("Thread Name :"+Thread.currentThread().getName()); MyThread m1=new MyThread("My Thread 1"); MyThread m2=new MyThread("My Thread 2"); } }
C:\j2se6\thread>javac
RunThread.java C:\j2se6\thread>java RunThread Thread Name :main Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 C:\j2se6\thread> |
Implementing the java.lang.Runnable Interface
The procedure for creating threads by implementing the Runnable Interface is as follows:
The following program demonstrates the thread creation way by implenting the Runnable interface:
class MyThread1 implements Runnable{ Thread t; MyThread1(String s){ t=new Thread(this,s); t.start(); } public void run(){ for(int i=0;i<10;i++){ System.out.println("Thread Name :"+Thread.currentThread().getName()); try{ Thread.sleep(100); }catch(Exception e){} } } } public class RunableThread{ public static void main(String args[]) { System.out.println("Thread Name :"+Thread.currentThread().getName()); MyThread1 m1=new MyThread1("My Thread 1"); MyThread1 m2=new MyThread1("My Thread 2"); } }
Output of the Program is:
C:\j2se6\thread>javac
RunableThread.java C:\j2se6\thread>java RunableThread Thread Name :main Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 Thread Name :My Thread 1 Thread Name :My Thread 2 C:\j2se6\thread> |
Thread Constructors: Several
constructors are available for creating new Thread instances.
Thread()
Thread(String)
Thread(Runnable)
Thread(Runnable,String)
Thread(ThreadGroup,String)
Thread(ThreadGroup,Runnable)
Thread(ThreadGroup,Runnable,String)
Thread(ThreadGroup, Runnable, String, long)
ThreadGroup? All threads belongs to an instance of the ThreadGroup Class. ThreadGroup is used to represent a group of threads. ThreadGroups are hierarchical: there is only one root ThreadGroup and it contains all other thread and groups and each subgroups can contain other groups and threads. All thread have only one thread group. And all thread groups (except the root thread group) belongs to exactly one parent thread group. Threads can access the only belonging thread group.
When a new ThreadGroup is created, it
is added as a member of existing ThreadGroup.
If a thread x in group1, and executes the code:
ThreadGroup group2=new ThreadGroup(?group2?);
Then the newly formed group2 comes under group1. If you want a parent group other than default then you have to specify the parent group at the time of creation.
ThreadGroup group2=new ThreadGroup(group2,?group3?);
Then newly formed group3 comes under the group2.
Some important methods are:
getName() ? This method
is used to retrieve the name of particular group.
ThreadGroup g=new ThreadGroup(?RoseIndia?);
String gname=g.getName();
getParent() ? This
method is used to retrieve the name of parent threadgroup of sub group.
ThreadGroup group=group3.getParent();
activeGroupCount() ?
This method returns the number of active thread group in a particular thread
group and all its subgroups.
int size=group.activeGroupCount();
getThreadGroup() ? This
method is used to know the thread is belong to which thread group.
ThreadGroup group=threadx.getThreadGroup();
activeCount() ? This method returns the number of active threads in a particular thread group and all its subgroups.
Life Cycle of Threads: When
you are programming with threads, understanding the life cycle of thread is very
valuable. While a thread is alive, it is in one of several states. By invoking
start() method, it doesn?t mean that the thread has access to CPU and start
executing straight away. Several factors determine how it will proceed.
Different states of threads are-
Different Thread States
Some Important Methods:
Thread
Priorities:
Thread scheduler can use the thread priorities to
determine the execution schedule of threads. Thread gets the ready-to-run state
according to their priorities. The thread scheduler provides the CPU time to
thread of highest priority thread in the ready-to-run state.
Priorities
are integer values from 1 (lowest priority given by the constant Thread.MIN_PRIORITY)
to 10 (highest priority given by the constant Thread.MAX_PRIORITY). The
default priority is 5(Thread.NORM_PRIORITY).
setPriority()
? This is method is used to set the priority of thread.
getPriority() ? This method is used to get the priority of thread.
Lock: This term refers to the access granted to
a particular thread that can access the shared resources. At any given time,
only one thread can hold the lock and thereby have access to the shared
resource. Every object in Java has build-in lock that only comes in action when
the object has synchronized method code. By associating a shared resource with a
Java object and its lock, the object can act as a guard, ensuring synchronized
access to the resource. Only one thread at a time can access the shared resource
guarded by the object lock.
Since there is one lock per object, if one thread has acquired the lock, no
other thread can acquire the lock until the lock is not released by first
thread. Acquire the lock means the thread currently in synchronized method and
released the lock means exits the synchronized method. Remember the following
points related to lock and synchronization:
There are two ways to synchronized the execution of code:
Synchronized Methods:
If any method is specified with the keyword synchronized then this method of an object is only executed by one thread at a time. A any thread want to execute the synchronized method, firstly it has to obtain the objects lock. Acquire the method is simply by calling the method. If the lock is already held by another thread, then calling thread has to wait.The following program demonstrates the synchronized method:
public class SynThread{ public static void main(String args[]){ Share s=new Share(); MyThread m1=new MyThread(s,"Thread1"); MyThread m2=new MyThread(s,"Thread2"); MyThread m3=new MyThread(s,"Thread3"); } } class MyThread extends Thread{ Share s; MyThread(Share s,String str){ super(str); this.s=s; start(); } public void run(){ s.doword(Thread.currentThread().getName()); } } class Share{ public synchronized void doword(String str){ for(int i=0;i<5;i++){ System.out.println("Started :"+str); try{ Thread.sleep(100); }catch(Exception e){} } } }
Output of the program is:
C:\j2se6\thread>javac
SynThread.java C:\j2se6\thread>java SynThread Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread2 Started :Thread2 Started :Thread2 Started :Thread2 Started :Thread2 C:\j2se6\thread> |
Synchronized Blocks (Statements): A synchronized statement is another way to create synchronized code. Synchronized statements must specify the object that provides the intrinsic lock. The synchronized block allows execution of arbitrary code to be synchronized on the lock of an arbitrary object.
General form of synchronized block is:
synchronized (object reference expression)
{
code block..
}
The following program demonstrates the synchronized block:
public class SynStatement{ public static void main(String args[]){ Share s=new Share(); MyThread m1=new MyThread(s,"Thread1"); MyThread m2=new MyThread(s,"Thread2"); MyThread m3=new MyThread(s,"Thread3"); } } class MyThread extends Thread{ Share s; MyThread(Share s,String str){ super(str); this.s=s; start(); } public void run(){ s.doword(Thread.currentThread().getName()); } } class Share{ public void doword(String str){ synchronized(this){ for(int i=0;i<5;i++){ System.out.println("Started :"+str); try{ Thread.sleep(100); }catch(Exception e){} } } } }
Output of the program is:
C:\j2se6\thread>javac SynStatement.java C:\j2se6\thread>java SynStatement Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread1 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread3 Started :Thread2 Started :Thread2 Started :Thread2 Started :Thread2 Started :Thread2 C:\j2se6\thread> |
Deadlock: A situation where a thread is waiting for an object lock that holds by second thread, and this second thread is waiting for an object lock that holds by first thread, this situation is known as Deadlock.
The following program demonstrates the deadlock situation:
public class DeadDemo{ public static void main(String args[]){ String s1="Dead"; String s2="Lock"; MyThread1 m=new MyThread1(s1,s2); MyThread2 m1=new MyThread2(s1,s2); } } class MyThread1 extends Thread{ String s1; String s2; MyThread1(String s1, String s2){ this.s1=s1; this.s2=s2; start(); } public void run(){ while(true){ synchronized(s1){ synchronized(s2){ System.out.println(s1+s2); } } } } } class MyThread2 extends Thread{ String s1; String s2; MyThread2(String s1,String s2){ this.s1=s1; this.s2=s2; start(); } public void run(){ while(true){ synchronized(s2){ synchronized(s1){ System.out.println(s2+s1); } } } } }
Output of the program is:
C:\j2se6\thread>javac
DeadDemo.java C:\j2se6\thread>java DeadDemo DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock LockDead LockDead LockDead LockDead LockDead LockDead LockDead DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock ......... ......... C:\j2se6\thread> |
Daemon Threads: In Java, any thread can be a Daemon thread. Daemon threads are service providers for other threads or objects running in the same process as the daemon thread. Daemon threads are used for background supporting tasks and are only needed while normal threads are executing. If normal threads are not running and remaining threads are daemon threads then the interpreter exits.
setDaemon(true/false)
? This method is used to specify that a thread is daemon thread.
public boolean isDaemon() ? This
method is used to determine the thread is daemon thread or not.
The following program demonstrates the Daemon Thread:
public class DaemonThread extends Thread { public void run() { System.out.println("Entering run method"); try { System.out.println("In run Method: currentThread() is"+ Thread.currentThread()); while (true) { try { Thread.sleep(500); } catch (InterruptedException x) { } System.out.println("In run method: woke up again"); } } finally { System.out.println("Leaving run Method"); } } public static void main(String[] args) { System.out.println("Entering main Method"); DaemonThread t = new DaemonThread(); t.setDaemon(true); t.start(); try { Thread.sleep(3000); } catch (InterruptedException x) { } System.out.println("Leaving main method"); } }
Output of this program is:
C:\j2se6\thread>javac
DaemonThread.java C:\j2se6\thread>java DaemonThread Entering main Method Entering run method In run Method: currentThread() isThread[Thread-0,5,main] In run method: woke up again In run method: woke up again In run method: woke up again In run method: woke up again In run method: woke up again In run method: woke up again Leaving main method C:\j2se6\thread> |
Ads