java多线程入门学习(二)

java多线程入门学习(二)

一.currentThread()和isAlive()

1. currentThread()方法

         返回的是当前运行线程,首先来个简单的例子了解一下这个方法的意义:
    
    
  1. package cn.sun.method;
  2. public class demo01 {
  3. public static void main(String[] args) {
  4. System.out.println(Thread.currentThread().getName());
  5. }
  6. }

         返回结果如下:
    
    
  1. main
         这样我们就知道当前运行的是main主函数。
         以下我们创建一个复杂一点的看下这里到底应该是哪个线程在某个时刻运行:
    
    
  1. package cn.sun.method;
  2. public class Demo02 extends Thread{
  3. public Demo02(){
  4. System.out.println("demo01 begin...");
  5. System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName());
  6. System.out.println("this.getName = "+ this.getName());
  7. System.out.println("demo02 end...");
  8. }
  9. public void run(){
  10. System.out.println("run begin...");
  11. System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName());
  12. System.out.println("this.getName = "+ this.getName());
  13. System.out.println("run end...");
  14. }
  15. }
运行测试:
    
    
  1. package cn.sun.test;
  2. import cn.sun.method.Demo02;
  3. public class Test01 {
  4. public static void main(String[] args) {
  5. Demo02 demo = new Demo02();
  6. Thread thread = new Thread(demo);
  7. thread.setName("A");
  8. thread.start();
  9. }
  10. }
结果如下:
    
    
  1. demo01 begin...
  2. Thread.currentThread().getName() = main
  3. this.getName = Thread-0
  4. demo02 end...
  5. run begin...
  6. Thread.currentThread().getName() = A
  7. this.getName = Thread-0
  8. run end...

2. isAlive()方法

          判断当前线程是否处于活动状态
首先来个例子看一看怎么回事:
    
    
  1. package cn.sun.method;
  2. public class MyThread extends Thread{
  3. @Override
  4. public void run() {
  5. System.out.println("run = "+this.isAlive());
  6. }
  7. }
测试类:
    
    
  1. package cn.sun.test;
  2. import cn.sun.method.Demo02;
  3. import cn.sun.method.MyThread;
  4. public class Test01 {
  5. public static void main(String[] args) {
  6. MyThread myThread = new MyThread();
  7. System.out.println("begin =="+myThread.isAlive());
  8. myThread.start();
  9. System.out.println("end =="+myThread.isAlive());
  10. }
  11. }
然后我们发现结果是这样子的:
    
    
  1. begin ==false
  2. end ==true
  3. run = true
总得来说这个方法不是太难,关键要分清楚某一时刻是不是出于活动状态,还有分清楚Thread.currentThread().getName()与this.getName()的区别。
最后来个例子,看例子比较清楚
    
    
  1. package cn.sun.method;
  2. public class CountOperate extends Thread{
  3. public CountOperate(){
  4. System.out.println("CountOperate begin...");
  5. System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
  6. System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
  7. System.out.println("this.getName()="+this.getName());
  8. System.out.println("this.isAlive()="+this.isAlive());
  9. System.out.println("CountOperate end...");
  10. }
  11. @Override
  12. public void run() {
  13. System.out.println("run begin...");
  14. System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
  15. System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
  16. System.out.println("this.getName()="+this.getName());
  17. System.out.println("this.isAlive()="+this.isAlive());
  18. System.out.println("run end...");
  19. }
  20. }
测试类:
     
     
  1. package cn.sun.test;
  2. import cn.sun.method.CountOperate;
  3. import cn.sun.method.Demo02;
  4. import cn.sun.method.MyThread;
  5. public class Test01 {
  6. public static void main(String[] args) {
  7. CountOperate countOperate = new CountOperate();
  8. Thread thread = new Thread(countOperate);
  9. System.out.println("main begin threat isAlive = "+thread.isAlive());
  10. thread.setName("A");
  11. thread.start();
  12. System.out.println("main end thread isAlive = "+thread.isAlive());
  13. }
  14. }
结果如下:
     
     
  1. CountOperate begin...
  2. Thread.currentThread().getName()=main
  3. Thread.currentThread().isAlive()=true
  4. this.getName()=Thread-0
  5. this.isAlive()=false
  6. CountOperate end...
  7. main begin threat isAlive = false
  8. main end thread isAlive = true
  9. run begin...
  10. Thread.currentThread().getName()=A
  11. Thread.currentThread().isAlive()=true
  12. this.getName()=Thread-0
  13. this.isAlive()=false
  14. run end...


3.this和Thread.currentThread()区别

Thread.currentThread() 返回当前线程对象,也就是执行这句代码这个线程。
this,也是当前线程对象~~~~
如果线程是start()方法调用起来的,那么二者是相等的。
不过,run()方法有可能被直接调用,这种情况下就不相等了。
      
      
  1. new Thread("aaa"){
  2. public void run(){
  3. System.out.println(Thread.currentThread() == this);
  4. new Thread("bbb"){
  5. public void run(){
  6. System.out.println(Thread.currentThread() == this);
  7. }
  8. }.start();
  9. new Thread("ccc"){
  10. public void run(){
  11. System.out.println(Thread.currentThread() == this);
  12. }
  13. }.run();
  14. }
  15. }.start();
结果是:
     
     
  1. true
  2. true
  3. false

第3个ccc线程是用run()方法调用的,那么就相当于new了一个普通类,调用了它的一个方法,仅此而已。
Thread.currentThread()返回的是aaa线程,this是ccc对象,所以不相等。

二.sleep()和getId()

1.sleep()方法

         在指定的毫秒数内让当前正在执行的线程休眠,这里的当前线程就是this.currentThread()返回的线程。。
简单举个例子看一下:
   
   
  1. package cn.sun.method;
  2. public class SleepDemo extends Thread{
  3. @Override
  4. public void run() {
  5. try {
  6. System.out.println("run threadName "+this.currentThread().getName()+" begin "+System.currentTimeMillis());
  7. Thread.sleep(2000);
  8. System.out.println("run threadName "+this.currentThread().getName()+" end "+System.currentTimeMillis());
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. }
测试类:
    
    
  1. package cn.sun.test;
  2. import cn.sun.method.CountOperate;
  3. import cn.sun.method.Demo02;
  4. import cn.sun.method.MyThread;
  5. import cn.sun.method.SleepDemo;
  6. public class Test01 {
  7. public static void main(String[] args) {
  8. SleepDemo s = new SleepDemo();
  9. System.out.println("begin="+System.currentTimeMillis());
  10. s.start();
  11. System.out.println("end="+System.currentTimeMillis());
  12. }
  13. }
结果是:
    
    
  1. begin=1460526284619
  2. end=1460526284619
  3. run threadName Thread-0 begin 1460526284620
  4. run threadName Thread-0 end 1460526286620

2. getId()方法

          便是线程唯一标示符
    
    
  1. package cn.sun.test;
  2. import cn.sun.method.CountOperate;
  3. import cn.sun.method.Demo02;
  4. import cn.sun.method.MyThread;
  5. import cn.sun.method.SleepDemo;
  6. public class Test01 {
  7. public static void main(String[] args) {
  8. Thread runThread = Thread.currentThread();
  9. System.out.println("name="+runThread.getName()+" id="+runThread.getId());
  10. }
  11. }
得到的是:
    
    
  1. name=main id=1



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值