线程的三个方法

SLEEP

sleep 意思是睡眠,当前线程暂停一段时间让给别的线程去运行。sleep是怎么复活的?由你的睡眠时间而定,等睡眠到规定的时间自动复活,CPU 没有线程的概念,会不断的从内存中获取指令去执行,睡眠的意思就是当前线程让出cpu 由其他线程去执行。

 static void testSleep() {
        new Thread(()->{
            for(int i=0; i<100; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(500);
                    //TimeUnit.Milliseconds.sleep(500)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void main(String[] args) throws InterruptedException {
         sleep();
//        yield();
   //      join();
    }

YIELD

yield 让出当前cpu进入线程的等待队列,下一次能不能进入线程看自己能不能抢到,当yield让出cpu后后续更大可能是将等待队列里面的线程拿过来进行执行。暂时没有应用场景。

    static void yield(){
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    if (i % 10 == 0) {
                        Thread.yield();
                    }
                    System.out.println("i am thread a");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    if (i % 10 == 0) {
                        Thread.yield();
                    }
                    System.out.println("i am thread b");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static void main(String[] args) throws InterruptedException {
//        sleep();
         yield();
         //join();
    }

JOIN

在当前线程加入你调用join的线程,此处是t1 线程加入到t2线程里面去了,当t2线程执行到 join的时候,就会去执行t1线程,只有t1 线程执行完成之后才会接着执行t2线程。

   static void join() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                    System.out.println("i am thread a");
                    TimeUnit.SECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                try {
                     System.out.println("i am thread b");
                      t1.join();
                      System.out.println("i am thread b");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        TimeUnit.SECONDS.sleep(10);
        t2.start();
    }

    public static void main(String[] args) throws InterruptedException {
//        sleep();
//        yield();
         join();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值