packagecom.example.Thread;import javax.swing.*;import java.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.HashMap;importjava.util.Map;/*** Created by 达达队长 on 2018/4/28.*/
class Count extendsThread {private volatile int stop = 1;public volatile int i = 0;privateJLabel count;publicCount(JLabel count) {this.count =count;
}
@Overridepublic voidrun() {try{synchronized (this) {while (true) {if (stop == 0) {
wait();
}else if (stop == 1) {
count.setText(i++ + "");
}else if (stop == 2) {return;
}
}
}
}catch(InterruptedException e) {
e.printStackTrace();
}
}public voidsetStop() {
stop= 0;
}public synchronized voidsetStart() {
stop= 1;
notify();