MULTITHREADING:
A program that has two or more independent subtasks is called as multithreaded program
and each of these independent subtasks is called a thread. it means that each subtask hass its own
path of execution. playing a computer game is a typical example of a multithread program.
Generally a game is combination of multiple threads. each subtask is called a thread.therefore
each thread runs by itself and has the CPU to itself. it is the internal mechanism hat actually
divides CPU time for each thread.
actually,mulithreading is a refined form of a time sharing system. in time sharing system the
CPU executes each process (a self-contained runnig program with its own address space)for a
few milliseconds,then saving its state and switching to the next program.
Multi threading is a specilized form of a multitasking. More than one thread executing
concurrently is called multi threading. More than one process executing concurrently is called
multitaksing. A thread is a light-weight process where as is multitasking is very heavy weight
process.thread can share the common address space. But the process can not share a common
address space.
CurrentThread() : this method returns the current runnding thread to the Thread class object.
Syntax:
Static Thread currentThread();
SetName() : this method can be used to set the internal name of the thread
class ThreadDemo
{
public static void main(String arg[])
{
Thread th=new Thread.currenThread();
System.out.println("The present thread is "+th);
th.setName("ThreadNew");
System.out.println("New Thread is "+th);
}
}
In the above output the statement Thread[main,5,main], in which the first argument main is the
mainthread, 5 is thread priority and third argument main is thread group
The first wayt to create a thread is to create a new class by extending a Thread class. The
extending class must override the run() method. The run method must be called by start()
method.
Extending a thread class :
class MyThread extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("Child Thread "+getName()+"i"+i);
}
System.out.println("Exit from Child Thread" +getName());
}
}
class td
{
public static void main(String arg[])
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
t1.setName("First");
t2.setName("Second");
t3.setName("Third");
t1.start();
t2.start();
t3.start();
System.out.println("Exit from main thread ");
}
}
MyThread()
{
t1=new Thread(this,"First");
t2=new Thread(this,"Second");
t3=new Thread(this,"Third");
t1.start();
t2.start();
t3.start();
for(int i=1;i<=5;i++)
{
st=Thread.currentThread().getName();
System.out.println("Thread name is "+st+"i "+i);
}
System.out.println("Exit from Child Thread "+st);
}
}
class tdimp
{
public static void main(String arg[])
{
MyThread m=new MyThread();
System.out.println("Exit from main Thread ");
}
}
void start() : A thread execution starts by calling start() method which, in turns the run() method.
The start method throws an IllegalThreadException.
void suspend() : This method is used to temporirly stop the execution of a thread. When the code
encounters suspend() method, it saves all the states and resources of the Thread. It can be called
again by resume() method.
static void sleep() : it suspends the the thread for executing for a minimum of the specified time
period. It throws an InterruptedException. So you need to define try-catch statements.
void join() : the thread who calls this method waits for itself to die. It throws an
InterruptedException.
static void yield() : this method is temporarily stops the execution of the caller’s thread and
places the caller’s thread at the end of the queue to wait for another to be executed.
isAlive() : this methods determines that the thread is still running or not. It returns a boolean
value such as true/false.
Class MyThread extends Thread
{
public void run()
{
try
{
for(int i=1;i<=5;i++)
{
System.out.println(“Child Thread name is “+getName()+”i “+_i);
Thread.sleep(10000);
}
}
catch(InterruptedException e)
{ }
class Thdemo
{
public static void main(String arg[])
{
MyThread f=new MyThread();
MyThread s=new MyThread();
MyThread t=new MyThread();
f.setName(“First”);
s.setName(“Second”);
f.start();
s.start();
System.out.println(“First thread is live “+f.isAlive());
System.out.println(“Second thread is ive “+s.isAlive());
try
{
System.out.println(“Waiting to finish first thread “);
f.join();
}
catch(InterruptedException e)
{ }
System.out.println(“First thread is live “+f.isAlive());
System.out.println(“First thread is live “+s.isAlive());
/*
Thread Exceptions:
try
}
------------
}
catch(InterruptedExcepion e)
{
-------------
catch(Exception e)
----------
Thread Protiries :
Every thread in java is assigned a prority value. it is the priority value that effects the
execution of a thread. The thread with the highest priority value is given preference, that is
higher-priority get more cpu time than lower priority threads.
Thread priority values that can be assigned to user created are simple integers ranging
between thread.MIN_PRIORITY and thread.MAX_PRIORITY. if you dont explicitly then by
default each thread is given priority thread.NORM_PRIORITY. generally thread priority
represents from 1 to 10. 1 means very low priority, it is represented with MIN_PRIORITY,10
means MAX_PRIORITY and 5 means NORM_PRIORITY.
thpri(String st)
{
super(st);
}
t1.setPriority(1);
t2.setPriority(10);
t1.start();
t2.start();
}
public void run()
{
String name=" ";
for(int i=1;i<=3;i++)
{
name=Thread.currentThread().getName();
System.out.println("name is "+getName()+i);
System.out.println("name is "+getName()+i);
}
}
}
Synchronisation of threads :
When two or more threads need to share a resource, you must ensure
that the resource will be used by only thread at a time.
*/
class synd
{
t1.start();
t2.start();
t3.start();
}
}
output:
[First]
[Second]
[Third]
suppose if you write
output :
[First
[Second
[Third
]
]
]
Born
|
|
| start() |
| |
| ------------ Ready
| ||
| ||
| yield()| |scheduled()
| ||
| Running ----------dead
|
| stop()
| or
| run()
| complete
|
| -----------------------------------
| | | | |
| wait() | sleep()| suspend()| i/o |locked()
| | | | |
| Ready Sleeping Suspend Blocked
| | | | |
| notify()| sleepti| meout resume()| i/o finished()
| | | | |
| -----------------------------------
| |
--------------------------------|
Bord Thread : a threa enters into a born state immediately after its
creation. that is it enters into a born statem after creating the
thread new statement is executed.
Ready Thread :
the ready thread remains in the born state unit it calls the
start() method.
threadobject.start();
Runnable thread :
when the start() method of a thread is called , the thread enters the
runnable state. the highest priority ready thread enters the running
state after execution of its start() method that executes the run()
method automatically.
Blocked Thread :
A thred comes into blocked state when any one of the following
occures.
Dead Thread : a thread enters the dead state when its execution
finish or it is stopped by another thread calling its stop() method.
you cannot restart a dead thread.
wait() : the wait() method tells the current thread to give up the
monitor and go to sleep until another thread calls eithers notify()
method.
The notify() : this method wakes up the first method that called
wait() on same object.
The notifyAll() : this method wakes up all the threads that are waiting
on the same object for the monitor.