• Java线程stop,sleep,yield,join


    一、Java多线程操作

    • 1、不建议使用stop,destroy方法来停止线程
    • 2、建议使用标志位flag来让线程自己停止

    run方法中:当flag为true时,线程一直执行
    公共停止方法中:设置flag为false。这样线程调用停止方法时,就会停止。

    TestStop.java
    让线程停止

    package com.peng.thread;
    
    // 不建议使用 stop,destroy等方法来停止线程
    // 建议使用flag标志位,让线程自己停止
    public class TestStop implements Runnable{
        private boolean flag = true;
        @Override
        public void run() {
            int i = 0;
            // 当flag为true时,线程一直执行
            while (flag){
                System.out.println("Thread is runing " + i++);
            }
        }
    
        // 一个公共stop方法,用来改变标志位为false
        public void stop(){
            this.flag = false;
        }
    
        public static void main(String[] args) {
            TestStop tt = new TestStop();
            new Thread(tt).start();
            for(int i=0; i < 1000; i++){
                System.out.println("Main Thread is running " + i);
                if (i == 900){
                	// 切换标志位,线程停止
                    tt.stop();
                    System.out.println("Thread is stoped");
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33


    TestSleep.java
    线程休眠
    利用Thread.sleep(1000),每隔1秒输出一次时间。

    package com.peng.thread;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    // 打印时间
    public class TestSleep {
        public static void main(String[] args) throws InterruptedException {
            while (true){
                Date curtime = new Date(System.currentTimeMillis());
                System.out.println(new SimpleDateFormat("HH:mm:ss").format(curtime));
                Thread.sleep(1000);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15


    TestYield.java
    线程礼让

    package com.peng.thread;
    
    // 线程礼让,给调度器一个提示,让线程从执行状态变为就绪态,然后处理器执行其他线程。但是调度器可以忽略它。
    // 也就是说yield不一定成功
    public class TestYield {
        public static void main(String[] args) {
            MyYield yield = new MyYield();
            new Thread(yield, "a").start();
            new Thread(yield, "b").start();
        }
    }
    
    class MyYield implements Runnable{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " starting");
            Thread.yield();
            System.out.println(Thread.currentThread().getName() + " ending");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20


    TestJoin
    线程插队

    package com.peng.thread;
    
    // join()  被插队线程暂时阻塞,等插队线程执行完之后,被阻塞线程继续执行
    public class TestJoin implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 300; i++){
                System.out.println(Thread.currentThread().getName() + "线程执行 " + i);
            }
        }
        public static void main(String[] args) throws InterruptedException {
            // 启动线程
            TestJoin tt = new TestJoin();
            Thread th1 = new Thread(tt, "aa");
            th1.start();
            Thread th2 = new Thread(tt, "bb");
            th2.start();
            // 主线程
            for (int i = 0; i < 200; i++){
                System.out.println("主线程执行 " + i);
                if (i == 100){
                    th1.join();
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    Thread.State的几种状态:
    NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED

    TestState.java

    package com.peng.thread;
    
    public class TestState{
        public static void main(String[] args) throws InterruptedException {
            // 使用lambda表达式
            Thread thread = new Thread(()->{
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("#####################");
            });
            // 此时线程thread的状态时NEW
            Thread.State state = thread.getState();
            System.out.println(state);
    
            // 此时线程thread状态是RUNNABLE
            thread.start();
            System.out.println(thread.getState());
    
            // 每隔200ms输出一次线程状态
            while (state != Thread.State.TERMINATED){
                Thread.sleep(200);
                state = thread.getState();
                System.out.println(state);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    二、总结

    Thread.sleep(1000),
    让当前线程休眠1000毫秒


    Thread.yield(),给调度器提示将执行态变为就绪态,暂时让出处理器给其他线程使用。但调度器可以忽略这个提示,也就是礼让不一定成功。

    Thread.stop(),不建议使用,而是要通过设置标志位的方式来结束线程。

    Thread.join(),插队,被插队线程暂时阻塞,等待插队线程执行完之后才能继续执行。

  • 相关阅读:
    基于php+mysql的大学生创业网站设计
    软件项目管理 8.4.软件项目质量计划
    Ae 核心表达式及用法(05):路径相关
    第一季:19单点登录【Java面试题】
    Kotlin协程 - launch原理 笔记
    Spring简介及IOC易懂介绍
    【产品经理】订单处理4-拆单策略
    BlockCanary原理解析
    百度给创新员工发2000w奖金........
    Vulnhub_CengBox
  • 原文地址:https://blog.csdn.net/qq_46480020/article/details/127717521