package com.lyon.demo.test.mayi; public class StopThreadDemo extends Thread{ public boolean flag = true; @Override public void run() { while (flag) { System.out.println("---"); } } public void stopThread(){ this.flag = false; System.out.println(Thread.currentThread().getName()+"线程停止了"); } public static void main(String[] args) { StopThreadDemo stopThreadDemo = new StopThreadDemo(); StopThreadDemo stopThreadDemo1 = new StopThreadDemo(); stopThreadDemo.start(); stopThreadDemo1.start(); for (int i = 0; i < 30; i++) { try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("...main"+i); if (i==29) { stopThreadDemo.stopThread(); stopThreadDemo1.stopThread(); } } } }
多线程复习之停止线程
最新推荐文章于 2023-07-20 12:19:59 发布