Note that there are some explanatory texts on larger screens.

plurals
  1. POLearning threads in Java, finding difficulty in Understanding some aspects
    primarykey
    data
    text
    <p>I am learning Threads in Java online and I am a beginner, I am facing difficulty in understanding some concept. Can anyone help me? </p> <p>Link I am learning from:<a href="http://www.codeproject.com/Articles/616109/Java-Thread-Tutorial" rel="nofollow">http://www.codeproject.com/Articles/616109/Java-Thread-Tutorial</a></p> <p>Problem:</p> <pre><code>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; //&lt;~~~ 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"); } } </code></pre> <p>EDIT: Code of FirstIteration and SceondIteration</p> <pre><code>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&lt;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&lt;20;i++){ System.out.print("2)my arg is="+arg+", "+i+"\n"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload