You are on page 1of 26

Light Weight Process (LWP).

Is similar to a pgm that has single flow of control

Multithreading:
A MT program contains 2 or more separate parts
that
can run concurrently
Daemon and non daemon threads
Main Thread

start start
start

T1 T2 T3

Switching Switching

3
Main Thread

The main thread is created automatically when


your
program is started
It is the thread from which other “child” threads
will be
spawned.
Often it must be the last thread to finish
execution
public class ThreadDemo {
public static void main(String[] args) {
Thread t=Thread.currentThread();
System.out.println("Thread Details
:"+t);
t.setName("ThreadDemo");
System.out.println("Thread Details
:"+t);
}
}
O/P
Thread Details :Thread[main,5,main]
Thread Details
:Thread[ThreadDemo,5,main]
Threads in Java
Life cycle of a
thread
New born
stop()

start()

stop()
Active Runnable
Running Dead
Thread

yield()
Killed
suspend() resume() Thread
sleep() Notify()
stop()
Wait()
Idle thread Blocked
6
(Not
Creating a Thread in Java

 By Implementing Runnable

By extending Thread


Runnable

Present in java.lang

 public void run()


- provides an entry point for concurrent
thread
inside main

Construction
1.Implement Runnable
2.Create an object of Thread class
3.Call start() on this object
class ThreadDemo implements
Runnable
{
public static void main(String[]
Thread t;
args)
ThreadDemo()
{
{
ThreadDemo td=new
t=new Thread(this,"Child");
ThreadDemo();
t.start();
Thread
}
t1=Thread.currentThread();
public void run()
t1.setName("Parent");
{
try
try
{
{
for(int i=0;i<3;i++)
for(int i=0;i<3;i++)
{
{
System.out.println(t1.getName()
System.out.println(t.getName()+i);
+i);
Thread.sleep(500);
Thread.sleep(500);
}
}
}
}
catch (Exception ex){}
catch (Exception ex){}
}
O/P

Parent0
Child0
Parent1
Child1
Parent2
Child2
Thread

 String getName()
 int getPriority()
 boolean isAlive()
 void join()
 void run()
 void sleep()
 void start()
class ThreadDemo extends Thread
{
ThreadDemo()
public static void main(String[]
{
args)
super("Child");
{
start();
ThreadDemo td=new
}
ThreadDemo();
public void run()
Thread
{
t1=Thread.currentThread();
try
t1.setName("Parent");
{
try
for(int i=0;i<3;i++)
{
{
for(int i=0;i<3;i++)
System.out.println(getName()+i);
{
Thread.sleep(500);
System.out.println(t1.getName()
}
+i);
}
Thread.sleep(500);
catch (Exception ex){}
}
}
}
catch (Exception ex){}
O/P

Parent0
Child0
Parent1
Child1
Parent2
Child2
isAlive() & join()
These methods are used to determine whether a thread
is finished or not.
final boolean isAlive()
-A thread is alive if it has been started and has not yet died.
final void join()
This method waits until the thread on which it is called
terminates.
Its name indicates the calling thread waiting until the
specified thread joins it.
public static void main(String[]
args)
{
ThreadDemo td=new
ThreadDemo();
Thread
t1=Thread.currentThread();
t1.setName("Parent");
td.join();
try
{
for(int i=0;i<3;i++)
{
System.out.println(t1.getName()
+i);
Thread.sleep(500);
}
}
catch (Exception ex){}
Thread Scheduling

void setPriority(intNumber);
intNumber – is an integer value to which the
thread’s
priority is set.
int getPriority()
returns the current priority of the thread
Some priority constants are also there:
MIN_PRIORITY=1
NORM_PRIORITY=5
MAX_PRIORITY=10
Voluntary Rescheduling

public static void sleep(long ms)throws


InterruptedException
Puts the currently executing thread to sleep for at
least the specified number of ms.
public static void yield()
Inform the scheduler that the current thread need not
run at the present time, so the scheduler may choose
another thread to run
Thread Synchronization

In java synchronization of code can b achieved in 2


ways:

1.Using sync. Methods


2.Using sync. Statements
ThreadDemo(Class1 c,String s)
class Class1 {
{ c1=c;
void call(String msg) msg=s;
{ t=new Thread(this);
System.out.println("["+msg); t.start();
Try }
{ public void run()
Thread.sleep(1000); {
} c1.call(msg);
catch (Exception ex) {} }
System.out.println("]"); public static void main(String[]
} args)
} {
Class1 c=new Class1();
class ThreadDemo implements ThreadDemo td1=new
Runnable ThreadDemo(c,"HELLO");
{ ThreadDemo td2=new
String msg; ThreadDemo(c,"HAI");
Class1 c1; ThreadDemo td3=new
Thread t; ThreadDemo(c,"JAVA");
}
O/P

[HELLO
[HAI
[JAVA
]
]
]
Synchronized methods

public synchronized void methName()


{
//all code here are synchronized.
}

Eg:
class Class1{
synchronized void call(…)
}
Synchronized statements

Has 2 parts:
 An object whose lock is to be acquired
and
 A statement to execute when the lock is
obtained

synchronized(expr)
{statements }
expr- is an object reference
In above problem..

public void run()


{
synchronized(c1)
{
c1.call(msg);
}
}
O/P

[HELLO]
[HAI]
[JAVA]
InterProcessCommunication
final void wait()
Lets one thread to wait until some conditions occurs.
final void notify() & notifyAll()
Tell waiting threads that something has occurred that
might satisfy that condition.
notify() : wakes up the first thread that called
wait() on the same object.
notifyAll() : wakes up all waiting threads.

You might also like