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