首頁 > Java > Java基礎 > java實作兩個線程交替列印

java實作兩個線程交替列印

王林
發布: 2019-12-24 17:56:25
轉載
3427 人瀏覽過

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實作兩個線程交替列印

免費學習影片教學推薦: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();

    }
}
登入後複製

輸出結果:

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零基礎入門

以上是java實作兩個線程交替列印的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:csdn.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板