import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
class MyThread extends Thread {
private JLabel label;
private long time;
private long endtime;
public MyThread(JLabel label, long time) {
this.label = label;
this.time = time;
}
public void init() {
endtime = System.currentTimeMillis() + time;
}
@Override
public void run() {
init();
while (true) {
long runtime = endtime - System.currentTimeMillis();
time = runtime;
if(runtime > 0){
label.setText(formatTime(runtime));
}else{
label.setText("GAME OVER");
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private String formatTime(long runtime) {
int m = (int) (runtime / 1000 / 60);
int s = (int) (runtime / 1000 % 60);
int ss = (int) (runtime % 1000);
return check(m) + ":" + check(s) + "." + check(ss);
}
private String check(int number) {
return number > 9 ? "" + number : "0" + number;
}
}
class ClockFrame extends JFrame {
private JLabel label;
private JButton button;
private MyThread mt;
private boolean bFlag = true;// 控制按钮的显示及线程的挂起恢复
public ClockFrame() {
label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
add(label, BorderLayout.CENTER);
button = new JButton("暂停");
add(button, BorderLayout.NORTH);
setBounds(222, 222, 222, 222);
setVisible(true);
mt = new MyThread(label, 10000L);
mt.start();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (bFlag) {
button.setText("继续");
mt.suspend();
} else {
button.setText("暂停");
mt.init();
mt.resume();
}
bFlag = !bFlag;
}
});
}
}
public class ClockTest {
public static void main(String[] args) {
new ClockFrame();
}
}
以上是一个多线程的码表程序,运行起来没有问题,但是用到了两个过时的方法,suspend,suspend,想请问如果用wait, notify这两个方法,应该怎么改这个程序,谢谢
分享到:
相关推荐
Java IO流学习总结 Java流操作有关的类或接口: Java流类图结构: ...流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。... 可以用于多线程下载或多个线程同时写数据到文件。
多线程事件引擎 四大功能 股票数据 选股 策略回测 实盘交易 历史数据均免费来自于网络 Wind免费个人接口 TuShare 实盘微信提醒及交互 一键挂机 全自动交易 模拟交易,支持9个模拟账号 实盘和回测共用同一策略代码 实...
1.3.1 Windows Sockets是一个开放的标准 1.3.2 Windows Sockets提供源代码可移植性 1.3.3 Windows Sockets支持动态链接 1.3.4 Windows Sockets的优点 1.4 Windows Sockets的前景 1.5 结论 第2章 Windows Sockets的...
1.3.1 Windows Sockets是一个开放的标准 1.3.2 Windows Sockets提供源代码可移植性 1.3.3 Windows Sockets支持动态链接 1.3.4 Windows Sockets的优点 1.4 Windows Sockets的前景 1.5 结论 第2章 Windows Sockets的...
第2章 多线程串口编程工具cserialport类 16 2.1 cserialport类的功能及成员函数介绍 16 2.2 应用cserialport类编制基于对话框的应用程序 30 2.3 应用cserialport类编制基于单文档的应用程序 35 2.4 对...
4.3.2 一个文档对象对应多个视对象 59 4.3.3 一个文档对应多个视类型 59 4.4 窗口框架 59 4.5 文档模板 60 4.6 建立标准单文档程序框架 61 4.6.1 新建项目 61 4.6.2 修改项目选项 62 4.6.3 完成向导 64 4.6.4 运行...
第2章 多线程串口编程工具cserialport类 16 2.1 cserialport类的功能及成员函数介绍 16 2.2 应用cserialport类编制基于对话框的应用程序 30 2.3 应用cserialport类编制基于单文档的应用程序 35 2.4 对...
第2章 多线程串口编程工具cserialport类 16 2.1 cserialport类的功能及成员函数介绍 16 2.2 应用cserialport类编制基于对话框的应用程序 30 2.3 应用cserialport类编制基于单文档的应用程序 35 2.4 对...
第2章 多线程串口编程工具CSerialPort类 16 2.1 CSerialPort类的功能及成员函数介绍 16 2.2 应用CSerialPort类编制基于对话框的应用程序 30 2.3 应用CSerialPort类编制基于单文档的应用程序 35 2.4 对CSerialPort类...
第2章 多线程串口编程工具CSerialPort类 16 2.1 CSerialPort类的功能及成员函数介绍 16 2.2 应用CSerialPort类编制基于对话框的应用程序 30 2.3 应用CSerialPort类编制基于单文档的应用程序 35 2.4 对CSerialPort...
zico矢量图标系统(http://ico.z01.com )、逐浪CMS Github平台(http://github.z01.com/ )、逐浪字库(http://f.ziti163.com )、UNI国际字码表(http://www.ziti163.com/uni )、Bootstrap V4中国站...