线程交替输出(你能想出几种方法)

 前提

关于线程的解决思路,最好不要从时间角度,优先级角度考虑。一般不要使用sleep和join。

LockSupport

LockSupport.unpark();
LockSupport.park();
public class A {
    static Thread t1 = null;
    static Thread t2 = null;
    public static void main(String[] args) {
        String[] a = {"1","2","3","4","5"};
        String[] b = {"a","b","c","d","e"};
        t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < a.length; i++) {
                    System.out.println(a[i]);
                    LockSupport.unpark(t2);
                    LockSupport.park();
                }
            }
        });
        t2 = new Thread(()->{
            for (int i = 0; i < b.length; i++) {
                LockSupport.park();
                System.out.println(b[i]);
                LockSupport.unpark(t1);
            }
        });
        t1.start();
        t2.start();
    }
}

ReentrantLock

针对线程通信这块,比起使用wait和notify更灵活,因为可以指定多个condition

lock.newCondition()
condition.signal();
condition.await();

 

public class C {
    static CountDownLatch latch = new CountDownLatch(1); //可以指定谁先执行时使用,具体看B类
    static ReentrantLock lock = new ReentrantLock();
    static Condition condition1 = lock.newCondition();
    static Condition condition2 = lock.newCondition();
    public static void main(String[] args) {
        String[] a = {"1","2","3","4","5"};
        String[] b = {"a","b","c","d","e"};
        C c = new C();
        new Thread(()->{
            c.t1(a);
        }).start();
        new Thread(()->{
            c.t2(b);
        }).start();
    }
    void t1(String[] a) {
        lock.lock();
        for (int i = 0; i < a.length; i++) {
            try {
                System.out.println(a[i]);
                condition1.signal();
                condition2.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
            condition1.signal();
        }
        lock.unlock();
    }
    void t2(String[] b){
        lock.lock();
        for (int i = 0; i < b.length; i++) {
            try {
                System.out.println(b[i]);
                condition2.signal();
                condition1.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            condition2.signal();
        }
        lock.unlock();
    }
}

 在这里可以看知道两个线程确实交替执行了(暂时没有控制线程先后顺序),但是,如果我想控制让哪个线程先执行,需要怎么做?想一想,在使用wait和notify的案例中给出答案。

wait、notify

synchronized
notify();
wait();

 可以看一下如何控制哪个线程先输出

public class B {
    static CountDownLatch latch = new CountDownLatch(1);
    public static void main(String[] args) {
        String[] a = {"1","2","3","4","5"};
        String[] b = {"a","b","c","d","e"};
        B b1 = new B();

        new Thread(()->{
            b1.t1(a);
        }).start();
        new Thread(()->{
            b1.t2(b);
        }).start();
    }
    void t1(String[] a) {
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (this){
            for (int i = 0; i < a.length; i++) {
                try {
                    System.out.println(a[i]);
                    notify();
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            notify();
        }
    }
    void t2(String[] b){
        synchronized (this){
            for (int i = 0; i < b.length; i++) {
                try {
                    System.out.println(b[i]);
                    latch.countDown();
                    notify();
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            notify();
        }
    }
}

LinkedTransferQueue

LinkedTransferQueue是一个由链表结构组成的无界阻塞TransferQueue队列。

LinkedTransferQueueSynchronousQueueLinkedBlockingQueue 的合体,性能比 LinkedBlockingQueue 更高(没有锁操作),比 SynchronousQueue能存储更多的元素。

相对于其他阻塞队列,LinkedTransferQueue多了tryTransfer和transfer方法。

transfer:调用此方法后要等到有消费者获取此元素之后才返回,不然就一直阻塞。

take和poll: 如果为空就一直阻塞.

 

public class D {
    public static void main(String[] args) {
        String[] a = {"1","2","3","4","5"};
        String[] b = {"a","b","c","d","e"};
        LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();
        new Thread(()->{
            try {
                for (int i = 0; i < a.length; i++) {
                    System.out.println(queue.take());
                    queue.transfer(a[i]);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < b.length; i++) {
                try {
                    queue.transfer(b[i]);
                    System.out.println(queue.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值