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();
}
}
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.
Saurabh garg said:
1 decade ago
I did not understand the reason. Can anyone explain.
Srirajyalakshmi said:
1 decade ago
I did not understand the logic can any one explain please.
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
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
Srirajyalakshmi said:
1 decade ago
Well here logic is as we are calling 2 threads with 2 start methods and one run method so this run method is called twice so first the 1st thread is executed until the loop ends and then second thread gets executed so finally we get the output as 1 1,2 2, 3 3,......
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
11
22
33
44
55
66
77
88
99
1010
11
22
33
44
55
66
77
88
99
1010
Bulu said:
10 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
11
22
33
44
55
66
77
88
99
1010
11
22
33
44
55
66
77
88
99
1010
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.
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?
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?
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.
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.
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?
int addY() {y++; return y;}
These are nonstatic methods in main method s1 class but still works, how?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers