0% found this document useful (0 votes)
3 views10 pages

Class Running Notes 21th Nov

The document discusses thread synchronization, which is the process of ordering threads for execution, and outlines two main methods: Mutual Exclusion and Thread Communication. It details the Mutual Exclusion process, including synchronized blocks, methods, and static synchronization, with example code demonstrating their usage. Additionally, it covers Thread Communication methods such as wait(), notify(), and notifyAll(), providing a foundational understanding of managing thread execution in programming.

Uploaded by

kg666235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views10 pages

Class Running Notes 21th Nov

The document discusses thread synchronization, which is the process of ordering threads for execution, and outlines two main methods: Mutual Exclusion and Thread Communication. It details the Mutual Exclusion process, including synchronized blocks, methods, and static synchronization, with example code demonstrating their usage. Additionally, it covers Thread Communication methods such as wait(), notify(), and notifyAll(), providing a foundational understanding of managing thread execution in programming.

Uploaded by

kg666235
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Dt : 21/11/2022

*imp

Thread Synchronization:

=>The process of odering the threads for execution is known as Thread

Synchronization

=>Thread synchronization process can be performed in two ways:

i
thi
1.Mutual Exclusion process

2.Thread Communication process

ipa
1.Mutual Exclusion process:
Ma
=>The process of locking the programming resources and ordering the threads for

execution is known as Mutual Exclusion process.

(Programming Resources : Class,Object,Method)


sh
=>This Mutual Exclusion process can be performed in three ways:

(a)synchronized block - Object Locking process


ate

(b)synchronized method - Instance method Locking process

(c)static synchronization - Class Locking process


nk

(a)synchronized block:
Ve

=>The process of declaring some statements using "synchronized" keyword is

known as synchronized block.

=>we use synchronized block to lock the objects.

syntax:

synchronized(object_ref)
{

//statements

Ex-program :

i
thi
Printer.java

package test;
public class Printer {

ipa
public void print(int n,String uname) {
for(int i=1;i<=n;i++) {
System.out.println("Print out for User : "+uname);
try {
Thread.sleep(2000);

}
Ma
}catch(Exception e) {e.printStackTrace();}

}
}
sh
UserOne.java
ate

package test;
public class UserOne implements Runnable{
public Printer p=null;
public UserOne(Printer p) {
this.p=p;
nk

}
@Override
public void run() {
Ve

synchronized(p)
{
p.print(5, "RAM");
}
}
}

UserTwo.java
package test;
public class UserTwo implements Runnable{
public Printer p=null;
public UserTwo(Printer p) {
this.p=p;
}
@Override
public void run() {
synchronized(p)
{
p.print(5, "RAJ");

i
}

thi
}
}

ipa
DemoThread3.java(MainClass)

package maccess;
import test.*;
Ma
public class DemoThread3 {
public static void main(String[] args) {
Printer p = new Printer();

UserOne ob1 = new UserOne(p);


UserTwo ob2 = new UserTwo(p);
sh
Thread t1 = new Thread(ob1);
Thread t2 = new Thread(ob2);
ate

t1.start();
t2.start();
}
nk

o/p:
Ve

Print out for User : RAM

Print out for User : RAM

Print out for User : RAM

Print out for User : RAM

Print out for User : RAM


Print out for User : RAJ

Print out for User : RAJ

Print out for User : RAJ

Print out for User : RAJ

Print out for User : RAJ

i
thi
Diagram:

ipa
Ma
sh
========================================================================
ate

Limitation of Object Locking process:

=>In Object Locking process the total instance members available within the

Object,will be under the lock.


nk

========================================================================
Ve

(b)synchronized method:

=>The process of declaring Instance method with synchronized keyword is known

as synchronized method.

=>In this process,the Instance method will be under the lock and the method

can be used by one user at-a-time


syntax:

synchronized return_type method_name(para_list)

//method_body

Ex-program:

i
thi
Printer.java

package test;
public class Printer {

ipa
public synchronized void print(int n,String uname) {
for(int i=1;i<=n;i++) {
System.out.println("Print out for User : "+uname);
try {
Thread.sleep(2000);

}
Ma
}catch(Exception e) {e.printStackTrace();}

}
}
sh
UserOne.java
ate

package test;
public class UserOne implements Runnable{
public Printer p=null;
public UserOne(Printer p) {
this.p=p;
nk

}
@Override
public void run() {
Ve

p.print(5, "RAM");
}
}

UserTwo.java

package test;
public class UserTwo implements Runnable{
public Printer p=null;
public UserTwo(Printer p) {
this.p=p;
}
@Override
public void run() {
p.print(5, "RAJ");
}
}

DemoThread3.java(MainClass)

i
thi
package maccess;
import test.*;
public class DemoThread4 {
public static void main(String[] args) {

ipa
Printer p = new Printer();

UserOne ob1 = new UserOne(p);


MaUserTwo ob2 = new UserTwo(p);

Thread t1 = new Thread(ob1);


Thread t2 = new Thread(ob2);

t1.start();
t2.start();
sh
}
}
ate

o/p:

Print out for User : RAM


nk

Print out for User : RAM

Print out for User : RAM


Ve

Print out for User : RAM

Print out for User : RAM

Print out for User : RAJ

Print out for User : RAJ

Print out for User : RAJ


Print out for User : RAJ

Print out for User : RAJ

========================================================================

(c)static synchronization:

=>The process of declaring static method with "synchronized" keyword is

i
thi
known as static synchronization.

syntax:

ipa
synchronized static return_type method_name(para_list)

{
Ma
//method_body

=>In static synchronization process the lock is applied on class and all static
sh
members of class will be synchronized.(Class Locking process)
ate

Ex:

Printer.java
nk

package test;
public class Printer {
public synchronized static void print(int n,String uname) {
Ve

for(int i=1;i<=n;i++) {
System.out.println("Print out for User : "+uname);
try {
Thread.sleep(2000);
}catch(Exception e) {e.printStackTrace();}
}
}
}

UserOne.java
package test;
public class UserOne implements Runnable{
@Override
public void run() {

Printer.print(5, "RAM");

}
}

i
UserTwo.java

thi
package test;
public class UserTwo implements Runnable{
@Override

ipa
public void run() {
Printer.print(5, "RAJ");
}
} Ma
DemoThread3.java(MainClass)

package maccess;
import test.*;
sh
public class DemoThread5 {
public static void main(String[] args) {
ate

UserOne ob1 = new UserOne();


UserTwo ob2 = new UserTwo();

Thread t1 = new Thread(ob1);


nk

Thread t2 = new Thread(ob2);

t1.start();
Ve

t2.start();
}
}

o/p:

Print out for User : RAM

Print out for User : RAM


Print out for User : RAM

Print out for User : RAM

Print out for User : RAM

Print out for User : RAJ

Print out for User : RAJ

Print out for User : RAJ

i
thi
Print out for User : RAJ

Print out for User : RAJ

ipa
====================================================================

*imp
Ma
2.Thread Communication process:

=>The process of establishing Communication b/w threads using the following


sh
methods from java.lang.Object class is known as "Thread Communication process".

(a)wait()
ate

(b)notify()

(c)notifyAll()
nk

(a)wait():
Ve

=>wait() method is used to stop the thread execution temporarly until it

receives msg in the form of notify() or notifyAll()

Method Signature:

public final void wait() throws java.lang.InterruptedException;


(b)notify():

=>notify() method will execute the locked resource completedly and unlock the

resource,and send the msg to the next waiting thread.

Method Signature:

public final native void notify();

i
thi
(c)notifyAll():

=>notifyAll() method will execute the locked resource completedly and unlock the

ipa
resource,and send the msg to the next waiting multiple threads.

Method Signature:
Ma
public final native void notifyAll();

Ex:(Program to demonstrate Producer-Consumer problem)


sh
ate
nk
Ve

You might also like