首页 > 其他 > 详细

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

时间:2020-03-05 19:07:24      阅读:66      评论:0      收藏:0      [点我收藏+]
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);
    }

}

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!