首页 > 编程知识 正文

java线程停止,java 让线程停止

时间:2024-04-27 11:08:42 阅读:334747 作者:JEFT

本文目录一览:

java 中如何使线程运行一定时间后停止?

java中使线程运行一定时间后停止,可以设置一个变量,当满足条件则退出线程:

import static java.lang.Thread.currentThread;

import java.util.concurrent.TimeUnit;

public class ThreadPauseDemo{

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

       Game game = new Game();

       Thread t1 = new Thread(game, "T1");

       t1.start();

       // 现在停止Game线程

       System.out.println(currentThread().getName() + " is stopping game thread");

       game.stop();

       // 查看Game线程停止的状态

       TimeUnit.MILLISECONDS.sleep(200);

       System.out.println(currentThread().getName() + " is finished now");

   }

}

class Game implements Runnable{

   private volatile boolean isStopped = false;

   public void run(){

       while(!isStopped){

           System.out.println("Game thread is running......");

           System.out.println("Game thread is now going to pause");

           try{

               Thread.sleep(200);

           } catch(InterruptedException e){

               e.printStackTrace();

           }

           System.out.println("Game thread is now resumed......");

       }

       System.out.println("Game thread is stopped......");

   }

   public void stop(){

       isStopped = true;

   }

}

程序输出如下:

Game thread is running......

main is stopping game thread

Game thread is now going to pause

Game thread is now resumed......

Game thread is stopped......

main is finished now

java如何关闭线程

关闭线程有几种方法,

一种是调用它里面的stop()方法

另一种就是你自己设置一个停止线程的标记 (推荐这种)

代码如下:

package com.demo;

//测试Thread的stop方法和自己编写一个停止标记来停止线程;

public class StopThread implements Runnable{

//停止线程的标记值boolean;

private boolean flag = true;

public void stopThread(){

flag = false;

}

public void run(){

int i=0;

while(flag){

i++;

System.out.println(Thread.currentThread().getName()+":"+i);

try{

Thread.sleep(1000);

}catch(Exception e){

}

System.out.println(Thread.currentThread().getName()+"=="+i);

}

}

public static void main(String args[]){

StopThread st = new StopThread();

Thread th = new Thread(st);

Thread th1 = new Thread(st);

th.start();

th1.start();

try{

Thread.sleep(5500);

}catch(Exception e){

}

/*

如果使用Thread.stop方法停止线程,不能保证这个线程是否完整的运行完成一次

run方法;但是如果使用停止的标记位,那么可以保正在真正停止之前完整的运行完

成一次run方法;

*/

th.stop();

st.stopThread();

}

}

java 怎么强制关闭 一个线程 ?

在Java的多线程编程中,java.lang.Thread类型包含了一些列的方法start(), stop(), stop(Throwable) and suspend(), destroy() and resume()。通过这些方法,我们可以对线程进行方便的操作,但是这些方法中,只有start()方法得到了保留。x0dx0a在Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》中详细讲解了舍弃这些方法的原因。x0dx0a如果真的需要终止一个线程,可以使用以下几种方法: x0dx0a1、让线程的run()方法执行完,线程自然结束。(这种方法最好)x0dx0ax0dx0a2、通过轮询和共享标志位的方法来结束线程,例如while(flag){},flag的初始值设为真,当需要结束时,将flag的值设为false。(这种方法也不很好,因为如果while(flag){}方法阻塞了,则flag会失效)x0dx0a如果线程因为执行sleep()或是wait()而进入Not Runnable状态,假如是wait() 用标志位就方法就不行了,x0dx0apublic final void wait(long timeout)x0dx0a throws InterruptedException此方法导致当前线程(称之为 T)将其自身放置在对象的等待集中,然后放弃此对象上的所有同步要求。即当前线程变为等待状态x0dx0await() 的标准使用方法x0dx0asynchronized(obj){x0dx0awhile(){x0dx0aobj.wait();x0dx0a}x0dx0a满足条件的处理过程x0dx0a}x0dx0a而您想要停止它,您可以使用第三种即x0dx0a3 使用interrupt(),而程式会丢出InterruptedException例外,因而使得执行绪离开run()方法

java多线程中如何有效的停止当前线程。

中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切,有效地中止其当前的操作。

线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序。虽然初次看来它可能显得简单,但是,你必须进行一些预警以实现期望的结果。你最好还是牢记以下的告诫。

首先,忘掉Thread.stop方法。虽然它确实停止了一个正在运行的线程,然而,这种方法是不安全也是不受提倡的,这意味着,在未来的JAVA版本中,它将不复存在。

中断线程最好的,最受推荐的方式是,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务。线程必须周期性的核查这一变量(尤其在冗余操作期间),然后有秩序地中止任务。

如何停止一个Java线程

1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止。

2. 使用stop方法强行终止线程(这个方法不推荐使用,因为stop和suspend、resume一样,也可能发生不可预料的结果)。

3. 使用interrupt方法中断线程。

1. 使用退出标志终止线程

当run方法执行完后,线程就会退出。但有时run方法是永远不会结束的。如在服务端程序中使用线程进行监听客户端请求,或是其他的需要循环处理的任务。在这种情况下,一般是将这些任务放在一个循环中,如while循环。如果想让循环永远运行下去,可以使用while(true){……}来处理。但要想使while循环在某一特定条件下退出,最直接的方法就是设一个boolean类型的标志,并通过设置这个标志为true或false来控制while循环是否退出。下面给出了一个利用退出标志终止线程的例子。

java线程如何停止?

通过调用interrupt方法可以使得处于阻塞状态的线程抛出一个异常,即interrupt方法可以用来中断一个正处于阻塞状态的线程;另外,改方法还会设置线程的中断状态(注:isInterrupted()可以用来查询中断状态)。

线程阻塞在reader.readLine()时,即使主线程改变了标志变量,但是并不能立即结束子线程,只有等待阻塞被打破,且运行到下一次循环条件判断的时候才能终止。所以在使用这种方法时,应该考虑到阻塞这种情况。当然,如果整个循环内的操作属于同一事务时,这种方法倒很不错。

调用线程对象的interrupt()时,sleep的线程会抛出InterruptedException异常,从而中断循环,终止线程。但是如果是IO如输入这些阻塞,中断的方法又不起作用了,还有就是对于没有阻塞的线程,调用interrupt()是达不到终止线程的效果的。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。