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

相关文章:

  • 2021-07-01
  • 2022-12-23
  • 2021-09-04
  • 2021-07-03
  • 2021-05-24
  • 2022-12-23
  • 2021-05-04
  • 2021-10-07
猜你喜欢
  • 2021-08-31
  • 2021-11-19
  • 2021-11-24
  • 2021-08-09
  • 2022-02-15
  • 2022-01-03
  • 2022-12-23
相关资源
相似解决方案