public class Six {
public static void main(String[] args){
//一份资源
Tickets tickets = new Tickets();
new Thread(tickets, "李现").start();
new Thread(tickets, "刘昊然").start();
}
}
class Tickets implements Runnable {
private Integer t = 0;
@Override
public void run() {
while (true) {
synchronized (t) {
if (t == 100) {
break;
}
t++;
}
System.out.println(Thread.currentThread().getName() + "-->" + t);
}
}
}
多线程对一个变量的自增
最新推荐文章于 2023-09-04 17:59:46 发布