This question's right option is nothing can be said about the output.
Question ID :988383703406 Consider the following code:
What will the above code print? This question's right option is it'll keep printing the x and y values incrementing one on each line.
Consider the following code:
This code's right option says it'll keep printing x and y values incrementing 1 on each line. I'm kind of confused. Can anyone clear my doubts regarding threads?
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
You are going to have to state the cause of your confusion a little more clearly. I suggest you actually compile and run a test program for each of these questions, then if you are still confused, pick one question and state what you are confused about. It may be that getting the code to compile and run will answer your questions. Bill
Ok , here's what i did with your codes Limit the loops so that i can atleast see some o/p & Pass a string to individual threads so that i know which thread did what at a particular instant . So i came up with this which is derived from your code3 -
<pre> public class TestQ extends Thread{ static Object obj = new Object(); static int x,y; String msg; public TestQ(String s){ msg = s; }
public void run() { synchronized(obj) { for(;(x < 5) && (y < 5);</pre><pre>){ x++; y++; System.out.println(x+" "+y+" "+msg); } } } public static void main(String[] args) { new TestQ("thread01").start(); new TestQ("thread02").start(); } } </pre> The o/p with this code on Win98 ---------- java ---------- 1 1 thread01 2 2 thread01 3 3 thread01 4 4 thread01 5 5 thread01 Normal Termination Output completed (3 sec consumed). As you can see , the second thread never gets to execute the for loop cause the static memebers x & y would've been set to 5 by the time it got into action . By saying synchronized(obj){ //some statements } you can be sure that no other instances of that class get to call the statements inside {} as long as any other instance has got a hold on it . try this - remove synchronized(obj) from my code & see the o/p. my o/p was ---------- java ---------- 1 1 thread01 2 2 thread02 3 3 thread02 4 4 thread02 5 5 thread02 Normal Termination Output completed (2 sec consumed). See , same o/p but different threads . A threads priority is used to determine when to switch from one thread to another but the two threads created in the code have equal priorities . How these get resolved will depend on your specific OS & this could vary . Again simply synchronizing the block doesn't help & so you define x & y as static & now you can be sure about the o/p no matter which thread get's it done. try this - int x,y;//remove the static from my code & see the o/p . try & see what happens when the loop is not synchronized & x ,y are not static . Code2 x & y are static so all instances share the same variable & run is synchronized so only one instance of this class get's to use run at any given instant . Hence you can be sure 'bout the o/p . Code1 For your first code x & y are not static so each thread would have it's own copies of x & y . And since resolving threads with equal priorities depends on the OS you could probably predict the o/p for your platform but for some other platform(for which you know nothing about) you coldn't predict the o/p. Try to see which threads are called in your other two codes . You'll get the picture . Hope i didn't confuse you & thanx to your query , i now understand synchronized . [This message has been edited by Ashish Hareet (edited July 22, 2001).]