死循环任务的线程结束方式

记录于2021年11月16日

前言

以下介绍的两种方发的适用情景不同,可根据具体需求选择使用。

一、两种方法

1.方法一
用定时器去结束任务,设定最短结束时间,若任务执行耗时不记,那么最快会经过给定的时间后退出。若算上任务执行的时间,则最快会在(任务执行的时间+给定的时间)后退出,只有一个线程时可以使用此方法。
2.方法二
在本线程(任务为死循环)中等待另一个线程完成任务,当另一个任务完成,此线程结束,当在线程中执行一些计算而主线程又急需这个计算结果,或者是本线程需要在某些任务完成后立即执行时,可以使用本方法。

二、代码实例

1.方法一

(计数器数)*(定时器时间) = 线程最快结束时间:

class ThreadDead extends Thread{	
	@Override
	public void run(){
		int count = 0;
		while(true){	//死循环
			try{
				if(++count==50)			//计数器
					break;
				
				//在此可以定义需要完成的任务
				
				Thread.sleep(20);		//定时器
			}catch(InterruptedException ie){
				ie.printStackTrace(System.out);
			}
		}
		System.out.println("线程任务完成!");
	}

	public static void main(String[] args){
		new ThreadDead().start();
	}

}
2.方法二

情景说明:现在有两个线程,一个负责拍摄且拍摄一段时间,假定1秒,一个负责播放,播放线程在拍摄线程没有结束之前需要一直运行,一旦拍摄线程结束,播放线程也要随之结束

代码如下:

public class ThreadDead2  {		
	
	volatile boolean isFinished = false;	//轻量级锁,禁用CPU缓存,使变量的改变直接写入到内存
	
	ThreadDead2(){
		new Play().start();		//分别启动两个线程
		new Shot().start();
	}

	class Play extends Thread{

		@Override
		public void run(){
			while(true){
				//你的任务在此定义
				if(isFinished)
					break;
			}
			System.out.println("播放任务结束!");
		}
	}

	class Shot extends Thread{

		@Override
		public void run(){
			try{
				Thread.sleep(1000);		//假设拍摄任务耗时1000ms
			}catch(InterruptedException ie){
				ie.printStackTrace(System.out);
			}
			System.out.println("拍摄任务完成!");
			isFinished = true;			//拍摄任务完成
		}
	}

	public static void main(String[] args){
		new ThreadDead2();
	}
}

总结

若本文有不恰当的地方,望大佬留下评论及时指正。(~ ̄▽ ̄)~
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 线程中写循环会导致线程一直占用 CPU 资源,影响系统的性能和稳定性。如果需要中断线程,可以使用线程的中断机制。 在 Java 中,线程中断机制通过调用线程的 interrupt() 方法实现。该方法会设置线程的中断标志位,线程在执行过程中可以检查该标志位,如果标志位被设置,线程可以选择退出执行。 下面是一个示例代码: ```java public class MyThread extends Thread { @Override public void run() { while (!Thread.interrupted()) { // 线程执行的代码 } } } public class Main { public static void main(String[] args) throws InterruptedException { MyThread thread = new MyThread(); thread.start(); // 在主线程中等待一段时间后中断子线程 Thread.sleep(1000); thread.interrupt(); } } ``` 在上面的代码中,MyThread 类继承自 Thread,重写了 run() 方法,在 run() 方法中使用 while 循环进行线程执行的代码。在 while 循环中使用 Thread.interrupted() 方法检查线程的中断标志位,如果标志位被设置,while 循环会退出,线程也会结束执行。 在 Main 类中,创建了 MyThread 对象并启动线程。在主线程中等待一段时间后,调用 thread.interrupt() 方法中断子线程的执行。 ### 回答2: 在线程中使用循环可能会导致线程无法正常退出,因为线程会一直停留在循环中,无法执行循环外的其他代码。但我们可以通过中断线程的方式来解决这个问题。 要中断一个线程,我们可以使用Thread类提供的interrupt()方法。首先,在循环内部通过检查Thread类的interrupted()方法来确定是否发生了中断请求。如果发生了中断请求,我们可以使用break语句来跳出循环。接下来,我们可以使用return语句或者在循环外部进行清理操作,从而使线程正常退出。 以下是一个简单的示例代码: ```java public class MyThread extends Thread { @Override public void run() { while (!Thread.interrupted()) { // 循环体代码 // 检查是否发生中断 if (Thread.interrupted()) { break; } } // 清理操作 } } public class Main { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); // 等待一段时间后中断线程 try { Thread.sleep(1000); thread.interrupt(); } catch (InterruptedException e) { e.printStackTrace(); } } } ``` 在这个示例中,我们创建了一个继承自Thread类的自定义线程类MyThread,并在run()方法中进行了循环。在循环中,我们通过调用Thread.interrupted()方法来检查是否发生了中断请求。如果中断请求发生,我们使用break语句来跳出循环。在main方法中,我们创建了一个MyThread对象并启动它。然后,通过调用Thread.sleep()方法等待一段时间后,调用thread.interrupt()方法来中断线程。 通过这种方式,我们可以在循环内部检查中断请求并优雅地让线程退出循环,从而中断线程的执行。 ### 回答3: 在线程中使用循环是为了让线程一直执行某个任务,但有时候我们需要中断这个线程,停止它的执行。下面是一种常见的在线程中写循环后中断线程的方法: 通常,我们会使用一个boolean类型的变量来标识线程是否继续执行循环。在外部想要中断线程时,我们将这个变量设置为false,从而使线程退出循环。 具体步骤如下: 1. 在线程的类中定义一个boolean类型的成员变量,例如isRunning,并初始化为true。 2. 在线程的run方法中使用while循环,并检查isRunning的值是否为true,如果是则继续执行循环。 3. 当外部需要中断线程时,通过调用线程对象的interrupt()方法发送中断信号给线程。 4. 在线程的run方法中的while循环中,检查线程的中断状态,可以使用Thread类的静态方法Thread.interrupted()来检查中断状态,如果检查到中断状态为true,则退出循环。 5. 循环结束后,线程终止执行。 示例代码如下: ```java public class MyThread extends Thread { private volatile boolean isRunning = true; @Override public void run() { while (isRunning) { // 执行线任务的代码 if (Thread.interrupted()) { // 检查线程的中断状态 break; } } // 线结束执行 } public void stopRunning() { isRunning = false; } } ``` 在外部代码中,我们可以通过调用线程对象的stopRunning()方法来中断线程: ```java public class Main { public static void main(String[] args) { MyThread thread = new MyThread(); thread.start(); // 一段时间后中断线程 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } thread.stopRunning(); } } ``` 这样,当调用stopRunning()方法后,线程的isRunning变量将被设置为false,从而让线程退出循环,实现了中断线程的目的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值