JAVA多线程学习(关于线程常见方法学习)

join()方法;


Runnable runnable = () -> {
            log.debug("开始");
            Sleeper.sleep(1);
            log.debug("结束");
        };
        Thread thread = new Thread(runnable, "start");
        thread.start();
        thread.join();
log.debug("主线程");
//结果
15:27:55.246 c.CreateThread [start] - 开始
15:27:56.257 c.CreateThread [start] - 结束
15:27:56.257 c.CreateThread [main] - 主线程

join()方法的应用同步;


@Slf4j(topic = "c.CreateThread")
public class TestJoin01 {
    static int b = 0;
    static int c = 0;
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        joins();
    }
    private static void joins() throws InterruptedException {
        Thread thread01 = new Thread(() -> {
            log.debug("thread01");
            Sleeper.sleep(1);
            b = 10;
        });
        Thread thread02 = new Thread(() -> {
            log.debug("thread02");
            Sleeper.sleep(2);
            c = 20;
        });
        thread01.start();
        thread02.start();
        long startTime = System.currentTimeMillis();
        log.debug("thread01等待");
        thread01.join();
        log.debug("thread02等待");
        thread02.join();
        long endTime = System.currentTimeMillis();
        log.debug("b:{} c:{} cost:{}",b,c,endTime-startTime);
    }
}
//结果
15:43:13.912 c.CreateThread [Thread-1] - thread02
15:43:13.912 c.CreateThread [main] - thread01等待
15:43:13.912 c.CreateThread [Thread-0] - thread01
15:43:14.927 c.CreateThread [main] - thread02等待
15:43:15.929 c.CreateThread [main] - b:10 c:20 cost:2020

join()方法的限时同步;


@Slf4j(topic = "c.CreateThread")
public class TestJoin02 {
    static int b = 0;
    static int c = 0;
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        joins();
    }
    private static void joins() throws InterruptedException {
        Thread thread01 = new Thread(() -> {
            log.debug("thread01");
            Sleeper.sleep(1);
            b = 10;
        });
        Thread thread02 = new Thread(() -> {
            log.debug("thread02");
            Sleeper.sleep(2);
            c = 20;
        });
        thread01.start();
        thread02.start();
        long startTime = System.currentTimeMillis();
        log.debug("thread01等待");
        thread01.join();
        log.debug("thread02等待");
        thread02.join(1);
        long endTime = System.currentTimeMillis();
        log.debug("b:{} c:{} cost:{}",b,c,endTime-startTime);
    }
}
//结果
15:48:15.890 c.CreateThread [Thread-1] - thread02
15:48:15.890 c.CreateThread [main] - thread01等待
15:48:15.890 c.CreateThread [Thread-0] - thread01
15:48:16.912 c.CreateThread [main] - thread02等待
15:48:16.926 c.CreateThread [main] - b:10 c:0 cost:1044

interrupt()打断阻塞的方法(阻塞状态中被打断打断标志会改变false);


@Slf4j(topic = "c.TestInterrupt01")
public class TestInterrupt02 {
    //打断阻塞中的线程
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread thread = new Thread(()->{
            log.debug("thread进入睡眠状态");
            Sleeper.sleep(5);
            log.debug("thread结束睡眠状态");
        });
        //启动线程
        thread.start();
        //主线程睡眠
        Sleeper.sleep(2);
        log.debug("interrupt打断");
        thread.interrupt();
        log.debug("打断标记:{}",thread.isInterrupted());
    }
}
//结果
15:55:59.317 c.TestInterrupt01 [Thread-0] - thread进入睡眠状态
15:56:01.316 c.TestInterrupt01 [main] - interrupt打断
15:56:01.318 c.TestInterrupt01 [Thread-0] - thread结束睡眠状态
15:56:01.316 c.TestInterrupt01 [main] - 打断标记:false
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at java.lang.Thread.sleep(Thread.java:340)
    at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
    at cn.itcast.n2.util.Sleeper.sleep(Sleeper.java:8)
    at cn.itcast.n2.TestInterrupt01.lambda$main$0(TestInterrupt01.java:18)
    at java.lang.Thread.run(Thread.java:748)

interrupt()打断正常的方法;


@Slf4j(topic = "c.TestInterrupt02")
public class TestInterrupt02 {
    //打断阻塞中的线程
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Thread thread = new Thread(()->{
            for (;;){

            }
        });
        //启动线程
        thread.start();
        log.debug("interrupt打断");
        thread.interrupt();
        log.debug("打断标记:{}",thread.isInterrupted());
    }
}
//结果//isInterrupted()方法获取打断的状态信息;
15:59:40.136 c.TestInterrupt02 [main] - interrupt打断
15:59:40.140 c.TestInterrupt02 [main] - 打断标记:true

interrupted()分阶段打断线程;



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值