Chakradhar: Presentation by
Chakradhar: Presentation by
Chakradhar
Multi Threaded
Programming
PROGRAMMING
MULTI THREAD
TASK(S)
MULTI PROCESSING
RUNNING
implements extends
Runnable Thread
override a method
public void run()
{
statements;
}
Runnable Implementation
class NewThread implements Runnable
{
variable declaration;
NewThread()
{
variable initialisation;
}
public void run()
{
thread code
}
}
Thread of Runnable type
Creating an instance of runnable object
NewThread r = new NewThread();
Registering a Thread
t.start();
Extending a Thread
class NewThread extends Thread
{
variable declaration;
NewThread()
{
variable initialisation;
}
public void run()
{
// thread executable code
}
}
Creating a Thread
Registering a Thread
t.start();
Thread Attributes
MAX_PRIORITY 10
MIN_PRIORITY 1
NORM_PRIORITY 5
Thread Constructors
Thread()
Thread(String)
Thread(Runnable)
Thread(Runnable, String)
Thread(ThreadGroup, String)
Thread(ThreadGroup,Runnable)
Thread(ThreadGroup,Runnable,String)
Thread Methods
Thread t=Thread.currentThread()
t.start()
t.join()
t.sleep(long time)
t.yield()
t.setPriority(int)
t.setDaemon(boolean b)
String name = t.getName()
boolean b = t.isAlive()
Daemon Thread
These are Threads which will execute in
background without our knowledge.
If Daemon threads are the only threads
running in JVM then quits since there are
no threads to which services can be given.
Method : setDaemon(true);
Inter Thread Communication
Communication between Threads in a multithreading
system is important. A Thread which share resources
must communicate in order to coordinate their
efforts, as well as to prevent situations in which one
process disrupts another. In instances where two or
more Threads are working together, synchronization
becomes an issue.
1. Resources Sharing (wait & notify)
2. Process Synchronisation (Synchrozation)
S1 0 T1
S2 3 0
S3 0 T4 T5
there are 3 semaphores in above diagram:
S1 has a value of zero and Thread T1 is blocked on S1
S2 has a value of three, meaning three more Threads
may execute a down operation on S2 without being
put to sleep
S3 has a value of zero, and has two sleeping Threads,
T4 and T5, blocked on it.
Thread Racing/Monitor
Sleeping
Semaphore Value
Processes
S1 0 0
S2 3 0
S3 0 T4 T5
class Resource
{
synchronized void fileRead(String file)
{
System.out.println(“[started “+file);
Thread.currentThread().sleep(1000);
System.out.println(“end of “+file+”]”);
}
}
Synchronization
class UseThread extends Thread
{
Resource res;
String file;
UseThread(String file, Resouce res)
{
this.res=res;
this.file=file;
this.start();
}
public void run()
{
res.fileRead(file);
}
}
Synchronization
class SyncDemo
{
public static void main(String[] args)
{
System.out.println(“Start of main application”);
Resource r=new Resource();
UseThread ut1=new UseThread(“ABCD”, r);
UseThread ut2=new UseThread(“XYZ”, r);
try
{
ut1.join(); ut2.join();
}
catch(Exception e){e.printStackTrace();}
System.out.println(“End of mail Application”);
}
}
Synchronization
2nd Method Synchronisation:
this is process of monitoring the resources
which are non synchronized from using class.
Example:
public void run()
{
synchronized (res)
{
res.readFile(file);
}
}
Synchronization
3rd Method Synchronisation:
Critical or Atamic code is a code which should
execute by acquiring lock on object which is
providing services through the code.
Example: Example:
public void run() public void readFile(String file)
{ {
simple code; simple code;
synchronized (this) synchronized (this)
{ {
critical code; reading of file;
} }
simple code; simple code;
} }
Inter Thread Communication
2 2
Thread Dead Lock
class A
{
synchronized void one(B ob)
{
System.out.println(“ In one of A”);
Thread.sleep(1000);
ob.two();
}
synchronized void two()
{
System.out.println(“ In two of A”);
}
}
Thread Dead Lock
class B
{
synchronized void one(A oa)
{
System.out.println(“ In one of B”);
Thread.sleep(1000);
oa.two();
}
synchronized void two()
{
System.out.println(“ In two of B”);
}
}
Thread Dead Lock
class Thread1 extends Thread
{
A oa; B ob;
Thread1(A oa, B ob)
{
this.oa=oa;
this.ob=ob;
}
public void run()
{
oa.one(ob);
}
}
Thread Dead Lock
class Thread2 extends Thread
{
A oa; B ob;
Thread1(A oa, B ob)
{
this.oa=oa;
this.ob=ob;
}
public void run()
{
ob.one(oa);
}
}
Thread Communication
PipedThread with Thread with
POS.write(byte) PIS.read()
Methods:
setMaxPriority(int p)
setDaemon(boolean b)
activeCount()
enumerate(Thread[ ] tlist)
enumerate(ThreadGroup[ ] tlist)