溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中怎么向線程傳遞數(shù)據(jù)

發(fā)布時(shí)間:2021-07-01 16:32:23 來源:億速云 閱讀:125 作者:Leah 欄目:編程語言

Java中怎么向線程傳遞數(shù)據(jù),很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

一、通過構(gòu)造方法傳遞數(shù)據(jù)

在創(chuàng)建線程時(shí),必須要建立一個(gè)Thread類的或其子類的實(shí)例。因此,我們不難想到在調(diào)用start方法之前通過線程類的構(gòu)造方法將數(shù)據(jù)傳入線程。并將傳入的數(shù)據(jù)使用類變量保存起來,以便線程使用(其實(shí)就是在run方法中使用)。下面的代碼演示了如何通過構(gòu)造方法來傳遞數(shù)據(jù):

package mythread;   public class MyThread1 extends Thread  {      private String name;       public MyThread1(String name)      {          this.name = name;      }      public void run()      {          System.out.println("hello " + name);      }      public static void main(String[] args)      {          Thread thread = new MyThread1("world");          thread.start();              }  }

由于這種方法是在創(chuàng)建線程對象的同時(shí)傳遞數(shù)據(jù)的,因此,在線程運(yùn)行之前這些數(shù)據(jù)就就已經(jīng)到位了,這樣就不會造成數(shù)據(jù)在線程運(yùn)行后才傳入的現(xiàn)象。如果要傳遞更復(fù)雜的數(shù)據(jù),可以使用集合、類等數(shù)據(jù)結(jié)構(gòu)。使用構(gòu)造方法來傳遞數(shù)據(jù)雖然比較安全,但如果要傳遞的數(shù)據(jù)比較多時(shí),就會造成很多不便。由于Java沒有默認(rèn)參數(shù),要想實(shí)現(xiàn)類似默認(rèn)參數(shù)的效果,就得使用重載,這樣不但使構(gòu)造方法本身過于復(fù)雜,又會使構(gòu)造方法在數(shù)量上大增。因此,要想避免這種情況,就得通過類方法或類變量來傳遞數(shù)據(jù)。

二、通過變量和方法傳遞數(shù)據(jù)

向?qū)ο笾袀魅霐?shù)據(jù)一般有兩次機(jī)會,***次機(jī)會是在建立對象時(shí)通過構(gòu)造方法將數(shù)據(jù)傳入,另外一次機(jī)會就是在類中定義一系列的public的方法或變量(也可稱之為字段)。然后在建立完對象后,通過對象實(shí)例逐個(gè)賦值。下面的代碼是對MyThread1類的改版,使用了一個(gè)setName方法來設(shè)置name變量:

package mythread;   public class MyThread2 implements Runnable  {      private String name;       public void setName(String name)      {          this.name = name;      }      public void run()      {          System.out.println("hello " + name);      }      public static void main(String[] args)      {          MyThread2 myThread = new MyThread2();          myThread.setName("world");          Thread thread = new Thread(myThread);          thread.start();      }  }

三、通過回調(diào)函數(shù)傳遞數(shù)據(jù)

上面討論的兩種向線程中傳遞數(shù)據(jù)的方法是最常用的。但這兩種方法都是main方法中主動(dòng)將數(shù)據(jù)傳入線程類的。這對于線程來說,是被動(dòng)接收這些數(shù)據(jù)的。然而,在有些應(yīng)用中需要在線程運(yùn)行的過程中動(dòng)態(tài)地獲取數(shù)據(jù),如在下面代碼的run方法中產(chǎn)生了3個(gè)隨機(jī)數(shù),然后通過Work類的process方法求這三個(gè)隨機(jī)數(shù)的和,并通過Data類的value將結(jié)果返回。從這個(gè)例子可以看出,在返回value之前,必須要得到三個(gè)隨機(jī)數(shù)。也就是說,這個(gè)value是無法事先就傳入線程類的。

package mythread;   class Data  {      public int value = 0;  }  class Work  {      public void process(Data data, Integer numbers)      {          for (int n : numbers)          {              data.value += n;          }      }  }  public class MyThread3 extends Thread  {      private Work work;       public MyThread3(Work work)      {          this.work = work;      }      public void run()      {          java.util.Random random = new java.util.Random();          Data data = new Data();          int n1 = random.nextInt(1000);          int n2 = random.nextInt(2000);          int n3 = random.nextInt(3000);          work.process(data, n1, n2, n3);   // 使用回調(diào)函數(shù)          System.out.println(String.valueOf(n1) + "+" + String.valueOf(n2) + "+"                 + String.valueOf(n3) + "=" + data.value);      }      public static void main(String[] args)      {          Thread thread = new MyThread3(new Work());          thread.start();      }  }

在上面代碼中的process方法被稱為回調(diào)函數(shù)。從本質(zhì)上說,回調(diào)函數(shù)就是事件函數(shù)。在Windows API中常使用回調(diào)函數(shù)和調(diào)用API的程序之間進(jìn)行數(shù)據(jù)交互。因此,調(diào)用回調(diào)函數(shù)的過程就是最原始的引發(fā)事件的過程。在這個(gè)例子中調(diào)用了process方法來獲得數(shù)據(jù)也就相當(dāng)于在run方法中引發(fā)了一個(gè)事件。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI