Synchonized


Java中的每一个对象都可以作为锁.

  1. 对于同步方法,锁是当前实例对象.
  2. 对于静态同步方法,锁是当前对象的Class对象.
  3. 对于同步方法块,锁是Synchonized括号里配置的对象.
/**
 * User: caiyuan
 * Date: 13-1-31
 */
public class SynSelf {

    public void sm1() {
        synchronized (this) {
            while (true) {
                System.out.println("sm1");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public synchronized void sm2() {
        while (true) {
            System.out.println("sm2");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void sm3() {
        while (true) {
            System.out.println("sm3");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        final SynSelf syn = new SynSelf();

        new Thread(new Runnable() {
            public void run() {
                syn.sm1();
            }
        }, "sm1").start();

        new Thread(new Runnable() {
            public void run() {
                syn.sm2();
            }
        }, "sm2").start();

        new Thread(new Runnable() {
            public void run() {
                syn.sm3();
            }
        }, "sm3").start();

        System.out.println("start");
    }

}
/**
 * User: caiyuan
 * Date: 13-1-31
 */
public class SynOther {

    public void om1(SynSelf o) {
        synchronized (o) {
            while (true) {
                System.out.println("om1");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void om2(SynSelf o) {
        synchronized (o) {
            while (true) {
                System.out.println("om2");
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        final SynSelf o = new SynSelf();
        final SynOther syn = new SynOther();

        new Thread(new Runnable() {
            public void run() {
                syn.om1(o);
            }
        }, "om1").start();

        new Thread(new Runnable() {
            public void run() {
                syn.om2(o);
            }
        }, "om2").start();

        new Thread(new Runnable() {
            public void run() {
                o.sm1();
            }
        }, "sm1").start();

        new Thread(new Runnable() {
            public void run() {
                o.sm2();
            }
        }, "sm2").start();

        new Thread(new Runnable() {
            public void run() {
                o.sm3();
            }
        }, "sm3").start();

        System.out.println("start");
    }

}
结果:两个测试类都竞争 SynSelf 锁, 执行一个方法体



你可能感兴趣的:(Synchonized)