java多线程入门学习(三)

java多线程入门学习(三)
在java中有三种方式可以终止正在运行的线程:
        (1)使用退出标志,使线程正常退出,也就是当前run()方法执行完成后线程终止
        (2)使用stop()方法强行终止,不推荐这个方法,因为stop和suspend及resume一样,都是过期的方法
        (3)使用interrupt方法终止线程

一.停不了的线程

         这里主要讲解的是interrupt()方法去停止线程,但是这种方式仅仅是在当前线程中打了一个停止标记,并不是真正的停止线程。
我们新建了一个线程类:
   
   
package cn.sun.interrupt;
 
public class MyThread extends Thread{
 
@Override
public void run() {
super.run();
for(int i=0; i<5000; i++){
System.out.println("i="+(i+1));
}
}
}
使用测试类:
   
   
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
myThread.sleep(2000);
myThread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果发现并没有任何改变,控制台输出依然是从1到5000。从这里我们可以学习到调用interrupt()方法并没有停止线程,那怎么办?

二.判断线程是否停止

         java的SDK中Thread.java提供了两种方法:
         (1)this.interrupt() :测试当前线程是否已经中断    静态方法
         (2)this.isInterrupt() :测试线程是否已经中断        非静态方法
我们试试看,奂时尚一个的MyThread.java,测试如下:
   
   
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
System.out.println("是否停止1?="+ myThread.interrupted());
System.out.println("是否停止2?="+ myThread.interrupted());
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果为:
   
   
i=4997
i=4998
i=4999
i=5000
是否停止1?=false
是否停止2?=false
为什么?因为:
interrupt()方法判断的是当前线程是否中断,这里很明显当前正在运行的是main线程,这个线程从未停止过,那既然这样,我们就试试能不能把main给中断了:
   
   
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
Thread.currentThread().interrupt();
System.out.println("是否停止1?="+Thread.interrupted());
System.out.println("是否停止2?="+Thread.interrupted());
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果是:
   
   
是否停止1?=true
是否停止2?=false
发现真的把main给中断了,但是很奇怪第二遍输出为什么又变成false了呢,究其原因是因为interrupt()方法具有清除状态功能
下面我们试试isInterrupted()方法:
   
   
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
System.out.println("是否停止1?="+myThread.isInterrupted());
System.out.println("是否停止2?="+myThread.isInterrupted());
} catch (Exception e) {
e.printStackTrace();
}
}
}
即如果如下:
   
   
i=168989
是否停止1?=true
是否停止2?=true
i=168990
从结果中得到:isInterruptes()方法并没有清除状态标识

总结一下:
          (1)this.interrupted() : 测试当前线程是否已经是中断状态,执行后将状态标识清除为false。
         (2)this.isInterrupted() : 测试线程Thread对象是否已经是中断状态,但不清除标志
3.能停止的线程——异常法

三.能停止的线程——异常法

首先恢复到刚开始的问题,如何停止线程,上面已经学了两种方法去判断是否是停止的线程,那么我们就用进去,还是上一个例子,我们在for循环时每次去查看是否线程已停止,如果停止了那就退出,不再继续执行:
   
   
package cn.sun.interrupt;
 
public class MyThread extends Thread{
 
@Override
public void run() {
super.run();
try {
for(int i=0; i<500000; i++){
if(this.interrupted()){
System.out.println("已经停止运行,将退出");
throw new InterruptedException();
}
System.out.println("i="+(i+1));
}
System.out.println("这是for循环外面的一句话");
} catch (InterruptedException e) {
System.out.println("进入到MyThread.java的run方法的catch块了");
}
}
}

测试类:
   
   
package cn.sun.interrupt;
 
public class Test01 {
 
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
} catch (Exception e) {
e.printStackTrace();
}
}
}
结果:
   
   
i=159582
i=159583
i=159584
已经停止运行,将退出
进入到MyThread.javarun方法的catch块了
         有的人疑惑为什么在for循环里不放break而要放exception,这是因为break仅仅停掉了for循环,它会跳出for循环继续执行后面的代码,而用异常便可以直接退出,不执行之后的代码

四.在沉睡中停止

让我们看看在沉睡中被终止的话控制台是如何显示的:
   
   
package cn.sun.interrupt;
 
public class Athread extends Thread{
 
@Override
public void run() {
super.run();
try {
System.out.println("run begin...");
Thread.sleep(2000);
System.out.println("run over...");
} catch (InterruptedException e) {
System.out.println("在沉睡中被终止,进入catch = "+this.interrupted());
}
}
}
测试:
   
   
package cn.sun.interrupt;
 
public class Test02 {
public static void main(String[] args) {
try {
 
Athread thread = new Athread();
thread.start();
Thread.sleep(200);
thread.interrupt();
} catch (InterruptedException e) {
System.out.println("main catch...");
e.printStackTrace();
}
System.out.println("end!");
}
 
}
结果是:
   
   
run begin...
end!
在沉睡中被终止,进入catch = false
结论:从沉睡状态中断会进入catch,同时停滞状态值已被清除,显示false 。

以下是相反的步骤:
   
   
package cn.sun.interrupt;
 
public class Athread extends Thread{
 
@Override
public void run() {
super.run();
try {
for(int i=0; i<5; i++){
System.out.println("i="+(i+1));
}
System.out.println("run begin...");
Thread.sleep(2000);
System.out.println("run over...");
} catch (InterruptedException e) {
System.out.println("先终止,再遇到了sleep,进入catch = "+this.interrupted());
}
}
}

测试:
   
   
package cn.sun.interrupt;
 
public class Test02 {
public static void main(String[] args) {
Athread thread = new Athread();
thread.start();
thread.interrupt();
System.out.println("end!");
}
 
}
这是结果:
   
   
end!
i=1
i=2
i=3
i=4
i=5
run begin...
先终止,再遇到了sleep,进入catch = false


五.能停止的线程——暴力停止


这种方法是直接使用stop()方法,就像突然掐断了一样停止在那个位置。

六.释放锁的不良后果和使用return停止线程

这里主要还是强调stop方法会导致数据不一致后果,不要使用这个方法了
另外,有上面可知其实有效使用return也可以停止线程:
   
   
package cn.sun.interrupt;
 
public class Bthread extends Thread{
 
@Override
public void run() {
while(true){
if(this.isInterrupted()){
System.out.println("停住了");
return ;
}
System.out.println("=======");
}
}
}
测试:
   
   
package cn.sun.interrupt;
 
public class Test02 {
public static void main(String[] args) {
try {
 
Bthread thread = new Bthread();
thread.start();
Thread.sleep(200);
thread.interrupt();
     } catch ( InterruptedException e ) {
System.out.println("main catch...");
e.printStackTrace();
}
System.out.println("end!");
}
 
}

结果为:
   
   
=======
=======
=======
停住了
end!

























































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值