Why in the following code the output is different when I compile or run it more than once

Posted by Sanjeev on Stack Overflow See other posts from Stack Overflow or by Sanjeev
Published on 2010-04-03T11:24:32Z Indexed on 2010/04/03 11:33 UTC
Read the original article Hit count: 239

Filed under:
|
class Name implements Runnable {
    public void run() {
        for (int x = 1; x <= 3; x++) {
            System.out.println("Run by "
                               + Thread.currentThread().getName()
                               + ", x is " + x);
        }
    }
}
public class Threadtest {
    public static void main(String [] args) {
        // Make one Runnable
        Name nr = new Name();
        Thread one = new Thread(nr);
        Thread two = new Thread(nr);
        Thread three = new Thread(nr);
        one.setName("A");
        two.setName("B");
        three.setName("C");
        one.start();
        two.start();
        three.start();
    }
}

The answer is different while compiling and running more then one time I don't know why? any idea.

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading