2013-07-24 27 views
0

我正在學習Java中的線程,我是初學者,我在理解某些概念時遇到困難。誰能幫我?在Java中學習線程,發現難點了解某些方面

鏈接我從學習:http://www.codeproject.com/Articles/616109/Java-Thread-Tutorial

問題:

public class Core { 
    public static void main(String[] args) { 
     Runnable r0,r1;//pointers to a thread method 
     r0=new FirstIteration("Danial"); //init Runnable, and pass arg to thread 1 
     SecondIteration si=new SecondIteration(); 
     si.setArg("Pedram");// pass arg to thread 2 

     r1=si; //<~~~ What is Happening here?? 

     Thread t0,t1; 
     t0=new Thread(r0); 
     t1=new Thread(r1); 
     t0.start(); 
     t1.start(); 
     System.out.print("Threads started with args, nothing more!\n"); 
    } 
} 

編輯:FirstIteration的代碼和SceondIteration

class FirstIteration implements Runnable{ 
    public FirstIteration(String arg){ 
     //input arg for this class, but in fact input arg for this thread. 
     this.arg=arg; 
    } 

    private String arg; 

    @Override 
    public void run() { 
     for(int i=0;i<20;i++){ 
     System.out.print("Hello from 1st. thread, my name is "+ 
      arg+"\n");//using the passed(arg) value 
     } 
    } 
} 

class SecondIteration implements Runnable{ 
    public void setArg(String arg){//pass arg either by constructors or methods. 
     this.arg=arg; 
    } 

    String arg; 

    @Override 
    public void run() { 
     for(int i=0;i<20;i++){ 
      System.out.print("2)my arg is="+arg+", "+i+"\n"); 
     } 
    } 
} 
+0

這是什麼問題? – MaVRoSCy

+1

請通過代碼,你會得到我的問題。 – user2613996

+0

向我們展示了FirstIteration和SecondIteration的代碼 – fGo

回答

7

R1 = SI; < ~~~這裏發生了什麼? *

參考分配。

您正在爲r1創建參考文獻si的副本。因此,在該聲明之後,r1si都將引用同一個對象。

  ______________________ 
si ------> |new SecondIteration();| // You have 1 reference to this object 
      ------------------------ 

r1 = si; // Make Runnable reference `r1` point the same object that `si` refers 

      ______________________ 
si ------> |new SecondIteration();| // Now you have 2 reference to this object 
      ------------------------ 
      ^
      | 
r1 ---------- 

其實,他們可以簡單地做:

r1 = new SecondIteration(); 

,而不是這兩個步驟。但是,由於setArg()方法沒有在Runnable接口中聲明。因此,調用它,你就必須做如下類型轉換

((SecondIteration)r1).setArg(); 

那可能不會有太大的可讀性,這就是爲什麼他們大概分這兩個步驟:

SecondIteration si = new SecondIteration(); 
si.setArg("Pedram"); 
r1=si; 
+0

但ri是一個線程方法指針,這就是作者告訴我們的。這項任務的意義是什麼? – user2613996

+0

@ user2613996。 'r1'是'Runnable'類型的引用。我不明白,你的意思是*線程方法指針*? –

+0

@ user2613996在java中沒有像_pointer到methods_這樣的東西 – fGo

0
r1=si; // <~~~ What is Happening here??*** 

沒什麼。

它們只是將存儲在局部變量s1中的對象分配給另一個局部變量r1。

不知道爲什麼,除了「代碼接口」(r1是一個Runnable,不再是更具體的SecondIteration)。

如果我想這樣做(隱藏哪些Runnable接口實現它的細節),我會更進一步,並使用本地範圍,因此SI完全消失:

  Runnable r0=new FirstIteration("Danial"); 
      Thread t0=new Thread(r0); 

      Thread t1; 
      { 
       SecondIteration si=new SecondIteration(); 
       si.setArg("Pedram");// pass arg to thread 2 
       t1 = new Thread(si); 
      } 


      t0.start(); 
      t1.start(); 
+0

謝謝,我弄明白了作者最初引用的線程方法指針的邏輯。 – user2613996

0

現在你會理解它。

public static void main(String[] args) { 
      Runnable r0 =new FirstIteration("Danial"); //init Runnable, and pass arg to thread 1 
      SecondIteration s1=new SecondIteration(); 
      s1.setArg("Pedram");// pass arg to thread 2 

      Thread t0 = new Thread(r0); 
      Thread t1 = new Thread(s1); 
      t0.start(); 
      t1.start(); 
      System.out.print("Threads started with args, nothing more!\n"); 
      } 
+0

謝謝你,我已經得到這個社區的熱烈響應! – user2613996

+0

'si.setArg' - >'si'在哪裏? –

+0

r1 = si只是對新分配的對象分配 – MayurB