need a bit explaination with threads......
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Answer : It will print "World" 5 times
Can any one tell me why isn't Hello being printed?
Sonir
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I get o/p :
World
World
World
World
World
Press any key to continue . . .
aneone plz ?
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The point to notice is that both classes are sharing the protected static int i as the initialiser in their for loops.
When t2.start(); is called, the TestClass thread object runs and the static integer variable i is incremented in the for loop to the value of 5.
When t1.start() is called, the for loop in class A evaluates int i. Since int i=5, the condition is not met and the class A for loop is not entered.
Try running the following code which displays the value of i.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It'll print "World" m times and "Hello" n times, where m + n = 5.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
ie i can have only one value.
if one method run increments i by two before the other method run gets a hand on it, it would increment from where it was earlier.....
if you don't believe me remove the static modifier and code prints 5 Hello and 5 World.
Answer : It will print "World" 5 times
Can any one tell me why isn't Hello being printed?
Sonir[/qb]<hr></blockquote>
[ January 24, 2002: Message edited by: mark stone ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
CMIW
TIA
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks,
Surendra
Surendra<br />SCJP
-
-
Number of slices to send:Optional 'thank-you' note:
-
-

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The fact that i is a static variable means that there will be only 5 lines of output.
But the fact that there are two different threads, we can not predict which thread gets to execute how many iterations of the for loop.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Roy Ben Ami:
Shivaji i agree 100%![]()
Me too

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Reid - SCJP2 (April 2002)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It seems disturbing to me that a loop variable wouldn't be tested using a test&set atomic action.
Are you sure about this? If so, is there a JLS reference that specifies this?
Rob
Rob
SCJP 1.4
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
class A extends Thread{
static protected int i = 0;
static protected int i1 = 0;
static protected int i2 = 0;
static protected int num = 5000000;
public void run(){
//for(; i<num; i++) System.out.println("Hello");
for(; i<num; i++) {
//System.out.println("Hello" + i1);
i1++;
}
}
}
public class TestThread extends A{
public void run(){
for(; i<num; i++) {
i2++;
//System.out.println("World" + i2);
}
//for(; i<num; i++)System.out.println("World");
}
public static void main(String args []) throws Exception{
Thread t1 = new A();
Thread t2 = new TestThread();
t2.start();
t1.start();
t2.join();t1.join();
System.out.println(A.i1 + ", " + A.i2);
}
}
results?
739754, 4260247
i ran it on jdk1.3.1_01 on win98
Yet Another SCJP2
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
if i print i1+i2 as follows:
System.out.println(A.i1 + ", " + A.i2 + ", " + (A.i1+A.i2));
result is
3324816, 1712630, 5037446!!!
Yet Another SCJP2
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Maybe the loops forgot they were suppose to stop at 5 million??
Rob

Rob
SCJP 1.4
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Yet Another SCJP2
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Rob
Rob
SCJP 1.4
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Imagine i being 5 at the time A grabs it..
A grabs i (which is 5) but in order to do: i = i + 1 it first copies i.
Meanwhile i was changed ten times really fast by TestThread which set that static i to 15,
BUT
the i in class A for doing the calculation i = i + 1 might now be doing:
15 = 5(since maybe this is a copy in memory?) + 1,
which of cource would then set static i back to 6.
Maybe this is why like Reid said above you could end up with 10 iterations of the original code instead of 5? Highly unlikely but maybe possible?
Actually I took a break and was trying to test this theory. I think maybe my theory is all wrong. I modified Bill's code slightly above to see if I could create what might be happening with i++ by adding an exagerated delay between creating i = i + 1. However, I can't really get the data to demonstrate this theory.
[ January 26, 2002: Message edited by: Rick Reumann ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
but my output is different. it prints
World
Hello
Hello
Hello
Hello
World
thanks,
anu
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I still cannot understand, why is everyone getting different ouptput?..
This shouldn't happened..
Any comments?
Sonir
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The differences in the output are due to the lack of synchronization.
SCJP2. Please Indent your code using UBB Code
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You are sharing i so that there will not be more than 5 lines at output.
5 is not really much, and in your case threads don't have time to exchange. Also the output depends on the implementation of your JVM.
If you change that 5 for 100, it will be more easy to examine the output.
And you will see that the output change every time you execute!!!
Hope it helped you!!!
When was your certification???
[/qb]<hr></blockquote>
[ January 27, 2002: Message edited by: Younes Essouabni ]
Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
My certification is tommorrow morning.
I am from India, so after 12 hours from now(approx)..
Sonir

-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why do u think that your English is not proper?..
I dont think so..
Neways...
Sonir

| That is a really big piece of pie for such a tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |








