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()分阶段打断线程;