乐趣区

java并发编程学习之synchronize二

synchronized 的应用方式

  1. 代码块:作用范围在{}中,作用对象是调用这个代码块的对象。
  2. 方法:作用范围是一个方法,作用对象是调用这个方法的对象。
  3. 静态方法:作用范围是这个静态方法,作用对象是这个类的所有对象。

1,2 是对象锁,3 是类锁

举例

代码块

无 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();}
}

运行的结果如下:

等 thread1 把代码块的执行完,释放了 syn 的锁,thread2 才开始执行。

有 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();}
}

运行的结果如下:


等 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();}
}

运行的结果如下:

等 thread1 把方法执行完,释放了的锁,thread2 才开始执行。

静态方法

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");
    }
}

运行的结果如下:

thread1 等待 thread2 执行完才执行,说明是类锁

类所的另外一种形式

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 {}

运行结果如下:

退出移动版