java多線程通訊

來源:互聯網
上載者:User

標籤:執行   方法   count   cut   future   one   trace   介紹   同步代碼塊   

最近在研究java多線程,這篇文章主要是介紹一些線程之間的通訊:

1:join 的方式,一個線程等待另一個線程執行完畢後在執行,可以控制線程執行的順序;

情境:B線程要在A線程完成後才開始任務:

不做任何控制的情況下的線程代碼如下:

@Test    public void threadTest4() throws InterruptedException, ExecutionException {//        線程A        final Thread threadA = new Thread(new Runnable() {            @Override            public void run() {                printNum("線程A");            }        });//        線程B        Thread threadB= new Thread(new Runnable() {            @Override            public void run() {//                try {//                    threadA.join();//                } catch (InterruptedException e) {//                    e.printStackTrace();//                }                printNum("線程B");            }        });        threadA.start();        threadB.start();        Thread.sleep(1000);    }
private void printNum(String threadName){        int i=0;        while (i++<3){            try {                Thread.sleep(10);            } catch (InterruptedException e) {                e.printStackTrace();            }            System.out.println(threadName+"列印"+i);        }    }

這樣列印出來的效果如下:

線程B列印1線程A列印1線程B列印2線程A列印2線程B列印3線程A列印3

這樣不能保證 B 線程在A 線程執行完之後再執行;可以通過 join 方法來實現我們的需求: 當在 B 線程調用 A線程的join 方法 則會 B 線程等待A線程執行完了之後再執行B 線程;將上面注掉的代碼解開就行了;

這樣列印出來的效果是:

線程A列印1線程A列印2線程A列印3線程B列印1線程B列印2線程B列印3

這樣就能保證 B 線程在 A線程執行結束後再執行;

2:多個線程按照一定的順序交叉執行:

情境:A 線程執行列印完 1 2 後 B 線程再執行列印 1 2 3 

這樣的情境需要使用 鎖的等待和喚醒的機制來實現,代碼實現如下:  需要用到兩個方法 wait  和 notify 方法  這兩個方法都是 Object對象的方法;

@Test    public void threadTest5() throws InterruptedException, ExecutionException {        final Object o=new Object();        Thread threadA = new Thread(new Runnable() {            @Override            public void run() {                synchronized (o){                    System.out.println("線程A 列印 1");                    try {                        o.wait();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("線程A 列印 2");                    System.out.println("線程A 列印 3");                }            }        });        Thread threadB = new Thread(new Runnable() {            @Override            public void run() {                synchronized (o){                    System.out.println("線程B 列印 1");                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("線程B 列印 2");                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println("線程B 列印 3");                    o.notify();                }            }        });        threadA.start();        threadB.start();        Thread.sleep(1000);    }

下面分析這段代碼的執行順序:

1:建立對象鎖 

final Object o=new Object();

2:A 首先獲得對象鎖的控制權;

3:A 調用  wait 方法 讓出對象鎖的控制權:

o.wait();

4:B 線程獲得對象鎖的控制權:

B線程的業務代碼處理完之後 調用 notify 方法,喚醒 正在 wait  的線程 然後結束B線程的同步代碼塊,

5:A 線程擷取到了對象鎖的控制權後執行自己的商務邏輯;

這樣就滿足我們需要的情境;

3: 四個線程 A B C D,其中 D 要等到 A B C 全執行完畢後才執行,而且 A B C 是同步啟動並執行

通過 調用對象鎖的 notify 和 wait 方法可以滿足線程的執行順序 但是線程是一次執行的,不能同時進行;

需要同步進行有需要進行控制線程的執行順序則可以使用 執行緒計數器來實現  

代碼如下:

@Test    public void threadTest1() throws InterruptedException {        int worker = 3;        System.out.println("計數器的值為:" + worker);        final CountDownLatch countDownLatch = new CountDownLatch(worker);        Thread threadD = new Thread(new Runnable() {            @Override            public void run() {                System.out.println("D 線程等待其他線程!");                try {                    countDownLatch.await();                    System.out.println("其他線程運行結束,D線程開始");                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        });        threadD.start();        for (int i = 0; i < 3; i++) {            final int finalI = i;            Thread threadA = new Thread(new Runnable() {                @Override                public void run() {                    System.out.println(Thread.currentThread().getName() + finalI + "is working");                    try {                        Thread.sleep(100);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                    System.out.println(Thread.currentThread().getName() + finalI + "is finish");                    countDownLatch.countDown();                }            });            threadA.start();        }        Thread.sleep(1000);    }

  上面代碼的執行順序如下:

1:建立執行緒計數器:計數器的計數個數為3;

2:當D線程開始執行的時候調用計數器的 await 方法,然後等待;

3:執行 ABC 線程的商務邏輯的處理,線上程的商務邏輯處理之後分別調用 計數器的 數字減1.

4:當計數器的數值為0 時D線程獲得執行權,開始執行;

 

4:多線程擷取線程處理的傳回值:

代碼如下:

@Test    public void threadTest3() throws InterruptedException, ExecutionException {        Callable<Integer> callable = new Callable<Integer>() {            @Override            public Integer call() throws Exception {                System.out.println("開始任務!!");                Thread.sleep(100);                int result=0;                for (int i = 0; i <100 ; i++) {                    result +=i;                }                return result;            }        };        FutureTask<Integer> futureTask = new FutureTask<Integer>(callable);        new Thread(futureTask).start();        System.out.println("任務擷取前");        System.out.println("任務擷取到的結果是:"+futureTask.get());        System.out.println("任務擷取後");        Thread.sleep(1000);    }

通過Callable  和 FutureTask 兩類可以實現這個功能, 注意 FutureTask  的 get()方法是同步,必須在callable中的call 方法執行結束後;


 

 


 

 

java多線程通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.