多线程输出方式控制


顺序输出

wait && notify

static boolean t2Runed = false;
    static final  Object o = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (o) {
                while (!t2Runed) {
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("t1");
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (o) {
                System.out.println("t2");
                t2Runed = true;
                o.notify();
            }
        });

        t1.start();
        t2.start();
    }

park && unpark

 public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            System.out.println("t1");
        });
        Thread t2 = new Thread(() -> {
            System.out.println("t2");
            LockSupport.unpark(t1);
        });
        t1.start();
        t2.start();
    }

交替输出 abcabcabcabcabc

ReentrantLock

public class SOutPutLock {
    public static void main(String[] args) {
        AwaitSigan as = new AwaitSigan(5);
        Condition aC = as.newCondition();
        Condition bC = as.newCondition();
        Condition cC = as.newCondition();
        new Thread(() -> {
            as.print(aC, bC, "a");
        }).start();
        new Thread(() -> {
            as.print(bC, cC, "b");
        }).start();
        new Thread(() -> {
            as.print(cC, aC, "c");
        }).start();
        as.start(aC);

    }
}

class AwaitSigan extends ReentrantLock {

    public int loopNumber;

    public AwaitSigan(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public void start(Condition condition) {
        this.lock();
        try {
            System.out.println("start");
            condition.signal();
        }finally {
            this.unlock();
        }
    }

    public void print(Condition cur, Condition next, String string) {
        for (int i = 0; i < loopNumber; i++) {
            this.lock();
            try {
                cur.await();
                System.out.print(string);
                next.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                this.unlock();
            }
        }
    }
}

Park && Unpark

public class SOutPutParkAndUnPark {
    public static void main(String[] args) {
        SyncPark syncPark = new SyncPark(5);
        Thread t1 = new Thread(() -> {
            syncPark.print("a");
        });
        Thread t2 = new Thread(() -> {
            syncPark.print("b");
        });
        Thread t3 = new Thread(() -> {
            syncPark.print("c\n");
        });
        syncPark.setThreads(t1, t2, t3);
        syncPark.start();
    }
}
class SyncPark {
    private int loopNumber;
    private Thread[] threads;
    public SyncPark(int loopNumber) {
        this.loopNumber = loopNumber;
    }
    public void setThreads(Thread... threads) {
        this.threads = threads;
    }
    public void print(String str) {
        for (int i = 0; i < loopNumber; i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(nextThread());
        }
    }
    private Thread nextThread() {
        Thread current = Thread.currentThread();
        int index = 0;
        for (int i = 0; i < threads.length; i++) {
            if(threads[i] == current) {
                index = i;
                break;
            }
        }
        if(index < threads.length - 1) {
            return threads[index+1];
        } else {
            return threads[0];
        }
    }
    public void start() {
        for (Thread thread : threads) {
            thread.start();
        }
        LockSupport.unpark(threads[0]);
    }
}

wait && notify

public class SOutPutWaitAndNotify {
    //线程 1 输出 a 5 次,线程 2 输出 b 5 次,线程 3 输出 c 5 次。现在要求输出 abcabcabcabcabc 怎么实现
    public static void main(String[] args) {
        SyncWaitNotify sy = new SyncWaitNotify(1, 5);
        new Thread(()->{
            sy.print(1, 2, "a");
        }).start();
        new Thread(()->{
            sy.print(2, 3, "b");
        }).start();
        new Thread(()->{
            sy.print(3, 1, "c");
        }).start();
    }
}

class SyncWaitNotify{
    public int flag;
    public int loopNumber;

    public SyncWaitNotify(int flag, int loopNumber) {
        this.flag = flag;
        this.loopNumber = loopNumber;
    }

    public void print(int waitFlag, int nextFlag,String str){
        for (int i = 0; i < loopNumber; i++) {
            synchronized (this) {
                while (flag != waitFlag) {
                    try {
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print(str);
                flag = nextFlag;
                this.notifyAll();
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值