>>previous>>
8. Multhithreading in Java
Thread
A process is a program in a execution. A thread is a line of execution. Two or more processes running concurrently in a computer is called multitasking. The process can contain multiple threads to execute its different sections. This is called multhreading. Using this, we can run different parts of the programs at a time.
States of thread
There are four states of thread. They are new, runnable, blocked and dead The fig shows the states of the thread.
start( ) suspend( )/wait( ) / sleep( )
resume( )
stop( ) run( ) stop( )
Adv of thread:
1. Can be created faster
2. Maximum use CPU time
3. Requires less overhead
4. Interprocess communication is faster
Context switching is faster.
Thread is created by
a) the class Thread which has the interface Runnable.
b) creating the objects of the class Thread
a) Using runnable interface
class c1 implements Runnable
{
} To create a thread
Thread t = new Thread(this) ;
Thread t = new Thread(this,"Demo thread’);
Where this referes the Applet object.
After thread is created , it will first execute start( ) method then run( ) method is automaticall
called.
b) Creating Thread Class Objects
class c1 extends Thread
{
}
Thread
Thread(Runnable, String)Thread(ThreadGroup, Runnable, String)
ThreadGroup
ThreadGroup(String name)
ThreadGroup(ThreadGroup ob,String name) Example:
class thread1 implements Runnable
{
Thread t = new Thread(this,"test thread") ;
System.out.println("Child thread : " + t) ;
t.start( ) ;
}
public void run( )
{
try{
for(int I=5; I>0; I--)
{ System.out.println("child thread"+I);
Thread.sleep(500);
}
} catch(InterruptedException e) { }
System.out.println("exiting child thread");
}
public static void main(String g[ ])
{
thread1 th = new thread1( );
try{
for(int j=5;j>0;j--)
{
System.out.println("Main thread : "+ j);Thread.sleep(1000);
}
} catch (InterruptedException e) { }
System.out.println("Main thread exiting …");
}
}
Creating Thread extends Thread class
class thread2 extends Thread
{
thread2( )
{
super("test thread") ;
System.out.println("Child thread : " + this) ;
start( ) ;
}
public void run( )
{
try{
for(int I=5; I>0; I--)
{ System.out.println("child thread"+I);
Thread.sleep(500);
}
} catch(InterruptedException e) { }
System.out.println("exiting child thread");
}
public static void main(String g[ ])
{
new thread2( );
try{
for(int j=5;j>0;j--)
{
System.out.println("Main thread : "+ j);Thread.sleep(1000);
}
} catch (InterruptedException e) { }
System.out.println("Main thread exiting …");
}
}
Runnable abstracts a unit of executable code. We can construct a thread on any object that
implements Runnable.
The thread methods are start( ), resume( ), sleep( ), suspend( ) , join( ) and toString( )
.
Synchronization
Two or more threads accessing the same data simultaneously may lead to loss of data integrity.
Java uses the concept of monitor. A monitor is an object, used as a mutually exclusive lock.
Java offers interprocess communication through the use of wait( ), notify( ) and notifyall( )
methods of Object class and all are synchronized methods.
Thread Priorities
The usage of setPriority( ) and getPriority( ) methods are used to set and get the priorities of
thread respectively. The yield( ) method enables provision of CPU’s time to threads with equal priority and prevents monopolization of a single thread. The Thread has final variables declared line – NORM_PRIORITY (5), MINIMUM_PRIORITY (1) and MAXIMUM_PRIORITY (10).
>>>NEXT>>>
No comments:
Post a Comment