Java 8种线程顺序执行方法(main)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java 8种线程顺序执行方法(main),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5854字,纯文字阅读大概需要9分钟。
内容图文
![Java 8种线程顺序执行方法(main)](/upload/InfoBanner/zyjiaocheng/613/e9c3e22a643644b1880ec8e6831b24f9.jpg)
1、主线程调用join
public class Main1 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程执行完成");
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程执行完成");
}
}
2、主线程、子线程调用join
public class Main2 {
public static void main(String[] args) {
final Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程1执行完成");
});
thread1.start();
final Thread thread2 = new Thread(() -> {
try {
thread1.join();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程2执行完成");
});
thread2.start();
try {
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程执行完成");
}
}
3、wait
public class Main3 {
public static void main(String[] args) {
Object obj = new Object();
final Thread thread1 = new Thread(() -> {
synchronized (obj) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程1执行完成");
obj.notify();
}
});
thread1.start();
synchronized (obj) { // current thread is not owner
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("主线程执行完成");
}
}
4、线程池
public class Main4 {
static ExecutorService executor = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
executor.submit(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程1执行完成");
});
Future<?> future = executor.submit(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程2执行完成");
});
while (!future.isDone()) {
}
System.out.println("主线执行完成");
executor.shutdown();
}
}
5、Condition
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Main5 {
public static void main(String[] args) {
final List<Boolean> flagList = Arrays.asList(false, false);
Lock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
final Thread thread1 = new Thread(() -> {
lock.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程1执行完成");
condition1.signal();
flagList.set(0, true);
lock.unlock();
});
thread1.start();
final Thread thread2 = new Thread(() -> {
lock.lock();
if (!flagList.get(0)) {
try {
condition1.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程2执行完成");
flagList.set(1, true);
condition2.signal();
lock.unlock();
});
thread2.start();
try {
lock.lock();
if (!flagList.get(1)) {
condition2.await();
}
lock.unlock();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程执行完成");
}
}
6、CountDownLatch
import java.util.concurrent.CountDownLatch;
public class Main6 {
public static void main(String[] args) {
// 计数器
CountDownLatch countDownLatch1 = new CountDownLatch(1);
CountDownLatch countDownLatch2 = new CountDownLatch(1);
final Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程1执行完成");
countDownLatch1.countDown();
});
thread1.start();
final Thread thread2 = new Thread(() -> {
try {
countDownLatch1.await();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程2执行完成");
countDownLatch2.countDown();
});
thread2.start();
try {
countDownLatch2.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程执行完成");
}
}
7、CyclicBarrier
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Main7 {
public static void main(String[] args) {
// 回环栅栏
CyclicBarrier cyclicBarrier1 = new CyclicBarrier(2);
CyclicBarrier cyclicBarrier2 = new CyclicBarrier(2);
final Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程1执行完成");
try {
cyclicBarrier1.await(); // 放开栅栏
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
});
thread1.start();
final Thread thread2 = new Thread(() -> {
try {
cyclicBarrier1.await();// 放开栅栏
Thread.sleep(1000);
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("子线程2执行完成");
try {
cyclicBarrier2.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
});
thread2.start();
try {
cyclicBarrier2.await();
} catch (InterruptedException |
BrokenBarrierException e) {
e.printStackTrace();
}
System.out.println("主线程执行完成");
}
}
8、Semaphore
import java.util.concurrent.Semaphore;
public class Main8 {
public static void main(String[] args) {
// 信号量
Semaphore semaphore1 = new Semaphore(0);
Semaphore semaphore2 = new Semaphore(0);
final Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程1执行完成");
semaphore1.release(); // 释放许可
});
thread1.start();
final Thread thread2 = new Thread(() -> {
try {
semaphore1.acquire();
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子线程2执行完成");
semaphore2.release(); // 释放许可
});
thread2.start();
try {
semaphore2.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程执行完成");
}
}
说明:一般测试的时候,还可以让线程休眠等待,但是这种方式控制不是特别好,建议使用上面这些方法。
内容总结
以上是互联网集市为您收集整理的Java 8种线程顺序执行方法(main)全部内容,希望文章能够帮你解决Java 8种线程顺序执行方法(main)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。