synchronized对象解析
package com.haiyisoft.hyoaPc;
public class Test7 {
    public static void main(String[] args) throws InterruptedException {
        Stu stu1 = new Stu(1);
        Thread t1 = new Thread(new MyThread(stu1));
        Thread t2 = new Thread(new MyThread(stu1));
        t1.start();
        t2.start();
    }
}
class MyThread implements Runnable{
    public MyThread(Stu stu){
        this.stu = stu;
    }
    private Stu stu;
    @Override
    public void run() {
        try {
            stu.a();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Stu{
    private Integer count ;
    public  Integer getCount(){
        return this.count;
    }
    public Stu(Integer count){
        this.count = count;
    }
    public  void a() throws InterruptedException {
        synchronized(count) {
            count++;
            
           
                
                System.out.println(count);
          
        }
    }
}sync

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号