线程的使用方法

线程的停止方法很多

,由本身自带的stop和destroy方法,但是这些方法已经在jdk中停止使用了。我们可以通过1设置标志位,,2使用次数停止

package com.heal.Thread_test;

public class TestStop implements Runnable {
    private Boolean flag=true;
    @Override
    public void run() {
        int i=0;
        while(flag){
            System.out.println("run----------------Thread"+i++);
        }



    }
    //        设置一个公开的方法用来停止线程,其实就是改变flag的值
    public void stop(){
        this.flag=false;
    }

    public static void main(String[] args) {
        TestStop testStop=new TestStop();
        new Thread(testStop).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("mian"+i);
            if (i==900){
                //调用stop方法改变flag的值,使线程停止
                testStop.stop();
                System.out.println("该线程停止了");
            }
        }
    }

}

线程的休眠sleep()方法****

在这里插入图片描述

package com.heal.Thread_test;
//模拟一个倒计时
public class TestSleep {
    public static void main(String[] args) throws InterruptedException {
        tenDown();

    }

    public static void tenDown() throws InterruptedException {
    int num=10;
    while(true){
        Thread.sleep(1000);
        System.out.println(num--);
        if (num<0){
            break;

        }
    }
    }
}

线程礼让yield()方法*

在这里插入图片描述

package com.heal.Thread_test;

public class TestYield {
    public static void main(String[] args) {
        MyYield myYield = new MyYield();


        new Thread(myYield,"a").start();//创建一个a线程
        new Thread(myYield,"b").start();//创建一个b线程


    }
}
    class MyYield implements Runnable {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "线程开始");
            Thread.yield();//调用线程的礼让方法
            System.out.println(Thread.currentThread().getName() + "线程结束");
        }
    }

在这里插入图片描述
**

线程jion()方法,强行插队

此线程执行后再执行其他线程,其他的线程处于阻塞的状态

package com.heal.Thread_test;

public class TestJion implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("线程VIP来了"+i);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        //启动vip线程
        TestJion testJion=new TestJion();
        Thread thread1=new Thread(testJion);
        thread1.start();
        
        
        //主线程
        for (int i = 0; i < 1000; i++) {
            if (i==200){
                thread1.join();
            }
            System.out.println("main"+i);

        }
    }
}

运行的结果为:在main199时,开始切换为vip线程,一直执行vip线程直到vip999,再接着main200执行

线程的状态

在这里插入图片描述

在这里插入图片描述

package com.heal.Thread_test;

public class TestState {
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
            for (int i = 0; i < 5; i++) {
                try{
                    Thread.sleep(1000);

                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
            System.out.println("");
        });
//        观察状态
        Thread.State state=thread.getState();
        System.out.println(state);//NEW因为还没有调用start方法

//        观察启动后
        thread.start();//启动线程
        state=thread.getState();//RUN
        System.out.println(state);

        while (state!=Thread.State.TERMINATED){
            Thread.sleep(100);
            state=thread.getState();
            System.out.println(state);
        }

    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值