zoukankan      html  css  js  c++  java
  • java多线程快速入门(十九)

    如何停止线程

      1、使用stop方法强行终止线程(这个方法不推荐使用,可能会出现业务操作未执行完,线程中断了)

    package com.cppdy;
    
    class MyThread15 extends Thread{
    
        @Override
        public synchronized void run() {
            while(true) {
                try {
                    wait();
                } catch (Exception e) {
                    
                }
                //如果这里有非常重要的代码块,是不是就出现问题了
                System.out.println("Thread run------");
            }
        }
        
    }
    
    public class ThreadDemo15 {
    
        public static void main(String[] args) throws Exception {
            MyThread15 mt = new MyThread15();
            Thread thread1 = new Thread(mt);
            Thread thread2 = new Thread(mt);
            thread1.start();
            thread2.start();
            int count=0;
            while(true) {
                System.out.println("Main run------");
                Thread.sleep(100);
                if(count==30) {
                    thread1.stop();
                    thread2.stop();
                    break;
                }
                count++;
            }
        }
    
    }
    View Code

      2、使用interrupt方法中断线程(将业务操作执行完后再中断线程)

    package com.cppdy;
    
    class MyThread15 extends Thread{
    
        @Override
        public synchronized void run() {
            while(true) {
                try {
                    wait();
                } catch (Exception e) {
                    
                }
                //如果这里有非常重要的代码块,是不是就出现问题了
                System.out.println("Thread run------");
            }
        }
        
    }
    
    public class ThreadDemo15 {
    
        public static void main(String[] args) throws Exception {
            MyThread15 mt = new MyThread15();
            Thread thread1 = new Thread(mt);
            Thread thread2 = new Thread(mt);
            thread1.start();
            thread2.start();
            int count=0;
            while(true) {
                System.out.println("Main run------");
                Thread.sleep(100);
                if(count==30) {
                    thread1.interrupt();
                    thread2.interrupt();
                    break;
                }
                count++;
            }
        }
    
    }
    View Code

      3、使用退出标志,使线程正常退出,也就是当run方法完成后线程终止

    package com.cppdy;
    
    class MyThread15 extends Thread{
    
        public volatile boolean flag=true;
        
        @Override
        public synchronized void run() {
            while(flag) {
                try {
                    
                } catch (Exception e) {
                    setFlag(flag);
                }
                System.out.println("Thread run------");
            }
        }
        
        public void setFlag(boolean flag) {
            this.flag=flag;
        }
        
    }
    
    public class ThreadDemo15 {
    
        public static void main(String[] args) throws Exception {
            MyThread15 mt = new MyThread15();
            Thread thread1 = new Thread(mt);
            Thread thread2 = new Thread(mt);
            thread1.start();
            thread2.start();
            int count=0;
            while(true) {
                System.out.println("Main run------");
                Thread.sleep(100);
                if(count==30) {
                    mt.setFlag(false);
                    break;
                }
                count++;
            }
        }
    
    }
    View Code
  • 相关阅读:
    pyqt5 动画在QThread线程中无法运行问题
    几种编码格式
    Android精品课程—PullToRefresh 下拉刷新
    【张泽华】android视频教程下载地址及上课源代码
    Android中常用适配器及定义自己的适配器
    VirtualBOX 虚拟机安装 OS X 10.9 Mavericks 及 Xcode 5,本人X220亲测
    google maps api申请的问题
    如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)
    cvsnt 设置用户、修改密码
    Windows下用Git下载android源码 转载
  • 原文地址:https://www.cnblogs.com/jiefu/p/10017595.html
Copyright © 2011-2022 走看看