/**
* 两个线程同时执行
*/
private static void demo1(){
Thread A = new Thread(new Runnable() {
@Override
public void run() {
printStr("A");
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
printStr("B");
}
});
A.start();
B.start();
}
/**
* A打印完成后B再打印
*/
private static void demo2(){
Thread A = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("A开始打印...");
printStr("A");
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("B等待A打印完成...");
try {
A.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
printStr("B");
}
});
A.start();
B.start();
}
/**
* A打印1后B打印123,然后A打印23
*/
private static void demo3(){
Object lock = new Object(); // 创建共享锁
Thread A = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("A开始打印...");
System.out.println("A -> 1");
try {
System.out.println("A开始等待");
lock.wait(); // A交出锁的控制权,进入等待状态
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("A -> 2");
System.out.println("A -> 3");
System.out.println("A打印结束");
}
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
System.out.println("B得到锁的控制权,开始打印...");
System.out.println("B -> 1");
System.out.println("B -> 2");
System.out.println("B -> 3");
System.out.println("B打印结束,通知A继续打印");
lock.notify(); // B唤醒A,A继续打印
}
}
});
// 注意:应优先让A拿到共享锁,如果B先于A拿到共享锁,则A线程将一直等待先去
A.start();
B.start();
}
/**
* A,B,C,D四个线程,A,B,C同时执行,全部完成后D线程执行
*/
private static void demo4() {
CountDownLatch countDownLatch = new CountDownLatch(3); // 创建计数器
Thread A = new Thread(new Runnable() {
@Override
public void run() {
printStr("A");
countDownLatch.countDown(); // 线程执行完成,计数器减1
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
printStr("B");
countDownLatch.countDown(); // 线程执行完成,计数器减1
}
});
Thread C = new Thread(new Runnable() {
@Override
public void run() {
printStr("C");
countDownLatch.countDown(); // 线程执行完成,计数器减1
}
});
Thread D = new Thread(new Runnable() {
@Override
public void run() {
try {
countDownLatch.await(); // 计数器为0时线程开始执行
} catch (InterruptedException e) {
e.printStackTrace();
}
printStr("D");
}
});
A.start();
B.start();
C.start();
D.start();
}
/**
* A,B,C全部准备好之后在一起执行
*/
private static void demo5(){
CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
Random random = new Random();
Thread A = new Thread(new Runnable() {
@Override
public void run() {
try {
int i = random.nextInt(4);
long time = (i + 1) * 1000L;
Thread.sleep(time);
System.out.println("A准备就绪,耗时:" + time + "秒,等待开始...");
cyclicBarrier.await(); // 等待其他线程准备就绪
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
long timeMillis = System.currentTimeMillis();
System.out.println("A发车了...,当前时间:" + timeMillis);
}
});
Thread B = new Thread(new Runnable() {
@Override
public void run() {
try {
int i = random.nextInt(4);
long time = (i + 1) * 1000L;
Thread.sleep(time);
System.out.println("B准备就绪,耗时:" + time + "秒,等待开始...");
cyclicBarrier.await(); // 等待其他线程准备就绪
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
long timeMillis = System.currentTimeMillis();
System.out.println("B发车了...,当前时间:" + timeMillis);
}
});
Thread C = new Thread(new Runnable() {
@Override
public void run() {
try {
int i = random.nextInt(4);
long time = (i + 1) * 1000L;
Thread.sleep(time);
System.out.println("C准备就绪,耗时:" + time + "秒,等待开始...");
cyclicBarrier.await(); // 等待其他线程准备就绪
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
long timeMillis = System.currentTimeMillis();
System.out.println("C发车了...,当前时间:" + timeMillis);
}
});
A.start();
B.start();
C.start();
}
/**
* 主线程获取子线程返回的结果
*/
private static void demo6(){
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int total = 0;
System.out.println("子线程开始执行...");
for (int i = 0; i <= 10; i++) {
total += i;
}
System.out.println("子线程执行完成,返回结果");
return total;
}
};
FutureTask<Integer> futureTask = new FutureTask<>(callable);
new Thread(futureTask).start();
System.out.println("主线程执行...");
try {
Integer integer = futureTask.get();
System.out.println("主线程获得的子线程返回的结果为:" + integer);
System.out.println("主线程执行完成");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
private static void printStr(String threadName) {
int i = 1;
System.out.println(threadName + "开始打印...");
while (i <= 3) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(threadName + " -> " + i);
i ++;
}
System.out.println(threadName + "打印结束");
}
线程间通信示例
于 2022-01-19 14:28:28 首次发布