首先讓我們來(lái)復(fù)習(xí)下一什么是多線程?

  多線程是這樣一種機(jī)制,它允許在程序中并發(fā)執(zhí)行多個(gè)指令流,每個(gè)指令流都稱為一個(gè)線程,彼此間互相獨(dú)立。

  線程又稱為輕量級(jí)進(jìn)程,它和進(jìn)程一樣擁有獨(dú)立的執(zhí)行控制,由操作系統(tǒng)負(fù)責(zé)調(diào)度,區(qū)別在于線程沒(méi)有獨(dú)立的存儲(chǔ)空間,而是和所屬進(jìn)程中的其它線程共享一個(gè)存儲(chǔ)空間,這使得線程間的通信遠(yuǎn)較進(jìn)程簡(jiǎn)單。

  多個(gè)線程的執(zhí)行是并發(fā)的,也就是在邏輯上“同時(shí)”,而不管是否是物理上的“同時(shí)”。如果系統(tǒng)只有一個(gè)CPU,那么真正的“同時(shí)”是不可能的,但是由于CPU的速度非常快,用戶感覺(jué)不到其中的區(qū)別,因此我們也不用關(guān)心它,只需要設(shè)想各個(gè)線程是同時(shí)執(zhí)行即可。

  多線程和傳統(tǒng)的單線程在程序設(shè)計(jì)上最大的區(qū)別在于,由于各個(gè)線程的控制流彼此獨(dú)立,使得各個(gè)線程之間的代碼是亂序執(zhí)行的。

  經(jīng)過(guò)以上介紹,想必大家都已經(jīng)回憶起當(dāng)時(shí)寫多線程程序的痛苦。那么再讓我們回憶一下,Java中是如何實(shí)現(xiàn)多線程的吧。

  作為一個(gè)完全面向?qū)ο蟮恼Z(yǔ)言,Java提供了類 java.lang.Thread 來(lái)方便多線程編程,這個(gè)類提供了大量的方法來(lái)方便我們控制自己的各個(gè)線程。讓我們來(lái)看一看 Thread 類。Thread 類最重要的方法是 run() ,它為Thread 類的方法 start() 所調(diào)用,提供我們的線程所要執(zhí)行的代碼。為了指定我們自己的代碼,只需要覆蓋它!

?1 public ? class ?MyThread? extends ?Thread? {
?2 ???????? private ? int ?index;
?3 ?????????
?4 ???????? public ?MyThread( int ?i)? {
?5 ???????????????? this .index? = ?i;
?6 ????????}

?7 ????????
?8 ???????? public ? void ?run()? {
?9 ???????????????? while ?( this .index? < ? 6 )? {
10 ????????????????????????System.out.println( " index?=? " ? + ? this .index);
11
12 ???????????????????????? this .index ++ ;
13 ????????????????}

14 ????????}

15 ????????
16 ???????? public ? static ? void ?main(String[]?args)? {
17 ???????????????? for ?( int ?i? = ? 0 ;?i? < ? 10 ;?i ++ )? {
18 ???????????????????????? new ?MyThread(i).start();
19 ????????????????}

20 ????????}

21 }

22

  當(dāng)然,除了以上這種方法,Java還提供了Runnable 接口。該接口只有一個(gè)方法 run(),我們聲明自己的類實(shí)現(xiàn) Runnable 接口并提供這一方法,將線程代碼寫入其中,就完成了這一部分的任務(wù)。但是 Runnable 接口并沒(méi)有任何對(duì)線程的支持,我們還必須創(chuàng)建 Thread 類的實(shí)例,這一點(diǎn)通過(guò) Thread 類的構(gòu)造函數(shù)來(lái)實(shí)現(xiàn)。

?1 public ? class ?MyThread? implements ?Runnable? {
?2 ???????? private ? int ?index;
?3 ?????????
?4 ???????? public ?MyThread( int ?i)? {
?5 ???????????????? this .index? = ?i;
?6 ????????}

?7 ????????
?8 ???????? public ? void ?run()? {
?9 ???????????????? while ?( this .index? < ? 6 )? {
10 ????????????????????????System.out.println( " index?=? " ? + ? this .index);
11
12 ???????????????????????? this .index ++ ;
13 ????????????????}

14 ????????}

15 ????????
16 ???????? public ? static ? void ?main(String[]?args)? {
17 ???????????????? for ?( int ?i? = ? 0 ;?i? < ? 10 ;?i ++ )? {
18 ???????????????????????? new ?Thread( new ?MyThread(i)).start();
19 ????????????????}

20 ????????}

21 }

22
23


  我們已經(jīng)習(xí)慣了以上兩種線程的方法,但是,Eclipse的swt卻不相同。如果按照我們以上的方法,當(dāng)訪問(wèn)swt的某一組件時(shí),系統(tǒng)會(huì)拋出異常:org.eclipse.swt.SWTException: Invalid thread access 那么,在swt中,如保使用線程呢?

?1 public ? class ?MyThread? extends ?Thread? {
?2 ???? private ?Display?display;
?3 ???? private ?Label?miniLabel;
?4 ????
?5 ???? private ? static ? int ?index? = ? 0 ;?
?6
?7 ???? public ?MyThread(Display?display,?Label?label)? {
?8 ???????? this .display? = ?display;
?9 ???????? this .miniLabel? = ?label;
10 ????}

11 ????
12 ???? public ? void ?run()? {
13 ???????? try ? {
14 ???????????? while ?( true )? {
15 ????????????????Thread.sleep( 1000 );
16 ???????????????? if ?( ! this .display.isDisposed())? {
17 ????????????????????Runnable?runnable? = ? new ?Runnable()? {
18 ???????????????????????? public ? void ?run()? {
19 ???????????????????????????? // ?your?source
20 ????????????????????????}

21 ????????????????????}
;
22 ????????????????????
23 ????????????????????display.asyncExec(runnable);? // ?關(guān)鍵在這一句上
24 ????????????????}

25 ????????????}

26 ????????}
? catch ?(Exception?ex)? {}
27 ????}

28 }

swt的display有兩種方式實(shí)現(xiàn)線程:asyncExec是線程異步的,syncExec是線程同步的。