使用java实现两个线程交替打印的多种方法

如何使用java实现两个线程交替打印?相信大部分人都不太了解,今天小编为了让大家更加了解两个线程交替打印是如何实现的,给大家总结了以下内容,跟随小编一起来看看吧。

成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的句容网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

使用ReentrantLock实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo2 {
    private static Lock lock = new ReentrantLock();
    private static Condition c1 = lock.newCondition();
    private static Condition c2 = lock.newCondition();
    private static CountDownLatch count = new CountDownLatch(1);
    public static void main(String[] args) {
       String c = "ABCDEFGHI";
       char[] ca = c.toCharArray();
       String n = "123456789";
       char[] na = n.toCharArray();
       Thread t1 = new Thread(() -> {
           try {
               lock.lock();
               count.countDown();
               for(char caa : ca) {
                   c1.signal();
                   System.out.print(caa);
                   c2.await();
               }
               c1.signal();
           } catch (InterruptedException e) {
               e.printStackTrace();
           } finally {
               lock.unlock();
           }
       });
       Thread t2 = new Thread(() -> {
           try {
               count.await();
               lock.lock();
               for(char naa : na) {
                   c2.signal();
                   System.out.print(naa);
                   c1.await();
               }
               c2.signal();
           } catch (InterruptedException e) {
               e.printStackTrace();
           } finally {
               lock.unlock();
           }
       });
       t1.start();
       t2.start();
    }
}

最后输出结果:

使用java实现两个线程交替打印的多种方法

使用LinkedTransferQueue实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;


import java.util.concurrent.LinkedTransferQueue;

public class Demo3 {
    private static LinkedTransferQueue linkedC = new LinkedTransferQueue();
    private static LinkedTransferQueue linkedN = new LinkedTransferQueue();
    public static void main(String[] args) {
        String c = "ABCDEFGHI";
        char[] ca = c.toCharArray();
        String n = "123456789";
        char[] na = n.toCharArray();
        Thread t1 = new Thread(() -> {
            for(char caa : ca) {
                try {
                    linkedC.put(caa);
                    char a = linkedN.take();
                    System.out.print(a);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for(char naa : na) {
                try {
                    char b = linkedC.take();
                    System.out.print(b);
                    linkedN.put(naa);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();

    }
}

输出结果:

使用java实现两个线程交替打印的多种方法

使用synchronized实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;


import java.util.concurrent.CountDownLatch;

public class Demo4 {
    private static CountDownLatch count = new CountDownLatch(1);
    public static void main(String[] args) {
        String c = "ABCDEFGHI";
        char[] ca = c.toCharArray();
        String n = "123456789";
        char[] na = n.toCharArray();
        Object lock = new Object();
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                count.countDown();
                for(char caa : ca) {
                    System.out.print(caa);
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.notify();
            }
        });
        Thread t2 = new Thread(() -> {
            try {
                count.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock) {
                for(char naa : na) {
                    System.out.print(naa);
                    lock.notify();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                lock.notify();
            }
        });
        t1.start();
        t2.start();
    }
}

输出结果:

使用java实现两个线程交替打印的多种方法

使用LockSupport实现两个线程交替打印

实现字母在前数字在后

package com.study.pattern;


import java.util.concurrent.locks.LockSupport;

public class Demo5 {
    private static Thread t1;
    private static Thread t2;
    public static void main(String[] args) {
        String c = "ABCDEFGHI";
        char[] ca = c.toCharArray();
        String n = "123456789";
        char[] na = n.toCharArray();
        t1 = new Thread(() -> {
            for(char caa : ca) {
                System.out.print(caa);
                LockSupport.unpark(t2);
                LockSupport.park();

            }
        });
        t2 = new Thread(() -> {
            for(char naa : na) {
                LockSupport.park();
                System.out.print(naa);
                LockSupport.unpark(t1);
            }
        });
        t1.start();
        t2.start();
    }
}

输出结果:

使用java实现两个线程交替打印的多种方法

看完上述内容,你们对两个线程交替打印的实现大概了解了吗?如果想了解更多相关文章内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


名称栏目:使用java实现两个线程交替打印的多种方法
分享网址:http://azwzsj.com/article/gdpjhi.html