sleep
sleep 方法是属于 Thread 类中的,sleep 过程中线程不会释放锁,只会阻塞线程,让出cpu给其他线程,但是他的监控状态依然保持着,当指定的时间到了又会自动恢复运行状态,可中断,sleep 给其他线程运行机会时不考虑线程的优先级,因此会给低优先级的线程以运行的机会
示例代码:
/**
* 线程sleep测试
*
* @author supu
* @date 2019-07-01 9:59
**/
public class ThreadSleepTest {
private static final Object obj = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
Thread t2 = new Thread(new MyThread2());
t1.start();
t2.start();
}
static class MyThread1 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread1 start");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread1 end");
}
}
}
static class MyThread2 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread2 start");
System.out.println("thread2 end");
}
}
}
}
运行结果:
wait
wait 方法是属于 Object 类中的,wait 过程中线程会释放对象锁,只有当其他线程调用 notify 才能唤醒此线程。wait 使用时必须先获取对象锁,即必须在 synchronized 修饰的代码块中使用,那么相应的 notify 方法同样必须在 synchronized 修饰的代码块中使用,如果没有在synchronized 修饰的代码块中使用时运行时会抛出IllegalMonitorStateException的异常
示例代码:
package com.springboot.demo.thread;
/**
* 线程wait测试类
*
* @author supu
* @date 2019-06-28 17:35
**/
public class ObjectWaitTest {
private static final Object obj = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
Thread t2 = new Thread(new MyThread2());
t1.start();
t2.start();
}
static class MyThread1 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread1 start");
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread1 end");
}
}
}
static class MyThread2 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread2 start");
obj.notify();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread2 end");
}
}
}
}
运行结果:
yield
和 sleep 一样都是 Thread 类的方法,都是暂停当前正在执行的线程对象,不会释放资源锁,和 sleep 不同的是 yield方法并不会让线程进入阻塞状态,而是让线程重回就绪状态,它只需要等待重新获取CPU执行时间,所以执行yield()的线程有可能在进入到可执行状态后马上又被执行。还有一点和 sleep 不同的是 yield 方法只能使同优先级或更高优先级的线程有执行的机会
示例代码:
package com.springboot.demo.thread;
/**
* yield测试
*
* @author supu
* @date 2019-06-28 18:02
**/
public class ThreadYieldTest {
private static final Object obj = new Object();
public static void main(String[] args) {
Thread t3 = new Thread(new MyThread3());
Thread t1 = new Thread(new MyThread1());
Thread t2 = new Thread(new MyThread2());
t3.start();
t1.start();
t2.start();
}
static class MyThread1 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread1 start");
Thread.yield();
System.out.println("thread1 end");
}
}
}
static class MyThread2 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread2 start");
System.out.println("thread2 end");
}
}
}
static class MyThread3 implements Runnable {
@Override
public void run() {
System.out.println("thread3 start");
Thread.yield();
System.out.println("thread3 end");
}
}
}
运行结果:
join
等待调用join方法的线程结束之后,程序再继续执行,一般用于等待异步线程执行完结果之后才能继续运行的场景。例如:主线程创建并启动了子线程,如果子线程中药进行大量耗时运算计算某个数据值,而主线程要取得这个数据值才能运行,这时就要用到 join 方法了
示例代码:
package com.springboot.demo.thread;
/**
* 线程join测试
*
* @author supu
* @date 2019-07-01 11:01
**/
public class ThreadJoinTest {
private static final Object obj = new Object();
private static int count = 0;
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread1());
Thread t2 = new Thread(new MyThread2());
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("count:" + count);
}
static class MyThread1 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread1 start");
for (int i = 0; i < 10; i++){
count++;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread1 end");
}
}
}
static class MyThread2 implements Runnable {
@Override
public void run() {
synchronized (obj) {
System.out.println("thread2 start");
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++){
count++;
}
System.out.println("thread2 end");
}
}
}
}
运行结果: