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();
    }
posted on 2021-06-30 21:31  XIN1024  阅读(48)  评论(0)    收藏  举报