首頁>技術>

如何實現有三個執行緒a,b,c,這三個執行緒都有一個方法分別列印A、B、C,問怎麼能實現列印10次ABC的功能,實現方案包括三種,分別為執行緒池、鎖標誌和join方法。

方法一:執行緒池

public class ThreadTest {    public static void main(String[] args) {        //執行緒池方式        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor() ;        int times = 10 ;        for(int i=0; i<times; i++) {            singleThreadExecutor.execute(new ThreadA());            singleThreadExecutor.execute(new ThreadB());            singleThreadExecutor.execute(new ThreadC());        }        singleThreadExecutor.shutdown();    }    public static class ThreadA implements Runnable {        @Override        public void run() {            System.out.println("A Thread, print A");        }    }    public static class ThreadB implements Runnable {        @Override        public void run() {            System.out.println("B Thread, print B");        }    }    public static class ThreadC implements Runnable {        @Override        public void run() {            System.out.println("C Thread, print C");        }    }}

方案二:ReetrantLock實現

public class ThreadTest2 {    private static Lock lock = new ReentrantLock() ;    private static int state = 0;    public static void main(String[] args) {        new Thread(new ThreadA()).start();        new Thread(new ThreadB()).start();        new Thread(new ThreadC()).start();    }    public static class ThreadA implements Runnable {        @Override        public void run() {            for(int i=0; i<10;) {               try {                   lock.lock();                   if(state % 3 == 0) {                       System.out.println("Thread A: print A");                       state ++ ;                       i++;                   }               } finally {                   lock.unlock();               }            }        }    }    public static class ThreadB implements Runnable {        @Override        public void run() {            for(int i=0; i<10;) {                try {                    lock.lock();                    if(state % 3 == 1) {                        System.out.println("Thread B: print B");                        state ++ ;                        i++;                    }                } finally {                    lock.unlock();                }            }        }    }    public static class ThreadC implements Runnable {        @Override        public void run() {            for(int i=0; i<10;) {                try {                    lock.lock();                    if(state % 3 == 2) {                        System.out.println("Thread C: print C");                        state ++ ;                        i++;                    }                } finally {                    lock.unlock();                }            }        }    }}

方案三:join實現

public class ThreadTest3 {    public static void main(String[] args) throws InterruptedException {        for(int i=0; i<10; i++) {            Thread a = new Thread(new ThreadA()) ;            Thread b = new Thread(new ThreadB()) ;            Thread c = new Thread(new ThreadC()) ;            a.start();            a.join();            b.start();            b.join();            c.start();            c.join();        }    }    public static class ThreadA implements Runnable {        @Override        public void run() {            System.out.println("A Thread, print A");        }    }    public static class ThreadB implements Runnable {        @Override        public void run() {            System.out.println("B Thread, print B");        }    }    public static class ThreadC implements Runnable {        @Override        public void run() {            System.out.println("C Thread, print C");        }    }}

13
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 「精讀系列」API 設計原則