Sychnized

3445 ワード

Javaの各オブジェクトはロックとして機能します。
  • 同期方法に対して、ロックは現在の例示的なオブジェクトである。
  • 静的同期方法に対して、ロックは現在のオブジェクトのクラスオブジェクトである。
  • 同期方法ブロックに対して、ロックはSynchnized括弧に配置されたオブジェクトです。
  • /**
     * 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");
        }
    
    }
    結果:二つのテストクラスが競争します。
    Syncel錠、
    一つの方法体しか実行できません。