实现两个线程交叉打印

实现字母在前数字在后

这个方法真的有很多很多种

1.利用信号量

import java.util.concurrent.*;


/**
 * @Author: 软件171 邱忠玉 201707163
 * @Date: 2020/9/20 10:04
 */
public class Main {

    private static Thread thread1;
    private static Thread thread2 = null;

    public static void main(String[] args) {
        String c = "ABCDEFGHI";
        char[] ca = c.toCharArray();
        String n = "123456789";
        char[] na = n.toCharArray();

        Semaphore semaphore = new Semaphore(0);
        Semaphore semaphore2 = new Semaphore(0);

        thread1 = new Thread(new Runnable() {
            @Override
            public void run() {

                for (char a : ca) {
                    System.out.println(a);
                    semaphore2.release();
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (char a : na) {
                    try {
                        semaphore2.acquire();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(a);
                    semaphore.release();
                }
            }
        });

        thread1.start();
        thread2.start();

    }


}

2.利用阻塞队列

import java.util.concurrent.*;


/**
 * @Author: 软件171 邱忠玉 201707163
 * @Date: 2020/9/20 10:04
 */
public class Main {

    private static LinkedTransferQueue<Character> linkedC = new LinkedTransferQueue<Character>();

    private static LinkedTransferQueue<Character> linkedN = new LinkedTransferQueue<Character>();

    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();


    }


}

3.利用LockSupport


import java.util.concurrent.locks.LockSupport;



/**
 * @Author: 软件171 邱忠玉 201707163
 * @Date: 2020/9/20 10:04
 */
public class Main {

    private static Thread thread1;
    private static Thread thread2 = null;

    public static void main(String[] args) {
        String c = "ABCDEFGHI";
        char[] ca = c.toCharArray();
        String n = "123456789";
        char[] na = n.toCharArray();


        thread1 = new Thread(new Runnable() {
            @Override
            public void run() {

                for (char a : ca) {
                    System.out.println(a);
                    LockSupport.unpark(thread2);
                    LockSupport.park();
                }

            }
        });

        thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (char a : na) {
                    LockSupport.park();
                    System.out.println(a);
                    LockSupport.unpark(thread1);
                }
            }
        });

        thread1.start();
        thread2.start();

    }


}

4.利用Synchronized

import java.util.concurrent.*;



/**
 * @Author: 软件171 邱忠玉 201707163
 * @Date: 2020/9/20 10:04
 */
public class Main {

    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();
    }
}

5.利用Lock

public class Main {


    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) {
                    System.out.print(caa);
                     c1.signal();
                    c2.await();
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });

        Thread t2 = new Thread(() -> {

            try {
                count.await();
                lock.lock();
                for (char naa : na) {                
                    System.out.print(naa);
                    c2.signal();
                    c1.await();
                }


            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

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


}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值