多線程實(shí)現(xiàn)按序打印

實(shí)現(xiàn)方式

1、使用Thread.join()方法;
2、使用計(jì)數(shù)器CountDownLatch
3、使用原子類AtomicInteger
4、使用volatile關(guān)鍵字
5、使用synchronized關(guān)鍵字
6、使用Lock
7、調(diào)用Thread的靜態(tài)方法sleep()-暴力解法

public class KCthreads {
    //怎么保證線程執(zhí)行順序?
    private volatile static int v = 0;  //[解法4使用]
    private static int s = 0;
    public static void main(String[] args) throws InterruptedException {
        //【解法1:】join()方法:將線程并行變?yōu)榇?//        Thread t1 = new Thread(() -> test(), "線程A");
//        Thread t2 = new Thread(() -> test(), "線程B");
//        Thread t3 = new Thread(() -> test(), "線程C");
//        t1.start();t1.join();
//        t2.start();t2.join();
//        t3.start();
        //【解法2:】CountDownLatch計(jì)數(shù)器
//        CountDownLatch count = new CountDownLatch(2);
//        new Thread(() -> {
//            test();
//            count.countDown();
//        }, "線程A").start();
//        new Thread(()->{  //匿名內(nèi)部類,也就是重寫run()方法
//            while (count.getCount() == 2){
//
//            }
//            count.countDown();
//            test();
//        },"線程B").start();
//        new Thread(()->{  //匿名內(nèi)部類,也就是重寫run()方法
//            while (count.getCount() == 1){
//
//            }
//            test();
//        },"線程C").start();
        //【解法3:】AtomicInteger原子類
//        AtomicInteger atomic = new AtomicInteger(0);
//        new Thread(() -> {
//            test();
//            atomic.incrementAndGet();  //自增i++
//        }, "線程A").start();
//        new Thread(()->{  //匿名內(nèi)部類,也就是重寫run()方法
//            while (atomic.get() == 0){
//
//            }
//            atomic.incrementAndGet();
//            test();
//        },"線程B").start();
//        new Thread(()->{  //匿名內(nèi)部類,也就是重寫run()方法
//            while (atomic.get() == 1){
//
//            }
//            atomic.incrementAndGet();
//            test();
//        },"線程C").start();
        //【解法4:】使用volatile關(guān)鍵字
//        new Thread(()->{
//            test();
//            v = 1;
//        },"線程A").start();
//        new Thread(()->{
//            while(v == 0){
//
//            }
//            test();
//            v = 2;
//        },"線程B").start();
//        new Thread(()->{
//            while(v == 1){
//
//            }
//            test();
//        },"線程C").start();
        //【解法5:】使用synchronized關(guān)鍵字
//        new Thread(()->{
//            test();
//            s++;
//        },"線程A").start();
//        new Thread(()->{
//            synchronized (KCthreads.class){
//                while (s == 0){
//
//                }
//                test();
//                s++;
//            }
//        },"線程B").start();
//        new Thread(()->{
//            synchronized (KCthreads.class){
//                while (s == 1){
//
//                }
//                test();
//            }
//        },"線程C").start();
        //【解法6:】使用鎖Lock:ReentrantLock
//        Lock lock = new ReentrantLock();
//        new Thread(()->{
//            test();
//        },"線程A").start();
//        new Thread(()->{
//            lock.lock();
//            test();
//            lock.unlock();
//        },"線程B").start();
//        new Thread(()->{
//            lock.lock();
//            test();
//            lock.unlock();
//        },"線程C").start();
        //【解法7:】最暴力解法:sleep
        Thread t1 = new Thread(() -> test(), "線程A");
        Thread.sleep(1000);
        t1.start();
        Thread t2 = new Thread(() -> test(), "線程B");
        Thread.sleep(1000);
        t2.start();
        Thread t3 = new Thread(() -> test(), "線程C");
        Thread.sleep(1000);
        t3.start();
    }

    private static void test() {
        System.out.println(Thread.currentThread().getName());
    }

    //打印結(jié)果:順序打印
    // 線程A
    // 線程B
    // 線程C
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 這篇文章主要是對(duì)多線程的問(wèn)題進(jìn)行總結(jié)的,因此羅列了100個(gè)多線程的問(wèn)題。 這些多線程的問(wèn)題來(lái)源于各大網(wǎng)站,可...
    程序員白楠楠閱讀 2,484評(píng)論 1 23
  • 并發(fā) 如何減少上下文切換 無(wú)鎖并發(fā)編程:將數(shù)據(jù)ID按照Hash算法取模分段,不同線程處理不同段數(shù)據(jù) CAS算法:J...
    Hengtao24閱讀 932評(píng)論 0 2
  • Java多線程編程實(shí)戰(zhàn)指南 核心篇 Thread類的start方法作用是啟動(dòng)相應(yīng)的線程。啟動(dòng)一個(gè)線程的實(shí)質(zhì)是請(qǐng)求J...
    縱橫Top閱讀 532評(píng)論 0 2
  • 前言 1. 基本介紹 在我學(xué)習(xí) Android 多線程優(yōu)化方法的過(guò)程中,發(fā)現(xiàn)我對(duì)多線程優(yōu)化的了解太片面。 寫這篇文...
    燈不利多閱讀 1,971評(píng)論 5 6
  • 表情是什么,我認(rèn)為表情就是表現(xiàn)出來(lái)的情緒。表情可以傳達(dá)很多信息。高興了當(dāng)然就笑了,難過(guò)就哭了。兩者是相互影響密不可...
    Persistenc_6aea閱讀 129,644評(píng)論 2 7

友情鏈接更多精彩內(nèi)容