java something

          不要以為......很遙遠(yuǎn)
          隨筆 - 23, 文章 - 1, 評(píng)論 - 2, 引用 - 0
          數(shù)據(jù)加載中……

          復(fù)習(xí)下java多線程

          好久沒(méi)搞這個(gè)了,今天把以前的筆記整理下,當(dāng)復(fù)習(xí)。

          Thread類和Runnable接口

          多線程是一個(gè)程序中可以有多段代碼同時(shí)運(yùn)行,那么這些代碼寫在哪里,如何創(chuàng)建線程對(duì)象呢?

              首先,我們來(lái)看Java語(yǔ)言實(shí)現(xiàn)多線程編程的類和接口。在java.lang包中定義了Runnable接口和Thread類。

           

          Runnable接口中只定義了一個(gè)方法:

          ·         public abstract void run()

          這個(gè)方法要由實(shí)現(xiàn)了Runnable接口的類實(shí)現(xiàn)。Runnable對(duì)象稱為可運(yùn)行對(duì)象,一個(gè)線程的運(yùn)行就是執(zhí)行該對(duì)象的run()方法。


                Thread
          類實(shí)現(xiàn)了Runnable接口,因此Thread對(duì)象也是可運(yùn)行對(duì)象。同時(shí)Thread類也是線程類,該類的常用構(gòu)造方法如下:

          ·         public Thread()

          ·         public Thread(Runnable target)

          ·         public Thread(String name)

          ·         public Thread(Runnable target, String name)
          target為線程運(yùn)行的目標(biāo)對(duì)象,即線程調(diào)用start()方法啟動(dòng)后運(yùn)行那個(gè)對(duì)象的run()方法,該對(duì)象的類型為Runnable,若沒(méi)有指定目標(biāo)對(duì)象,則以當(dāng)前類對(duì)象為目標(biāo)對(duì)象,name為線程名


           

            線程的創(chuàng)建 

          介紹下如何創(chuàng)建和運(yùn)行線程的兩種方法。線程運(yùn)行的代碼就是實(shí)現(xiàn)了Runnable接口的類的run()方法或者是Thread類的子類的run()方法,因此構(gòu)造線程體就有兩種方法:
              ·         繼承Thread類并覆蓋它的run()方法;
              ·        
          實(shí)現(xiàn)Runnable接口并實(shí)現(xiàn)它的run()方法。

            1,繼承Thread類創(chuàng)建線程

          通過(guò)繼承Thread類,并覆蓋run()方法,這時(shí)就可以用該類的實(shí)例作為線程的目標(biāo)對(duì)象。下面的程序定義了SimpleThread類,它繼承了Thread類并覆蓋了run()方法。

          程序SimpleThread.java

          public class SimpleThread extends Thread{

            public SimpleThread(String str){

              super(str);

          }

          public void run(){

              for(int i=0; i<100; i++){

                System.out.println(getName()+" = "+ i);

                try{

                   sleep((int)(Math.random()*100));

                }catch(InterruptedException e){}

              }

          System.out.println(getName()+ " DONE");

          }

          }

          _____________________________________________________________________________

              SimpleThread類繼承了Thread類,并覆蓋了run()方法,該方法就是線程體。

          程序 ThreadTest.java

          public class ThreadTest{

            public static void main(String args[]){

              Thread t1 = new SimpleThread("Runner A");

              Thread t2 = new SimpleThread("Runner B");

              t1.start();

              t2.start();

           }

          }

          _____________________________________________________________________________

          ThreadTest類的main()方法中創(chuàng)建了兩個(gè)SimpleThread類的線程對(duì)象并調(diào)用線程類的start()方法啟動(dòng)線程。構(gòu)造線程時(shí)沒(méi)有指定目標(biāo)對(duì)象,所以線程啟動(dòng)后執(zhí)行本類的run()方法。

          注意,實(shí)際上ThreadTest程序中有三個(gè)線程同時(shí)運(yùn)行,在應(yīng)用程序的main()方法啟動(dòng)時(shí),JVM就創(chuàng)建一個(gè)主線程,在主線程中可以創(chuàng)建其他線程。

            2,實(shí)現(xiàn)Runnable接口創(chuàng)建線程

          可以定義一個(gè)類實(shí)現(xiàn)Runnable接口,然后將該類對(duì)象作為線程的目標(biāo)對(duì)象。實(shí)現(xiàn)Runnable接口就是實(shí)現(xiàn)run()方法。

          下面程序通過(guò)實(shí)現(xiàn)Runnable接口構(gòu)造線程體。

          程序 ThreadTest.java

          class T1 implements Runnable{

            public void run(){

              for(int i=0;i<15;i++)

                System.out.println("Runner A="+i);

            }

          }

          class T2 implements Runnable{

            public void run(){

              for(int j=0;j<15;j++)

                System.out.println("Runner B="+j);

            }

          }

          public class ThreadTest{

            public static void main(String args[]){

              Thread t1=new Thread(new T1(),"Thread A");

              Thread t2=new Thread(new T2(),"Thread B");

              t1.start();

              t2.start();

            }

          }

          _____________________________________________________________________________




              

           



          posted on 2011-09-01 20:46 Jamie 閱讀(388) 評(píng)論(0)  編輯  收藏 所屬分類: 多線程


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 嘉善县| 甘肃省| 同江市| 姚安县| 云安县| 赞皇县| 天门市| 台湾省| 邛崃市| 丹棱县| 康定县| 安宁市| 咸阳市| 乐东| 顺义区| 青田县| 金华市| 呼伦贝尔市| 榆社县| 天水市| 荥经县| 广昌县| 英吉沙县| 泽普县| 汝南县| 建瓯市| 彩票| 靖西县| 北安市| 嘉禾县| 定州市| 上杭县| 湖北省| 邻水| 康乐县| 清徐县| 章丘市| 原阳县| 南丰县| 朝阳县| 崇义县|