实现两条线程交替打印奇偶数的两种简单方法
2021-04-19 03:27
标签:turn unlock sync str tac tst notifyall code lock 此方法不容易扩展到多条线程,越多的线程在唤醒时会经历越多的竞争,加大CPU资源浪费,同时也增加耗时 此方法容易扩展到多条线程交替,且为精准唤醒,减少线程竞争带来的消耗 实现两条线程交替打印奇偶数的两种简单方法 标签:turn unlock sync str tac tst notifyall code lock 原文地址:https://www.cnblogs.com/dwwzone/p/13291464.html实现两条线程交替打印奇偶数的两种简单方法
使用Synchronized
public class Main {
private int count = 0;
public static void main(String[] args) throws InterruptedException {
Main main = new Main();
new Thread(()->{
for (int i = 0; i {
for (int i = 0; i
使用ReetrantLock+Condition实现精准唤醒
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Turning {
public static void main(String[] args) throws InterruptedException {
Turning turning = new Turning();
new Thread(()->{
for (int i = 0; i {
for (int i = 0; i
上一篇:二叉树数组实现
文章标题:实现两条线程交替打印奇偶数的两种简单方法
文章链接:http://soscw.com/index.php/essay/76479.html