首先讓我們來復習下一什么是多線程?

  多線程是這樣一種機制,它允許在程序中并發執行多個指令流,每個指令流都稱為一個線程,彼此間互相獨立。

  線程又稱為輕量級進程,它和進程一樣擁有獨立的執行控制,由操作系統負責調度,區別在于線程沒有獨立的存儲空間,而是和所屬進程中的其它線程共享一個存儲空間,這使得線程間的通信遠較進程簡單。

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

  多線程和傳統的單線程在程序設計上最大的區別在于,由于各個線程的控制流彼此獨立,使得各個線程之間的代碼是亂序執行的。

  經過以上介紹,想必大家都已經回憶起當時寫多線程程序的痛苦。那么再讓我們回憶一下,Java中是如何實現多線程的吧。

  作為一個完全面向對象的語言,Java提供了類 java.lang.Thread 來方便多線程編程,這個類提供了大量的方法來方便我們控制自己的各個線程。讓我們來看一看 Thread 類。Thread 類最重要的方法是 run() ,它為Thread 類的方法 start() 所調用,提供我們的線程所要執行的代碼。為了指定我們自己的代碼,只需要覆蓋它!

?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

  當然,除了以上這種方法,Java還提供了Runnable 接口。該接口只有一個方法 run(),我們聲明自己的類實現 Runnable 接口并提供這一方法,將線程代碼寫入其中,就完成了這一部分的任務。但是 Runnable 接口并沒有任何對線程的支持,我們還必須創建 Thread 類的實例,這一點通過 Thread 類的構造函數來實現。

?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


  我們已經習慣了以上兩種線程的方法,但是,Eclipse的swt卻不相同。如果按照我們以上的方法,當訪問swt的某一組件時,系統會拋出異常: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);? // ?關鍵在這一句上
24 ????????????????}

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

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

28 }

swt的display有兩種方式實現線程:asyncExec是線程異步的,syncExec是線程同步的。