问题解决了,这种写法可行!
public static void main(String[] args) throws InterruptedException {
ByBus b = new ByBus();
Thread t1 = new Thread(b, "线程一");
Thread t2 = new Thread(b, "线程二");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("执行完毕");
}
}
class ByBus implements Runnable {
public static int persons = 70;
public void run() {
while (true) {
synchronized (ByBus.class) {
if(persons<=0)
break;
System.out.println(Thread.currentThread().getName() +-- persons);
}
}
} |