C#,JAVA各版本之Thread.join()詳解

來源:互聯網
上載者:User

      join方法的功能就是使非同步執行的線程變成同步執行。也就是說,當調用線程執行個體的start方法後,這個方法會立即返回,如果在調用start方法後後需要使用一個由這個線程計算得到的值,就必須使用join方法。如果不使用join方法,就不能保證當執行到start方法後面的某條語句時,這個線程一定會執行完。而使用join方法後,直到這個線程退出,程式才會往下執行。下面的代碼示範了2種語言中多線程.join的用法。

      JAVA版本:  

package com.cn.net;public class test {/** * @param args * @throws InterruptedException  */public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stub//線程A        final Thread ThreadA = new Thread(new Runnable() {@Overridepublic void run() { for (int i = 0; i < 10; i++)            { System.out.println("A : " );                if (i == 9)                {                    break;                }                try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}            }}});                        Thread ThreadB = new Thread(new Runnable()  {        @Overridepublic void run() {                    for (int i = 0; i < 5; i++)                    {                    System.out.println("B : ");                        if (i == 4)                        {                            break;                        }                        try {Thread.sleep(1000);}                         catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}                                              }                    //                    try {//ThreadA.join();//在這裡插入線程A//} catch (InterruptedException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}                    for (int i = 0; i < 5; i++)                    {                    System.out.println("C : " );                        if (i == 4)                        {                            break;                        }                        try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}                    }        }                });            ThreadA.start();            ThreadB.start();              }
}

  C#版本:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace Mutiprocess{    class Program    {        static void Main(string[] args)        {            //線程A            Thread ThreadA = new Thread(delegate()            {                for (int i = 0; i < 10; i++)                {                    Console.WriteLine("A : ");                    if (i == 9)                    {                        break;                    }                    Thread.Sleep(1000);                }            });            //線程B            Thread ThreadB = new Thread(delegate()            {                for (int i = 0; i < 5; i++)                {                    Console.WriteLine("B : ");                    if (i == 4)                    {                        break;                    }                    Thread.Sleep(1000);                }                ThreadA.Join();//在這裡插入線程A                for (int i = 0; i < 5; i++)                {                    Console.WriteLine("C : ");                    if (i == 4)                    {                        break;                    }                    Thread.Sleep(1000);                }            });            ThreadA.Start();            ThreadB.Start();      //            前5秒 A B交替出現,然後A出現5次,C出現5次。      //如果注釋掉ThreadA.Join(),那麼結果是前5秒AB 交替出現 後5秒AC交替出現。      //這裡我就不再羅嗦了。還是那樣線程B中join了線程A只有A執行結束,才可以繼續執行線程B中剩下的代碼。      //join其實就是使非同步執行線程轉為同步執行。        }    }}

  2中版本的執行(可能)結果是(cpu決定):

A :
B :
A :
B :
A :
B :
B :
A :
B :
A :
A :
A :
A :
A :
A :
C :
C :
C :
C :
C :

當把ThreadA.Join()  注釋掉後的顯示的規則結果是:

A :
B :
A :
B :
A :
B :
A :
B :
A :
B :
C :
A :
C :
A :
C :
A :
C :
A :
C :
A :

 

更多關於多線程的解釋及例子相見

http://developer.51cto.com/art/200906/132331.htm

http://www.cnblogs.com/qinpengming/archive/2012/05/08/2490665.html

 

 

  

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.