学习Java中的线程,在理解某些方面时遇到困难

时间:2013-07-24 09:50:05

标签: java multithreading

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

3 个答案:

答案 0 :(得分:7)

  

R1 = SI; &lt; ~~~这里发生了什么? *

参考作业。

您正在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;

答案 1 :(得分:0)

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

没什么。

它们只是将存储在局部变量s1中的对象分配给另一个局部变量r1。

不知道为什么,除了可能是#34;代码接口&#34; (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();

答案 2 :(得分: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");
           }