Rumah > Java > JavaAsas > java实现两个线程交替打印

java实现两个线程交替打印

王林
Lepaskan: 2019-12-24 17:56:25
ke hadapan
3426 orang telah melayarinya

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();
    }
}
Salin selepas log masuk

最后输出结果:

88c17746473fa0d7b2a0be3dfe9191a.png

免费学习视频教程推荐:java教学视频

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

实现字母在前数字在后

package com.study.pattern;


import java.util.concurrent.LinkedTransferQueue;

public class Demo3 {
    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();

    }
}
Salin selepas log masuk

输出结果:

85d9de8becb1ef5561c6583890d0723.png

使用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();
    }
}
Salin selepas log masuk

输出结果:

f02ca4dad2d8953328a393a1cab86da.png

使用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();
    }
}
Salin selepas log masuk

输出结果:

36cca51950e18f0d2894eb162d433c3.png

相关文章教程推荐:java零基础入门

Atas ialah kandungan terperinci java实现两个线程交替打印. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:csdn.net
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan