怎样暂时停止一个thread,然后我给一个命令thread继续

tzjrxy 2008-10-22 12:52:35
请给一个例子给我
...全文
729 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
pauliuyou 2008-10-31
  • 打赏
  • 举报
回复
public void run()
{
while(true)
{
if (isRun)
{
doSomething();
}
}
}

在外部控制isRun变量, 就可以了
nine_suns99 2008-10-24
  • 打赏
  • 举报
回复
楼主还不结贴啊?等分升4角呢……
tzjrxy 2008-10-22
  • 打赏
  • 举报
回复
这个例子我看了,可是实际上我只有一个thread,我就想运行thread的时候我发一个命令,它可以停,一直等到我发另外一个命令,它又可以继续动

假设我用interrupt()停止了这个thread, 怎么样可以再开始?
lihan6415151528 2008-10-22
  • 打赏
  • 举报
回复
楼主参考这个


解释很详细了



JAVA 线程很好的例子


import java.io.*;
//多线程编程
public class MultiThread{
public static void main(String args[]){
System.out.println("我是主线程!";

//下面创建线程实例thread1
ThreadUseExtends thread1=new ThreadUseExtends();

//创建thread2时以实现了Runnable接口的ThreadUseRunnable类实例为参数
Thread thread2 = new Thread(new ThreadUseRunnable(),"SecondThread";

thread1.start();//启动线程thread1使之处于就绪状态
//thread1.setPriority(6);//设置thread1的优先级为6
//优先级将决定cpu空出时,处于就绪状态的线程谁先占领cpu开始运行
//优先级范围1到10,MIN_PRIORITY,MAX_PRIORITY,NORM_PAIORITY
//新线程继承创建她的父线程优先级,父线程通常有普通优先级即5NORM_PRIORITY

System.out.println("主线程将挂起7秒!";
try{
Thread.sleep(7000);//主线程挂起7秒
}catch (InterruptedException e){
return;
}

System.out.println("又回到了主线程!";
if(thread1.isAlive()){
thread1.stop();//如果thread1还存在则杀掉他
System.out.println("thread1休眠过长,主线程杀掉了thread1!";
}else
System.out.println("主线程没发现thread1,thread1已醒顺序执行结束了!";

thread2.start();//启动thread2
System.out.println("主线程又将挂起7秒!";
try{
Thread.sleep(7000);//主线程挂起7秒
}catch (InterruptedException e){
return;
}

System.out.println("又回到了主线程!";
if(thread2.isAlive()){
thread2.stop();//如果thread2还存在则杀掉他
System.out.println("thread2休眠过长,主线程杀掉了thread2!";
}else
System.out.println("主线程没发现thread2,thread2已醒顺序执行结束了!";

System.out.println("程序结束按任意键继续!");
try{
System.in.read();
}catch (IOException e){
System.out.println(e.toString());
}

}//main
}//MultiThread


class ThreadUseExtends extends Thread{
//通过继承Thread类,并实现它的抽象方法run()
//适当时候创建这一Thread子类的实例来实现多线程机制
//一个线程启动后(也即进入就绪状态)一旦获得CPU将自动调用它的run()方法

ThreadUseExtends(){}//构造函数
public void run(){
System.out.println("我是Thread子类的线程实例!");
System.out.println("我将挂起10秒!");
System.out.println("回到主线程,请稍等,刚才主线程挂起可能还没醒过来!");
try{
sleep(10000);//挂起5秒
}catch (InterruptedException e){
return;
}

//如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉
//但如果休眠时间过长,则线程还存活,可能被stop()杀掉
}
}


class ThreadUseRunnable implements Runnable{
//通过实现Runnable接口中的run()方法,再以这个实现了run()方法的类
//为参数创建Thread的线程实例

//Thread thread2=new Thread(this);
//以这个实现了Runnable接口中run()方法的类为参数创建Thread类的线程实例
ThreadUseRunnable(){}//构造函数
public void run(){
System.out.println("我是Thread类的线程实例并以实现了Runnable接口的类为参数!");
System.out.println("我将挂起1秒!");
System.out.println("回到主线程,请稍等,刚才主线程挂起可能还没醒过来!");
try{
Thread.sleep(1000);//挂起5秒
}catch (InterruptedException e){
return;
}
//如果该run()方法顺序执行完了,线程将自动结束,而不会被主线程杀掉
//但如果休眠时间过长,则线程还存活,可能被stop()杀掉
}
}
//该程序可做的修改如改休眠时间或优先级setPriority()


duduli 2008-10-22
  • 打赏
  • 举报
回复
15楼的方法可以。
w3329307 2008-10-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yinyuan1987 的回复:]
Java code
class NameRunnable implements Runnable{
public void run(){
System.out.println("Run by"+Thread.currentThread().getName());

try{
Thread.sleep(1000);
}catch(InterruptedException ex){}
}

}


public class ManyNames{
public static void main(String[] args){
NameRunnable nr = new NameRunnable();

Thread one = new Thread(nr);
one.setNa…
[/Quote]

有个地方敲过了,
w3329307 2008-10-22
  • 打赏
  • 举报
回复
yield()可以停止的吧
  • 打赏
  • 举报
回复
呵呵,楼上朋友已经把例子给出来了!

楼主加油!

不过建议楼主去看一下我的博客,对楼主很有帮助的!

共同学习
liujiaqwer 2008-10-22
  • 打赏
  • 举报
回复
至少应该有2个thread吧
abcniu 2008-10-22
  • 打赏
  • 举报
回复
在你的类中加入两个公共方法
public synchronized void 挂起线程()
{
try
{
wait();
}
catch(InterruptedException e)
{

}
}

public synchronized void 恢复线程()
{
notifyAll();
}

创建的对象直接调用这两个方法就可以了
nine_suns99 2008-10-22
  • 打赏
  • 举报
回复

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ThreadTest extends Thread
{
private String status = "r";

public void run()
{
while(status.equalsIgnoreCase("r") || status.equalsIgnoreCase("s"))
{
System.out.println("Thread " + Thread.currentThread().getName() + " is running...");
try
{
Thread.sleep(1000L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
if(status.equalsIgnoreCase("s"))
{
System.out.println("Thread " + Thread.currentThread().getName() + " is waiting...");
synchronized(this)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}

public static void main(String[] args)throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
ThreadTest thread = new ThreadTest();
thread.start();
while(true)
{
String option = in.readLine();
if(option.equalsIgnoreCase("Q"))
{
System.out.println("System exit!");
thread.status = "q";
break;
}
else if(option.equalsIgnoreCase("S"))
{
thread.status = "s";
}
else if(option.equalsIgnoreCase("R"))
{
thread.status = "r";
synchronized(thread)
{
thread.notify();
}
}

}
}
}
ZXEOC 2008-10-22
  • 打赏
  • 举报
回复
去看JDK文档
tzjrxy 2008-10-22
  • 打赏
  • 举报
回复
怎么用wait和notify()?给个例子行不行?
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 tzjrxy 的回复:]
我不要sleep这个方法………………
[/Quote]
你看看我7楼帖子中,有thread.interrupt(); 方法啊!
  • 打赏
  • 举报
回复
你要是只是暂时使线程等待,可以使用wait()是线程等待,用notify()或Allnotify()唤醒线程

若你想了解更多关于Thread.interrupt知识,去我博客:
http://blog.csdn.net/yinyuan1987/archive/2008/10/22/3123725.aspx
这篇文章,专门讲解中断的
ZXEOC 2008-10-22
  • 打赏
  • 举报
回复
你的程序只有一个thread?是除了main线程以外只有一个吧?main也是一个线程
tzjrxy 2008-10-22
  • 打赏
  • 举报
回复
我不要sleep这个方法………………
  • 打赏
  • 举报
回复
首先楼主想中断一线程
不要Thread.interrupt所迷惑。
尽管,其名称似乎在暗示着什么,然而,这种方法并不会中断一个正在运行的线程(待会将进一步说明),正如Listing A中描述的那样。它创建了一个线程,并且试图使用Thread.interrupt方法停止该线程。Thread.sleep()方法的调用,为线程的初始化和中止提供了充裕的时间。线程本身并不参与任何有用的操作。

Listing A
class Example1 extends Thread {

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

Example1 thread = new Example1();

System.out.println( "Starting thread..." );

thread.start();

Thread.sleep( 3000 );

System.out.println( "Interrupting thread..." );

thread.interrupt();

Thread.sleep( 3000 );

System.out.println( "Stopping application..." );

System.exit( 0 );

}


如果你运行了Listing A中的代码,你将在控制台看到以下输出:

Starting thread...

Thread is running...

Thread is running...

Thread is running...

Interrupting thread...

Thread is running...

Thread is running...

Thread is running...

Stopping application...

甚至,在Thread.interrupt()被调用后,线程仍然继续运行了一段时间。

xqh2168 2008-10-22
  • 打赏
  • 举报
回复
wait()是线程等待,用notify()或Allnotify()唤醒线程。
  • 打赏
  • 举报
回复
原来是楼主没说清楚啊!我上面打的就当是自我学习了。
加载更多回复(2)

62,615

社区成员

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

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