Multithreading and Thread Synchronization Lecture Note
Multithreading and Thread Synchronization Lecture Note
Multithreading and Thread Synchronization Lecture Note
SYNCHRONIZATION
Compiled By G.T 1
Thread Basics
Compiled By G.T 2
cont…
Compiled By G.T 4
MULTITHREADING
Compiled By G.T 5
Understanding Multithreading
Compiled By G.T 6
Cont…
Compiled By G.T 7
logical concurrency and physical
concurrency.
Compiled By G.T 8
Cont…
Compiled By G.T 10
Multiprogramming Vs Multithreading
Compiled By G.T 12
Cont…
Compiled By G.T 13
Cont…
Compiled By G.T 14
HOW JAVA SUPPORTS
MULTITHREADING
Compiled By G.T 15
Cont…
Compiled By G.T 16
Thread life cycle
void run()
The new thread begins its life inside this method
void stop()
The thread is being terminated
Compiled By G.T 17
Cont…
yield()
Causes the currently executing thread object to
temporarily pause and allow other threads to execute
Allow only threads of the same priority to run
Compiled By G.T 18
approaches to creating threads
Compiled By G.T 19
Cont…
Compiled By G.T 20
Creating Subclasses of Thread
Compiled By G.T 21
class MyThread extends Thread
{
static String message[] =
{ "Java", "is", "hot,", "aromatic,", "and", "invigorating."};
public MyThread(String id)
{
super(id);
}
public void run()
{
String name = getName();
for (int i=0;i<message.length;++i) {
randomWait();
System.out.println(name + message[i]);
}}
void randomWait()
{
try {
sleep((long)(3000*Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
} }}
Compiled By G.T 22
class ThreadTest1
{
public static void main(String args[])
{
MyThread thread1 = new MyThread("thread1: ");
MyThread thread2 = new MyThread("thread2: ");
thread1.start();
thread2.start();
boolean thread1IsAlive = true;
boolean thread2IsAlive = true;
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println("Thread 1 is dead.");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println("Thread 2 is dead.");
}
} while(thread1IsAlive || thread2IsAlive);
}
}
Compiled By G.T 23
Cont…
Compiled By G.T 25
class MyClass implements Runnable
{
static String message[] =
{ "Java", "is", "hot,", "aromatic,", "and", "invigorating."};
String name;
public MyClass(String id)
{
name = id;
}
public void run()
{
for(int i=0;i<message.length;++i) {
randomWait();
System.out.println(name+message[i]);
}}
void randomWait()
{
try {
Thread.currentThread().sleep((long)(3000*Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}}}
Compiled By G.T 26
class ThreadTest2
{
public static void main(String args[])
{
Thread thread1 = new Thread(new MyClass("thread1: "));
Thread thread2 = new Thread(new MyClass("thread2: "));
thread1.start();
thread2.start();
boolean thread1IsAlive = true;
boolean thread2IsAlive = true;
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println("Thread 1 is dead.");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println("Thread 2 is dead.");
}
} while(thread1IsAlive || thread2IsAlive);
}
}
Compiled By G.T 27
Thread States
Compiled By G.T 28
Cont…
Compiled By G.T 29
Cont…
Compiled By G.T 30
Cont…
In the not runnable state, a thread is not just waiting for its
share of processing resources, but is blocked waiting for
the occurrence of an event that will send it back to the
runnable state.
Compiled By G.T 31
Cont…
Compiled By G.T 32
Cont…
Compiled By G.T 33
Thread Priority and
Scheduling
From an abstract or a logical perspective, multiple threads
execute as concurrent sequences of instructions.
Compiled By G.T 34
Cont…
Compiled By G.T 35
Cont…
Compiled By G.T 36
Cont…
Compiled By G.T 37
Cont…
Compiled By G.T 38
Synchronization(Concurrency control
techniques)
Compiled By G.T 39
Cont…
Some of the main techniques used to control concurrent
execution of transactions are based on the concept of
locking data items.
Locks are used as a means of synchronizing the access by
concurrent threads to the different java objects.
Compiled By G.T 40
Cont…
Compiled By G.T 41
class ThreadSynchronization
{
public static void main(String args[])
{
MyThread thread1 = new MyThread("thread1: ");
MyThread thread2 = new MyThread("thread2: ");
thread1.start();
thread2.start();
boolean thread1IsAlive = true;
boolean thread2IsAlive = true;
do {
if (thread1IsAlive && !thread1.isAlive()) {
thread1IsAlive = false;
System.out.println("Thread 1 is dead.");
}
if (thread2IsAlive && !thread2.isAlive()) {
thread2IsAlive = false;
System.out.println("Thread 2 is dead.");
}
} while(thread1IsAlive || thread2IsAlive);
}
}
Compiled By G.T 42
class MyThread extends Thread
{
static String message[] =
{ "Java", "is", "hot,", "aromatic,", "and", "invigorating."};
public MyThread(String id)
{
super(id);
}
public void run()
{
SynchronizedOutput.displayList(getName(),message);
}
void randomWait()
{
try {
sleep((long)(3000*Math.random()));
} catch (InterruptedException x) {
System.out.println("Interrupted!");
}}}
Compiled By G.T 43
class SynchronizedOutput
{
public static synchronized void displayList(String
name,String list[])
{
for(int i=0;i<list.length;++i) {
MyThread t = (MyThread) Thread.currentThread();
t.randomWait();
System.out.println(name+list[i]);
}
}
}
Compiled By G.T 44
A Thread's Life
Compiled By G.T 45
END……
Compiled By G.T 46