测试未用synchronized修饰的方法对已加锁的对象的操作能否成功

时间:2020-03-05 19:07:24   收藏:0   阅读:66
public class TestSync implements Runnable{

    int num = 100;
    
    public static void main(String[] args){
        TestSync syn = new TestSync();
        Thread t = new Thread(syn);
        t.start();
        try{
            Thread.sleep(1000);
        } catch(InterruptedException e){
            e.printStackTrace();
        }
        syn.num = 500;
        System.out.println(syn.num);
     
    }
 
    public synchronized void run(){
        
        num = 10000;
        
         try{
            Thread.sleep(5000);
        } catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("num:" + num);
    }

}

 

原文:https://www.cnblogs.com/yxfyg/p/12421692.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!