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

posted @ 2019-03-29 15:12  逆水乘舟,不进则退  阅读(139)  评论(0编辑  收藏  举报