♣
如何暂停并稍后继续一个线程的执行
如何暂停并稍后继续一个线程的执行?
比如:一个从1到10000的累加程序,我想让程序运行100ms时,暂停执行;
1100m后继续执行。
给思路的,介绍书目的都有分~
呵呵,给出解决代码的,那就不用说了:p
· 网友精彩回答:
class threadtest{
public static void main(string[] args){
long start = system.currenttimemillis(); //得到当前的毫秒时间
for (int i = 1;i<= 10000;i++){
if (system.currenttimemillis() - start == 100){
try{
thread.sleep(1100);
}catch(exception e){
e.printstacktrace();
system.exit(0);
}
}
system.out.println(i);
}
}
}
知道为什么吗?
因为程序运行时间不是像你认为的每个毫秒都会到你的if里面判断
事实上是system.currenttimemillis() - start == 100几乎不可能
比如说,当i=50时,system.currenttimemillis() - start =99,
而当i=51时,这个值是101,这个100是到不了的
假如线程t1在下载
那么button pause addactionlistener完成t1.wait();
在button resume 的addactionlistener完成t1.notify();
改用wait()和notify(),功能没变
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ttext
extends thread {
private jtextfield t = new jtextfield(15);
private jbutton bp = new jbutton("pause");
private int count = 0;
private boolean startf = false, pus = false;
public ttext(container c) {
c.add(t);
bp.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
pus = true;
}
});
c.add(bp);
new tnotify(c, this);
start();
}
public void startt() {
startf = true;
}
public synchronized void run() { //注意synchronized,不然会产生
//异常illegalmonitorstateexception
while (true) {
if (startf) {
t.settext(integer.tostring(count++));
}
try {
if(pus) {
wait(); //改用wait();
pus = !pus;
}
sleep(100);
}
catch (interruptedexception e) {
system.err.println("interrupted");
}
}
}
}
class tnotify
extends thread {
private ttext t;
private boolean startf;
private jbutton br = new jbutton("resume");
public tnotify(container c, ttext t) {
this.t = t;
br.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
startt();
}
});
c.add(br);
start();
}
public void startt() {
startf = true;
}
public void run() {
while (true) {
if (startf) {
synchronized(t) { //注意synchronized,不然会产生
//异常illegalmonitorstateexception
t.notify();
}
startf = !startf;
}
try {
sleep(100);
}
catch (interruptedexception e) {
system.err.println("interrupted");
}
}
}
}
public class threadt
extends jframe {
private jbutton
bs = new jbutton("start");
private jpanel p = new jpanel();
private ttext t;
private tnotify tn;
public threadt() throws headlessexception {
super("test");
this.setsize(300, 200);
container cp = getcontentpane();
cp.setlayout(new flowlayout());
t = new ttext(cp);
bs.addactionlistener(new actionlistener() {
public void actionperformed(actionevent e) {
t.startt();
}
});
cp.add(bs);
this.setvisible(true);
this.setdefaultcloseoperation(jframe.exit_on_close);
}
public static void main(string[] args) throws headlessexception {
threadt threadt1 = new threadt();
}
}
- 更多问题:
- · 最简单的查询.dbf数据库中对应的记录的方法是什么?
- · 不执行 Form1_Load 过程的问题
- · 悬红:如果您是TAPI或者电话编程高手,请以您的工作成果来换取您的报酬
- · 求贤,工作地点北京
- · 看完了C#入门经典和C#技术揭秘,可不可以看一些.Net构架方面的书了
- · 请问神笔电脑书法大师,是用Authorware做的吗?
- · 大家注意了,这是我对同一个问题连开的第三贴!1
- · vc6调试时没有了响应?
- · 如何将字符串转换成控件类型
- · 求救呀上传文件太大后,页面出错如何做处理?
- · 请教有关两个表的连接操作
- · 肉食之害--请看完再评论
- · 没有人能给我详细的满意答复,这是我开的第二贴!!!
- · destructor莫名其妙出错
- · MSSQL2000 安装时出现的问题,急救
- · 新手提问.
- · 进程技术详解
- · apache专题 | apache
- · fso专题
- · ie浏览器专题
- · 漏洞与补丁 | 漏洞
- · Fireworks专题
- · sco unix
- · unix介绍
- · unix ftp
- · 网管工作
- · fat ntfs
- · ipc 入侵
- · 网卡mac地址
- · 注册表编辑
- · 注册表还原
- · 微内核
- · 使用汇编写进注册表启动的简单例子
- · 禁用注册表有什么用处
- · 注册表在哪
- · 电脑注册表损坏时出现的十种症状
- · 怎样修改注册表
- · 如何恢复注册表啊
- · 谁有注册表恢复工具?
- · 如何修改注册表
- · firefox浏览器

