问个关于线程 暂停 继续的问题,希望有代码示例,谢谢。

jyf7356759 2008-03-07 05:25:38
想要个示例代码,键盘按p就线程暂停,按r就继续执行,按s停止。
谢谢了。
...全文
140 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
zapdos 2008-03-08
  • 打赏
  • 举报
回复
是么?那真是很奇怪
jyf7356759 2008-03-08
  • 打赏
  • 举报
回复
好的,先结了这个。
老紫竹 2008-03-08
  • 打赏
  • 举报
回复
如果异常在catch范围内,程序能正常切换,那么就是可控异常,不用管它!
jyf7356759 2008-03-08
  • 打赏
  • 举报
回复
暂停后继续的时候会有个异常...什么情况啊。
jyf7356759 2008-03-08
  • 打赏
  • 举报
回复
谢谢老竹竹先,我去运行下看看。
不过怎么程序有错,运行不了呢?第一行import报错?这种是什么错误啊?
老紫竹 2008-03-08
  • 打赏
  • 举报
回复
FROM:http://www.java2000.net/viewthread.jsp?tid=1735
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
public static void main(String[] args) throws IOException, InterruptedException {
TestThread t = new TestThread();
t.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (true) {
line = reader.readLine();
System.out.println("line=" + line);
if ("p".equalsIgnoreCase(line)) {
t.pause = true;
} else {
t.pause = false;
synchronized (t) {
t.notifyAll();
}
}
}
}
}

class TestThread extends Thread {
boolean pause;

public void run() {
while (true) {
try {
if (pause) {
synchronized (this) {
wait();
}
}
System.out.println("I am working ...");
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
jyf7356759 2008-03-08
  • 打赏
  • 举报
回复
Java_2000老竹竹和其他版主帮帮我啊
jyf7356759 2008-03-08
  • 打赏
  • 举报
回复
能帮我看看这段代码为什么不能控制暂停和继续么?

public class testThread {

public static void main(String[] args) throws InterruptedException {

A a = new A();
a.start();
synchronized(a){
a.wait(10000);
}
synchronized(a){
a.notifyAll();
}
}
}
class A extends Thread{

public void run(){

try {
for(int i=0; i<10; i++)
{
System.out.println(" do ......" + i);
}
} catch (Exception dx) {
dx.printStackTrace();
}
}

}
jyf7356759 2008-03-08
  • 打赏
  • 举报
回复
哦,谢谢了,能给个小例子讲解下么?最简单的。如果我还是运行不了那就结了这个我重发了
zapdos 2008-03-08
  • 打赏
  • 举报
回复
就是先做一把锁,然后用wait();和notify();来控制就行了
zapdos 2008-03-08
  • 打赏
  • 举报
回复
就是先做一把锁,然后用wait();和notify();来控制就行了
jyf7356759 2008-03-08
  • 打赏
  • 举报
回复
那我能再请问下thread是怎么让它暂停和继续的么?
jyf7356759 2008-03-07
  • 打赏
  • 举报
回复
那很奇怪啊,我运行就什么都没有output.
zapdos 2008-03-07
  • 打赏
  • 举报
回复
不行?我这边可以运行啊
C:\java>javac test.java

C:\java>java test
my name is 0
my name is 1
my name is 2
my name is 3
my name is 4
my name is 0
my name is 1
my name is 2
my name is 3
my name is 4
my name is 0
my name is 1
my name is 2
my name is 3
my name is 4
my name is 0
my name is 1
my name is 2
my name is 3
my name is 4
my name is 4
my name is 3
my name is 2
my name is 1
my name is 0
my name is 4
my name is 3
my name is 2
my name is 1
my name is 0
my name is 4
my name is 3
my name is 2
my name is 1
my name is 0
my name is 4
my name is 2
my name is 0
my name is 3
my name is 1
my name is 4
my name is 2
my name is 3
my name is 1
my name is 0
my name is 4
my name is 2
my name is 3
my name is 1
my name is 0
my name is 4
my name is 2
my name is 1
my name is 0
my name is 3
jyf7356759 2008-03-07
  • 打赏
  • 举报
回复
谢谢楼上,不过测试了运行不了,能再帮我看看么?
jyf7356759 2008-03-07
  • 打赏
  • 举报
回复
求助!高手帮帮忙啊。特别是各位版主!
zapdos 2008-03-07
  • 打赏
  • 举报
回复

public class test{
test() throws Exception{
Thread t[] = new Thread[5];
final Thread o = new Thread();
for(int i=0;i<t.length;i++){
final int j = i;
t[i] = new Thread(){
{start();}
public void run(){
try{
System.out.println("my name is "+j);
synchronized(o){
o.wait();
}
run();
}catch(Exception e){System.out.println(e);}

}
};
}
for(int i=0;i<10;i++){
synchronized(this){
wait(500);
}
synchronized(o){
o.notifyAll();
}
}
System.exit(0);
}
public static void main(String args[]) throws Exception{
new test();
}
}

这样行不?
每半秒暂停一次
jyf7356759 2008-03-07
  • 打赏
  • 举报
回复
晕死,没人做过么?
jyf7356759 2008-03-07
  • 打赏
  • 举报
回复
比如一个简单的程序,打印1-100,按p暂停,r继续,s停止,谢谢各位了。
jyf7356759 2008-03-07
  • 打赏
  • 举报
回复
最后顶一下,睡觉了。
加载更多回复(2)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧