Java Programming - Threads - Discussion

Discussion Forum : Threads - Finding the output (Q.No. 4)
4.
What will be the output of the program?
class s1 implements Runnable 
{ 
    int x = 0, y = 0; 
    int addX() {x++; return x;} 
    int addY() {y++; return y;} 
    public void run() { 
    for(int i = 0; i < 10; i++) 
        System.out.println(addX() + " " + addY()); 
} 
    public static void main(String args[]) 
    { 
        s1 run1 = new s1(); 
        s1 run2 = new s1(); 
        Thread t1 = new Thread(run1); 
        Thread t2 = new Thread(run2); 
        t1.start(); 
        t2.start(); 
    } 
}
Compile time Error: There is no start() method
Will print in this order: 1 1 2 2 3 3 4 4 5 5...
Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)
Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...
Answer: Option
Explanation:

Both threads are operating on different sets of instance variables. If you modify the code of the run() method to print the thread name it will help to clarify the output:

public void run()
{
for(int i = 0; i < 10; i++)

System.out.println(
Thread.currentThread().getName() + ": " + addX() + " " + addY()
);

}
Discussion:
13 comments Page 1 of 2.

Gunasekaran said:   5 years ago
Threads will executes in any order we cannot except this order execute.

Raghu said:   6 years ago
I cannot understand the program please explain in detail.

Ramesh said:   6 years ago
Here if we will use synchronized to run method then we will get the result as option B. Because one thread will fully complete the loop then the other will access run method.

Saurabh said:   6 years ago
Here, int addX() {x++; return x;}
int addY() {y++; return y;}

These are nonstatic methods in main method s1 class but still works, how?

AkshayA said:   8 years ago
I believe that what @Hari has said is correct. While you may be getting an output of 1 1 2 2 3 3, this is most likely because this is a simple example.

In Java, and multi-threading, context switching/time slicing is handled by Java. At any point, any thread can be brought into context or taken out of it.

I am fairly certain that if this was a more complex set of tasks, we would be able to see what appears to be a random order of execution.

Reetu said:   8 years ago
I got this output as:

Thread-0:1 1
Thread-1:1 1
Thread-0:2 2
Thread-1:2 2
Thread-0:3 3
Thread-1:3 3
Thread-0:4 4
Thread-1:4 4
Thread-0:5 5
Thread-1:5 5
Thread-0:6 6
Thread-1:6 6
Thread-0:7 7
Thread-1:7 7
Thread-0:8 8
Thread-1:8 8
Thread-0:9 9
Thread-1:9 9
Thread-0:10 10
Thread-1:10 10

I did not get the output shown in question but this output is making a sense because when in first attempt both threads will get same output then how output is changed.

Can anyone explain it?

Hari said:   9 years ago
Threads will execute in any order! it is the nature of thread! It will print but the order cannot be judged for threads.

Bulu said:   9 years ago
I got this output in eclipse:

11
22
33
44
55
66
77
88
99
1010
11
22
33
44
55
66
77
88
99
1010

Deepika said:   1 decade ago
I got the following output:

11
22
33
44
55
66
77
88
99
1010
11
22
33
44
55
66
77
88
99
1010

Sundar said:   1 decade ago
I got the following output:

1 1
2 2
1 1
3 3
2 2
4 4
3 3
5 5
4 4
6 6
5 5
7 7
6 6
8 8
7 7
9 9
8 8
10 10
9 9
10 10


Post your comments here:

Your comments will be displayed after verification.