线程启动start后,如何停止的?

通过调用Thread类的start()方法来启动一个线程,这时此线程是处于就绪状态,并没有运行。
然后通过此Thread类调用方法run()来完成其运行操作的,
这里方法run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程终止,
而CPU再运行其它线程,

Deamon:守护进程

 

  1. public class TestThread { 
  2.  
  3.     public static void main(String[] args) { 
  4.         Thread1 t = new Thread1(); 
  5.         Thread c = new Control(t); 
  6.         t.setSleep(true); 
  7.         c.setDaemon(true); 
  8.          
  9.         t.start(); 
  10.         c.start(); 
  11.         System.out.println("You can input 'g' and 'Enter' to start your job."); 
  12.         System.out.println("You can input 'w' and 'Enter' to let your job to wait..."); 
  13.         System.out.println("You can input 's' and 'Enter' to finish your job."); 
  14.     } 
  15. class Thread1 extends Thread { 
  16.     private boolean isSleep = true
  17.     private boolean isStop = false
  18.      
  19.     public void run() { 
  20.         while(!isStop) { 
  21.             if(isSleep) { 
  22.                 try
  23.                     Thread.sleep(1); 
  24.                 } catch (InterruptedException e) { 
  25.                     e.printStackTrace(); 
  26.                 } 
  27.             } else
  28.                 System.out.println("Thread: "+Thread.currentThread().getName() + " do someting."); 
  29.                 try
  30.                     Thread.sleep(2000); 
  31.                 } catch (InterruptedException e) { 
  32.                     e.printStackTrace(); 
  33.                 } 
  34.             } 
  35.         } 
  36.         System.out.println("Thread: "+Thread.currentThread().getName() + " finished."); 
  37.     } 
  38.      
  39.     public void setSleep(boolean sleep) { 
  40.         this.isSleep = sleep; 
  41.     } 
  42.     public void setStop(boolean stop) { 
  43.         this.isStop = stop; 
  44.     } 
  45. class Control extends Thread { 
  46.     private Thread1 t; 
  47.     public Control(Thread1 t) { 
  48.         this.t = t; 
  49.     } 
  50.      
  51.     public void run() { 
  52.         while(true) { 
  53.             int r=0
  54.             try
  55.                 r=System.in.read(); 
  56.             } catch (IOException e) { 
  57.                 e.printStackTrace(); 
  58.             } 
  59.             if(r == 'g') { 
  60.                 t.setSleep(false); 
  61.             } else if(r == 'w') { 
  62.                 t.setSleep(true); 
  63.             } else if(r == 's') { 
  64.                 t.setStop(true); 
  65.             } 
  66.         } 
  67.     } 

 

程序说明:
1,刚开始程序的等待的。
2,你输入‘g’回车后会运行。
3,你输入‘w’回车后会再次等待。
4,再次输入‘g’回车后又会运行。
5,输入‘s'回车,会终止程序。

6,这里将控制线程设置成了Deamon的形式,因为线程t由线程c控制可以终止,而线程c始终无法终止,所以把它设置为后台线程,当让控制的线程t退出时,所有的前台线程都结束了,这样线程c就可以自动退出。

 

ESP32 MicroPython中的线程管理可以使用`threading`模块来实现。以下是关于启动停止线程的基本步骤: **启动线程**: ```python import machine from utime import sleep_ms # 创建一个新的线程函数 def my_thread_function(): while True: print("Thread is running...") sleep_ms(1000) # 每秒打印一次 # 创建线程对象并启动 my_thread = machine.Thread(target=my_thread_function) my_thread.start() # 启动线程 ``` 在这个例子中,`target=my_thread_function`指定了线程运行的任务函数,`start()`则开始执行。 **停止线程**: MicroPython的`Thread`对象并没有直接提供一个用于停止线程的方法,因为它们通常是无限循环或者阻塞操作。如果你想优雅地停止一个任务,你需要在线程函数内部添加一些控制机制。 一种做法是在函数内部设置一个标志,外部通过某种信号(如全局变量、事件或者回调)告诉线程停止: ```python running = True ... def my_thread_function(): global running while running: if not some_condition: # 根据条件判断是否停止 running = False print("Thread is running...") sleep_ms(1000) # 在需要停止的地方设置running为False running = False ``` 另一种常见的做法是让线程等待一个特定事件,比如中断信号或一个共享计数器减到零。 **相关问题--:** 1. 如何处理ESP32 MicroPython中的线程同步问题? 2. 如果线程长时间未响应,如何检查其状态? 3. 在ESP32上创建过多线程会不会影响性能?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值