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");
            }
        }
    }
}


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);
        }
    }
}


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");
    }
}


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();
            }
        }
    }
}

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);
        }
    }
}

二、总结

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


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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值