class Teacher implements Runnable {
private int notes = 80;
public void run() {
while (true) {
if (notes > 0) {
System.out.println(Thread.currentThread().getName()+" "+notes--+" ");
}
}
}
}
public class Example12 {
public static void main(String[] args) {
Teacher t = new Teacher();
new Thread(t,"teacher1").start();
new Thread(t,"teacher2").start();
new Thread(t,"teacher3").start();
}
}
这样可能会造成线程安全问题,加锁即可解决多线程问题