0

我正在在线学习 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");
        }
    }
}
4

3 回答 3

7

r1=si; <~~~ 这是怎么回事??*

参考作业。

si您正在创建参考的副本r1。因此,在该语句之后,两者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;
于 2013-07-24T09:52:58.577 回答
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");
           }
于 2013-07-24T09:55:58.137 回答
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();
于 2013-07-24T09:54:09.933 回答