my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Question" Watch "Threads Question" New topic
Author

Threads Question

prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
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

Joined: May 02, 2006
Posts: 218
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 ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Radoslaw Sztando
Ranch Hand

Joined: Mar 11, 2004
Posts: 40
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.


Regards,
Radek Sztando
Radoslaw Sztando
Ranch Hand

Joined: Mar 11, 2004
Posts: 40
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

Joined: May 02, 2006
Posts: 218
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

Joined: Jul 24, 2006
Posts: 48
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

Joined: Mar 11, 2004
Posts: 40
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

Joined: Jul 24, 2006
Posts: 48
If that is the case then there is a possibility of 1233 answer also..which again makes it unpredictable
Radoslaw Sztando
Ranch Hand

Joined: Mar 11, 2004
Posts: 40
well... you're right
My mistake (from the beginning of this thread I'm afraid)... Sorry for confusion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Question
 
Similar Threads
annonymous thread
Doubt Reg: initializing Variables with method call
Array Indexing
Is this a thread problem.....(..
why does 'break' traverse one more time