Class Running Notes 21th Nov
Class Running Notes 21th Nov
*imp
Thread Synchronization:
Synchronization
i
thi
1.Mutual Exclusion process
ipa
1.Mutual Exclusion process:
Ma
=>The process of locking the programming resources and ordering the threads for
(a)synchronized block:
Ve
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();
t1.start();
t2.start();
}
nk
o/p:
Ve
i
thi
Diagram:
ipa
Ma
sh
========================================================================
ate
=>In Object Locking process the total instance members available within the
========================================================================
Ve
(b)synchronized method:
as synchronized method.
=>In this process,the Instance method will be under the lock and the method
//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();
t1.start();
t2.start();
sh
}
}
ate
o/p:
========================================================================
(c)static synchronization:
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
t1.start();
Ve
t2.start();
}
}
o/p:
i
thi
Print out for User : RAJ
ipa
====================================================================
*imp
Ma
2.Thread Communication process:
(a)wait()
ate
(b)notify()
(c)notifyAll()
nk
(a)wait():
Ve
Method Signature:
=>notify() method will execute the locked resource completedly and unlock the
Method Signature:
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();