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

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);
    }

}

 

posted @ 2020-03-05 18:07  yxfyg  阅读(100)  评论(0)    收藏  举报