美文网首页
synchronize

synchronize

作者: 博瑜 | 来源:发表于2017-07-22 18:34 被阅读0次
    package threadimp;
    
    public class MySync {
    public static void main(String[] args) {
    final MySync sync1 = new MySync(); 
    final MySync sync2 = new MySync(); 
    new Thread("thread1") {
        @Override
        public void run() {
            synchronized (sync1) {
                System.out.println(this.getName() +  " start");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(this.getName() +  " wake up");
            }
        }
    }.start();
    
    new Thread("thread2") {
        @Override
        public void run() {
            synchronized (sync1) {
                System.out.println(this.getName() +  " start");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println(this.getName() +  " wake up");
            }
        }
    }.start();
    }
    }

    相关文章

      网友评论

          本文标题:synchronize

          本文链接:https://www.haomeiwen.com/subject/yoiukxtx.html