• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads Question

 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {

static int i = 0;

public static void main(String[] args) {

Thread t = new Thread(new Runnable(){

public void run(){
for(int j = 0; j < 3; j++)
System.out.println(++i);
}
});

t.start();
System.out.println(i);
}
}

Is the output ( given as 0123) predictable and always the same ? If so how ?
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output does come out to be 0123, but it is not predictable. This is because after calling t.start(), it is upto the scheduler to decide whether the main thread should begin execution first or the child thread.

Usually, Java gives the child thread the same priority as the thread in which it was created.

If the scheduler decides to give priority to the child thread, the output would not remain the same (See code output below).

Just to demonstrate the above to you,


Here is the output from 2 consecutive runs of the program:
Output 1:
Main thread's priority is 5
In Child 1
In Child 2
In Child 3
In Main 4
In Main 5
In Main 6
Child thread's priority is 5
6

Output 2:
Main thread's priority is 5
In Main 1
In Main 2
In Main 3
In Child 4
In Child 5
In Child 6
Child thread's priority is 5
6
[ August 15, 2006: Message edited by: Aniket Patil ]
 
Radoslaw Sztando
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I guess output's not predictable.
Imagine scenario when new thread is executing line "System.out.println(++i);"
First i is incremented and then displayed, but I believe that line
"System.out.println(i);" from main thread can be executed after i is incremented but before it is displayed by new thread. In this scenario the same value of i could be displayed twice.
Anyone please correct me if I'm wrong.

Hope it helps.
 
Radoslaw Sztando
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aniket I guess you're wrong.
Doesn't matter which tread starts when - the output will be "0123" unless the situation which I described occurs.
To change output printing in main thread must happen after i is incremented and before it is displayed in other thread
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point i am trying to make is it is possible for the scheduler to assign execution time to the child thread first. Check the output in my earlier post.

In the original code, it is possible that the child thread runs first, increments i to 3 and then the main thread starts executing. In that case, a possible output would be 1233.

The scheduler may decide to keep a thread with equal priority as the currently running thread in the runnable queue or give it a chance to run by pre-empting the current thread.
 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
for the above given question , the answer is given as predictabe and it is 0123..can any one tell me how?
 
Radoslaw Sztando
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prarthana,

in my opinion if this output is predicatble then System.out.println(++i); is atomic operation - it cannot be split.

Hope it helps
 
prarthana reddy
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is the case then there is a possibility of 1233 answer also..which again makes it unpredictable
 
Radoslaw Sztando
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well... you're right
My mistake (from the beginning of this thread I'm afraid)... Sorry for confusion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic