Skip to content
This repository was archived by the owner on Nov 5, 2023. It is now read-only.

Latest commit

 

History

History
175 lines (149 loc) · 3.56 KB

File metadata and controls

175 lines (149 loc) · 3.56 KB

Notes (October 10,2022)

Topics

  • Thread Synchronization

Thread Synchronization

  • Used for Shared resources.
  • Race condition- More than one thread try to catch the CPU.
    1. Synchronized Block
    2. Synchronized Method
  • Keyword: Synchronized

Synchronized Function Syntax

sychronized public void function(){ //before access specifier
}
synchronized(share_resource){
}

Programs

Program 1a:-

class Print{
    void callPrint(String msg){
        System.out.print("["+msg);
        System.out.println("]");
    }
}

//THREAD
class TestSyn implements Runnable{
        Thread t;
        Print target;
        String msg;
        TestSyn(Print target, String msg){
                this.target=target;
                this.msg=msg;
                t=new Thread(this);
        }     
        public void run(){
            target.callPrint(msg);
        }
}

class TestSynDemo{
        public static void main(String args[]){
            Print target=new Print();//Share resource
            TestSyn t1=new TestSyn(target,"Welcome");
            TestSyn t2=new TestSyn(target,"to");
            TestSyn t3=new TestSyn(target,"University");
            t1.t.start();
            t2.t.start();
            t3.t.start();
        }
}
  • Output:-
[user]$ java TestSynDemo
[Welcome]
[to]
[University]

Program 1b:-

class Print{
    synchronized void callPrint(String msg){
        System.out.print("["+msg);
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e){}
        System.out.println("]");
    }
}

//THREAD
class TestSyn implements Runnable{
        Thread t;
        Print target;
        String msg;
        TestSyn(Print target, String msg){
                this.target=target;
                this.msg=msg;
                t=new Thread(this);
        }     
        public void run(){
            target.callPrint(msg);
        }
}

class TestSynDemo{
        public static void main(String args[]){
            Print target=new Print();//Share resource
            TestSyn t1=new TestSyn(target,"Welcome");
            TestSyn t2=new TestSyn(target,"to");
            TestSyn t3=new TestSyn(target,"University");
            t1.t.start();
            t2.t.start();
            t3.t.start();
        }
}
  • Output:-
[user]$ java TestSynDemo
[Welcome]
[University]
[to]
  • NOTE: The Output isn't displayed in a correct order because it depends on OS, CPU and the method of execution.

Program 1c:-

class Print{
    synchronized void callPrint(String msg){
        System.out.print("["+msg);
        try{
            Thread.sleep(1000);
        }catch(InterruptedException e){}
        System.out.println("]");
    }
}

//THREAD
class TestSyn implements Runnable{
        Thread t;
        Print target;
        String msg;
        TestSyn(Print target, Sdharunkrishna@dharun-fedora-laptop Oct_10_classtring msg){
                this.target=target;
                this.msg=msg;
                t=new Thread(this);
        }     
        public void run(){
            synchronized(target){
                target.callPrint(msg);
            }
        }
}

class TestSynDemo{
        public static void main(String args[]){
            Print target=new Print();//Share resource
            TestSyn t1=new TestSyn(target,"Welcome");
            TestSyn t2=new TestSyn(target,"to");
            TestSyn t3=new TestSyn(target,"University");
            t1.t.start();
            t2.t.start();
            t3.t.start();
        }
}
  • Output:-
[user]$ java TestSynDemo
[Welcome]
[University]
[to]