java併發編程學習之synchronize(二)

synchronized的應用方式

  1. 代碼塊:做用範圍在{}中,做用對象是調用這個代碼塊的對象。
  2. 方法:做用範圍是一個方法,做用對象是調用這個方法的對象。
  3. 靜態方法:做用範圍是這個靜態方法,做用對象是這個類的全部對象。

1,2是對象鎖,3是類鎖ide

舉例

代碼塊

無this

public class SynchronizeDemo1 {
    static String syn = new String();

    static class SynClass {
        public void myRun() {
            try {
                System.out.println(Thread.currentThread().getName() + "進來了");
                synchronized (syn) {
                    Thread.sleep(3000);
                }
                System.out.println(Thread.currentThread().getName() + "出來了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class Runnable1 implements Runnable {
        SynClass synClass;

        public Runnable1(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun();
        }
    }

    static class Runnable2 implements Runnable {
        SynClass synClass;

        public Runnable2(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun();
        }
    }

    public static void main(String[] args) {
        SynClass synClass = new SynClass();
        Runnable1 runnable1 = new Runnable1(synClass);
        Runnable2 runnable2 = new Runnable2(synClass);
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

運行的結果以下:this

clipboard.png

等thread1把代碼塊的執行完,釋放了syn的鎖,thread2纔開始執行。spa

有this

public class SynchronizeDemo2 {
    static String syn = new String();

    static class SynClass {
        public void myRun() {
            try {
                System.out.println(Thread.currentThread().getName() + "-myRun");
                synchronized (this) {
                    Thread.sleep(3000);
                }
                System.out.println(Thread.currentThread().getName() + "-myRun");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public void myRun2() {
            try {
                System.out.println(Thread.currentThread().getName() + "-myRun2");
                synchronized (this) {
                    Thread.sleep(3000);
                }
                System.out.println(Thread.currentThread().getName() + "-myRun2");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class Runnable1 implements Runnable {
        SynClass synClass;

        public Runnable1(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun();
        }
    }

    static class Runnable2 implements Runnable {
        SynClass synClass;

        public Runnable2(SynClass synClass) {
            this.synClass = synClass;
        }

        @Override
        public void run() {
            synClass.myRun2();
        }
    }

    public static void main(String[] args) {
        SynClass synClass = new SynClass();
        Runnable1 runnable1 = new Runnable1(synClass);
        Runnable2 runnable2 = new Runnable2(synClass);
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

運行的結果以下:code

clipboard.png
等thread1把代碼塊的執行完,釋放了this的鎖,thread2纔開始執行。對象

方法

public class SynchronizeDemo3 extends Thread {
    @Override
    public void run() {
        sync();
    }

    synchronized public void sync(){
        System.out.println(Thread.currentThread().getName() + "進來了");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "出來了");
    }

    public static void main(String[] args) {
        SynchronizeDemo3 synchronizeDemo1 = new SynchronizeDemo3();
        Thread thread1 = new Thread(synchronizeDemo1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(synchronizeDemo1);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

運行的結果以下:ip

clipboard.png

等thread1把方法執行完,釋放了的鎖,thread2纔開始執行。get

靜態方法

public class SynchronizeDemo4 {
    static class Runnable1 implements Runnable {
        @Override
        public void run() {
            SynClass.myRun();
        }
    }

    static class Runnable2 implements Runnable {
        @Override
        public void run() {
            SynClass.myRun2();
        }
    }

    public static void main(String[] args) {
        Runnable1 runnable1 = new Runnable1();
        Runnable2 runnable2 = new Runnable2();
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

class SynClass {
    public synchronized static void myRun() {
        System.out.println(Thread.currentThread().getName() + "-myRun");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-myRun");
    }

    public synchronized static void myRun2() {
        System.out.println(Thread.currentThread().getName() + "-myRun2");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "-myRun2");
    }
}

運行的結果以下:it

clipboard.png

thread1等待thread2執行完才執行,說明是類鎖io

類所的另一種形式

public class SynchronizeDemo5 {
    static class Runnable1 implements Runnable {
        @Override
        public void run() {
            synchronized (SynClass2.class){
                System.out.println(Thread.currentThread().getName() + "-myRun");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "-myRun");
            }
        }
    }

    static class Runnable2 implements Runnable {
        @Override
        public void run() {
            synchronized (SynClass2.class){
                System.out.println(Thread.currentThread().getName() + "-myRun2");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "-myRun2");
            }
        }
    }

    public static void main(String[] args) {
        Runnable1 runnable1 = new Runnable1();
        Runnable2 runnable2 = new Runnable2();
        Thread thread1 = new Thread(runnable1);
        thread1.setName("thread1");
        Thread thread2 = new Thread(runnable2);
        thread2.setName("thread2");
        thread1.start();
        thread2.start();
    }
}

class SynClass2 {
}

運行結果以下:class

clipboard.png

相關文章
相關標籤/搜索