Thread类的JOIN方法
适用于线程较少的场景。
实例代码:
public class JoinOfThread {
public static void main(String[] args) throws Exception {
Thread threadA = new Thread(() -> {
System.out.println("我第一个执行");
});
Thread threadB = new Thread(() -> {
System.out.println("我第二个执行");
});
Thread threadC = new Thread(() -> {
System.out.println("我最后执行");
});
threadA.start();
threadA.join();
threadB.start();
threadC.join();
threadC.start();
// 第二种join方法
// Thread threadB = new Thread(() -> {
// try {
// threadA.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("我第二个执行");
// });
//
// Thread threadC = new Thread(() -> {
// try {
// threadB.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("我最后执行");
// });
//
// threadA.start();
// threadB.start();
// threadC.start();
}
}
利用volatile实现多线程的有序执行
volatile的含义介绍: volatile.
public class Demo02 {
static volatile int order = 0;
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
while (true){
if (order == 0){
System.out.println("我第一个执行");
order++;
}
// 只循环一次
break;
}
});
Thread threadB = new Thread(() -> {
while (true){
if (order == 1){
System.out.println("我第二个执行");
order++;
break;
}
}
});
Thread threadC = new Thread(() -> {
while (true){
if (order == 2){
System.out.println("我最后执行");
order = 0;
break;
}
}
});
threadA.start();
threadB.start();
threadC.start();
}
}
synchronized + wait + notify
public class Demo03{
public static void main(String[] args) {
final Object synObj = new Object();
final Object synObj2 = new Object();
Thread t1 = new Thread(() -> {
synchronized(synObj) {
try {
synObj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我最后执行");
}
});
t1.start();
Thread t2 = new Thread(() -> {
synchronized(synObj) {
synchronized (synObj2){
try {
synObj2.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("我第二个执行");
}
synObj.notify();
}
});
t2.start();
Thread t3 = new Thread(() -> {
synchronized(synObj2) {
System.out.println("我第一个执行");
synObj2.notify();
}
});
t3.start();
}
}
Cadition+Lock
public class Test {
public static Lock lock = new ReentrantLock();
public static int count = 0;
public static Condition conditionA = lock.newCondition();
public static Condition conditionB = lock.newCondition();
public static void main(String[] args) {
Thread t1 = new Thread() {
@Override
public void run() {
lock.lock();
if (count < 5) {
System.out.println("线程1未达到业务要求,暂停中,等待线程2处理到达到要求后唤醒");
try {
conditionA.await();// 暂停线程并释放锁
System.out.println("conditionA被唤醒");
conditionB.await();
System.out.println("conditionB被唤醒");
System.out.println("我是线程1后面的代码");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.unlock();
}
};
Thread t2 = new Thread() {
@Override
public void run() {
lock.lock();
while (count < 10) {
count++;
System.out.println("线程2业务处理中: " + count);
try {
Thread.sleep(1000);
if (count == 5) {
conditionA.signal();
System.out.println("唤醒线程1");
lock.unlock();// 调用signal()方法后,线程2并不会释放锁,需要手动释放线程2才会执行
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
lock.lock();// 不加这个会报java.lang.IllegalMonitorStateException
System.out.println("等待3秒后conditionB会被唤醒");
Thread.sleep(3000);
conditionB.signal();
} catch (InterruptedException e) {
e.printStackTrace();
}
lock.unlock();// 这里释放锁,线程2执行完,线程1才会执行
}
};
t1.start();
t2.start();
}
}
CountDownLatch
public class Demo04 {
static CountDownLatch count = new CountDownLatch(1);
public static void main(String[] args) {
Thread thread2 = new Thread(() -> {
try {
count.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1");
});
thread2.start();
Thread thread1 = new Thread(() -> {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("0");
count.countDown();
});
thread1.start();
}
}