如何暂停并稍后继续一个线程的执行?
比如:一个从1到10000的累加程序,我想让程序运行100ms时,暂停执行;
1100m后继续执行。
给思路的,介绍书目的都有分~
呵呵,给出解决代码的,那就不用说了:P
比如:一个从1到10000的累加程序,我想让程序运行100ms时,暂停执行;
1100m后继续执行。
给思路的,介绍书目的都有分~
呵呵,给出解决代码的,那就不用说了:P
解决方案 »
- [?] 如何获得当前执行的文件名
- 如何将通过socket读入的DataInputStream转换为Document类型来使用?
- java链接SqlServer2000数据库的链接文怎么写啊。高人指教!
- System.out.println("Hello World");搜集这句的完整解释
- 大家来帮我看下这个有线程的程序,有何不妥之处?
- 请教一下这个提示是什么意思?
- java中可以调用.exe文件么?谢谢
- JTable中如何自动调整column的大小
- 关于JNI的问题?
- 问:如何部署WEB应用程序?
- String s = "C" + ":\"; 如何实现 aa:\
- File 绝对高手请进 -> 除了File构造函数外, 有什么方法能将 一个File 放入另一个File中吗?
推荐thinking in java
和Core Java 2
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);
}
}
}
事实上是System.currentTimeMillis() - start == 100几乎不可能比如说,当i=50时,System.currentTimeMillis() - start =99,
而当i=51时,这个值是101,这个100是到不了的
//程序修改smlovetp(IT菜鸟) 的
class ThreadTest{
public static void main(String[] args){
long start = System.currentTimeMillis(); //得到当前的毫秒时间
boolean sleep = true;
for (int i = 1;i<= 10000;i++){
if (sleep && (System.currentTimeMillis() - start >= 100)){
try{
Thread.sleep(1000);
sleep = false;
}catch(Exception e){
e.printStackTrace();
System.exit(0);
}
} System.out.println(i);
}
}
}
在一个线程里让程序固定运行100ms那可以行得通吗??
就是这个意思
那么button pause addactionlistener完成t1.wait();
在button resume 的addactionlistener完成t1.notify();
以下是我的代码,但是,不知为什么没有产生效果。
[code]
# import java.util.*;
#
# class CanPause extends Thread {
# // 从0累加到10000
# private int counter = 0;
# public void run() {
# while(counter < 10000) {
# System.out.print(counter++ + " ");
# }
# }
# }
#
# public class Pausing {
# public static void main(String[] args) {
# final CanPause pauseable = new CanPause();
# pauseable.start();
# //100ms 后,暂停程序执行
# new Timer(true).schedule(new TimerTask() {
# public void run() {
# System.out.println("start waiting----------------\n");
# synchronized(CanPause.class) {
# try {
# pauseable.wait();
# } catch(Exception e ){}
# }
# }
# }, 100);
# // 400ms后,恢复执行
# new Timer(true).schedule(new TimerTask() {
# public void run() {
# System.out.println("start notifying--------------\n");
# synchronized(CanPause.class) {
# pauseable.notifyAll();
# }
# }
# }, 500);
# }
# }
# [/code]
# try {
# pauseable.wait();
~~~~~~~~~~~~~~~wait和notify方法只能由current thread调用,这个地方必然产生异常IllegalMonitorStateException
# } catch(Exception e ){} //怎么没把异常打印出来 :)
# }如果需要由外部暂停线程,并不赞同使用suspend()方法!可以通过设置标记;时间的精度控制很困难 :)
java.util.timer.*
中的类来控制thread的行为。
这个类就是用来控制代码定时启动的。给你一点我的代码,希望对你有所帮助。
/*
restime就是你所需要的间隔时间,这个方法一旦被调用,将在restime/1000秒之后执行run()中的代码,你只需要很少的工作就可以使他重复的执行。还是一句话,多看看api
*/
public void AllStart() {
timer.schedule(new TimerTask() {
public void run() {
//dosomething }
}, restime);
System.out.println("下次运行在"+(restime/1000)+"秒后");
}
因为他们容易造成死锁,不过可以在synchronized函数中调用wait()和notify()
就不会产生异常IllegalMonitorStateException
呵呵,不过是理论的东东,我操作起来不熟,所以还是使用suspend()和resume()方法
cozmic (蓝色的猪) :
我把你的程序改成了这样:
import java.util.*;class CanPause
extends Thread {
// 从0累加到10000
private int counter = 0;
public void run() {
while (counter < 10000) {
System.out.print(counter++ +" ");
try {
sleep(100);
}catch(InterruptedException e) {
System.err.println("Interrupted");
}
}
}
}public class Pausing {
public static void main(String[] args) {
final CanPause pauseable = new CanPause();
pauseable.start();
//100ms 后,暂停程序执行
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("start waiting----------------\n");
pauseable.suspend();
}
}, 100);
// 2500ms后,恢复执行
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("start notifying--------------\n");
pauseable.resume();
}
}, 2500);
}
}
运行没问题的
和你的功能差不多的
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;
public TText(Container c) {
c.add(t);
bp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
suspend();
}
});
c.add(bp);
new TNotify(c, this);
start();
} public void StartT() {
startf = true;
} public void run() {
while (true) {
if (startf) {
t.setText(Integer.toString(count++));
}
try {
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) {
t.resume();
startf = !startf;
}
try {
sleep(100);
}
catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
}
}public class ThreadT
extends JFrame {
private JButton
bs = new JButton("start"),
bp = new JButton("pause"),
br = new JButton("resume");
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();
}}
而TNotify是一个唤醒线程,由TText调用
主线程用来启动TText
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();
}
}
谢谢:P
但是还想问一点:为什么在我的代码中,用wait() 和 notifyAll() 就是不可以呢?
-------------------------------------------
E:\java\Thread>java Pausing
0 1 2 3 4 5 6 7 8 9
start waiting------------------------------------(*没有任何等待就打印出10)10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
74
start notifying------------------------------------(没有任何等待就打印出以下)Exception in thread "Timer-1" java.lang.IllegalMonitorStateException: current
read not owner
at java.lang.Object.notifyAll(Native Method)
at Pausing1$2.run(Pausing1.java:41)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100 101 102 (*Ctrl+c结束)
为什么,线程并没有等待?是不是因为没有等待,导致后来的notifyALl()没有获得Canpause.class
的锁?
--------------------
import java.util.*;class CanPause extends Thread {
// 从0累加到10000
private int counter = 0;
public void run() {
while(counter < 10000) {
System.out.print(counter++ + " ");
//暂停100ms(仿照mooninsun的:P)
try {
Thread.sleep(100);
} catch(Exception e ){}
}
}
}public class Pausing1 {
public static void main(String[] args) {
final Boolean semo = new Boolean(true);
final CanPause pauseable = new CanPause();
pauseable.start();
//1s 后,暂停程序执行
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("\nstart waiting------------------------------------\n");
synchronized(CanPause.class) {
try {
pauseable.wait();
} catch(Exception e ){}
}
}
}, 1000);
try {
Thread.sleep(2000);
} catch(Exception e ){}
// 5.5s后,恢复执行
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("\nstart notifying------------------------------------\n");
synchronized(CanPause.class) {
pauseable.notifyAll();
}
}
}, 5500);
}
}
这说明对于pauseable.wait()并没有synchronized也就是上锁
所以pauseable.wait()并没有起任何作用,后面的notifyALl()就更不用说了
我帮你改成了下面的程序,你看看:
import java.util.*;class CanPause
extends Thread {
// 从0累加到10000
private int counter = 0;
public boolean pf = false;
public synchronized void run() { //注意这里用了synchronized,因为要调用wait()
while (counter < 10000) {
System.out.print(counter++ +" ");
try {
sleep(100);
if(pf) {
wait();
pf = !pf;
}
}catch(InterruptedException e) {
System.err.println("Interrupted");
}
}
}
}public class Pausing {
public static void main(String[] args) {
final CanPause pauseable = new CanPause();
pauseable.start();
//100ms 后,暂停程序执行
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("start waiting----------------\n");
pauseable.pf = true;
}
}, 100);
// 2500ms后,恢复执行
new Timer(true).schedule(new TimerTask() {
public void run() {
System.out.println("start notifying--------------\n");
synchronized (pauseable) { //这里我把CanPause.class改成了pauseable
pauseable.notify();
}
}
}, 2500);
}
}