您好,欢迎来到纷纭教育。
搜索
您的当前位置:首页【面试:并发篇07:多线程:一些方法的注意点】

【面试:并发篇07:多线程:一些方法的注意点】

来源:纷纭教育

【面试:并发篇07:多线程:一些方法的注意点】

00.前言

有任何问题请指出,感谢。

01.start 与 run

我们知道 start方法是运行Thread里的run方法,那么我们之间调用run方法,这两者之前的区别是什么

例子
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test")
public class Test {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                log.debug(Thread.currentThread().getName());
            }
        };
        t1.start();
        t1.sleep(100);
        t1.run();
    }
}

结果

可以看出 直接调用 run 是在主线程中执行了 run,没有启动新的线程 使用 start 是启动新的线程,通过新的线程间接执行 run 中的代码

02. sleep 与 yield

sleep与yield的区别

sleep

yield

  1. 调用 yield 会让当前线程从 Running 进入 Runnable 就绪状态,然后调度执行其它线程
  2. 具体的实现依赖于操作系统的任务调度器

sleep状态

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test6")
public class Test6 {

    public static void main(String[] args) {
        Thread t1 = new Thread("t1") {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        t1.start();
        log.debug("t1 state: {}", t1.getState());

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug("t1 state: {}", t1.getState());
    }
}

结果

18:53:22.128 c.Test6 [main] - t1 state: RUNNABLE
18:53:22.633 c.Test6 [main] - t1 state: TIMED_WAITING

刚开始调用的时候t1状态为运行态,之后当t1线程调用sleep时 主线程查看t1的状态为TIMED_WAITING(阻塞)

yield状态

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.TestYield")
public class TestYield {
    public static void main(String[] args) {
        Runnable task1 = () -> {
            System.out.println("---->1 ");
        };
        Runnable task2 = () -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
            System.out.println("              ---->2 ");

        };
        Thread t1 = new Thread(task1, "t1");
        Thread t2 = new Thread(task2, "t2");
        t1.start();
        log.debug("t2 state: {}", t2.getState());
        t2.start();
        log.debug("t2 state: {}", t2.getState());

    }

}

结果

---->1
00:28:50.450 c.TestYield [main] - t2 state: NEW
00:28:50.454 c.TestYield [main] - t2 state: RUNNABLE
---->2

可以看出t2线程yield让步后就变为了RUNNABLE就绪态

补充:

yield并不是一定会让步,它的原理是把自己让出 然后变成就绪态 然后和其它线程再争抢cpu,所以有可能yield后还是此线程。

例子

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test9")
public class Test9 {

    public static void main(String[] args) {
        Runnable task1 = () -> {
            int count = 0;
            for (;;) {
                System.out.println("---->1 " + count++);
            }
        };
        Runnable task2 = () -> {
            int count = 0;
            for (;;) {
                Thread.yield();
                System.out.println("              ---->2 " + count++);
            }
        };
        Thread t1 = new Thread(task1, "t1");
        Thread t2 = new Thread(task2, "t2");

        t1.start();
        t2.start();
    }
}

结果

---->1 16193
---->1 16194
---->1 16195
---->1 16196
---->1 16197
---->1 16198
---->1 16199
---->1 16200
---->1 16201
---->1 16202
---->1 16203
---->1 16204
---->1 16205
---->1 16206
---->1 16207
---->1 16208
---->1 16209
---->1 16210
---->1 16211
---->2 4458
---->2 4459
---->2 4460
---->2 4461
---->2 4462
---->2 4463
---->2 44
---->2 4465
---->2 4466
---->2 4467

可以看出yield让步大概率是 让步到其它线程,但并不代表此线程就一定会让步。

再补充:Priority(优先级)

线程优先级会提示调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略它 如果 cpu 比较忙,那么优先级高的线程会获得更多的时间片,但 cpu 闲时,优先级几乎没作用。可以说它和yield一样 都是不确定是否一定会改变当前线程。

例子

package cn.itcast.test;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test9")
public class Test9 {

    public static void main(String[] args) {
        Runnable task1 = () -> {
            int count = 0;
            for (;;) {
                System.out.println("---->1 " + count++);
            }
        };
        Runnable task2 = () -> {
            int count = 0;
            for (;;) {
                System.out.println("              ---->2 " + count++);
            }
        };
        Thread t1 = new Thread(task1, "t1");
        Thread t2 = new Thread(task2, "t2");
        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
    }
}

结果

---->1 61594
---->1 61595
---->1 61596
---->1 61597
---->1 61598
---->1 61599
---->1 61600
---->1 61601
---->1 61602
---->1 61603
---->1 61604
---->2 459942
---->2 459943
---->2 459944
---->2 459945
---->2 459946
---->2 459947
---->2 459948
---->2 459949
---->2 459950
---->2 459951
---->2 459952
---->2 459953
---->2 459954
---->2 459955
---->2 459956
---->2 459957
---->2 459958
---->2 459959
---->2 459960
---->2 459961
---->2 459962
---->2 459963
---->2 4599

可以看出虽然t2的优先级为MAX_PRIORITY(优先级为10) t1的优先级为MIN_PRIORITY(优先级为1),但是仍然会出现t1不断输出的情况,说明了 优先级只是作为参考 真正决定执行什么线程的是cpu。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fenyunshixun.cn 版权所有 湘ICP备2023022495号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务