This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

need a bit explaination with threads......

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answer : It will print "World" 5 times
Can any one tell me why isn't Hello being printed?
Sonir
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran your code And my output included both World and Hello. I compiled and Ran using jEdit.... One note you should always put the access modifier first "static protected int i = 0;" should be "protected static int i = 0;" it is better practice.
 
Ranch Hand
Posts: 5390
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have JDK1.3
I get o/p :
World
World
World
World
World
Press any key to continue . . .
aneone plz ?
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also get World printed out 5 times. I'm not too hot on threads but I think I can suggest an explanation.
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.
 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The given answer is wrong. It may or may not print "World" 5 times as there are 2 threads running simultaneously.
It'll print "World" m times and "Hello" n times, where m + n = 5.
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the integer i is "static"
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 ]
 
R K Singh
Ranch Hand
Posts: 5390
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am with Jim
CMIW
TIA
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am with Chung Lee and Mark Stone on this. It is the static variable. There is only one copy of 'i' and the second loop does not get ececuted. Changing i to non static variable will print the second list.
Thanks,
Surendra
 
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im with ravish and jim
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, it is not as "easy" as mark thinks. i agree with ravish and jim that the output cant be determined. thats not what mark said...
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there are two parts to the answer :
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.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivaji i agree 100%
 
R K Singh
Ranch Hand
Posts: 5390
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Roy Ben Ami:
Shivaji i agree 100%


Me too
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks have been assuming that there will always be 5 total iterations in this code. While that would often be the case, I don't think you can even depend on that. For example, on a multiprocessor box I believe you could see more subtle interactions. There is no synchronization to guard access to that static 'i', and the process of testing and then incrementing in a loop is not an atomic action. I think you could make a plausible argument for 10 iterations being possible, although the probability of seeing it would be very low if threads get CPU slices on an essentially random schedule.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You raise an interesting point.
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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is my code:
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
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
more interesting:
if i print i1+i2 as follows:
System.out.println(A.i1 + ", " + A.i2 + ", " + (A.i1+A.i2));
result is

3324816, 1712630, 5037446!!!
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's weird. It's running an extra 37,000 times!
Maybe the loops forgot they were suppose to stop at 5 million??
Rob
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sometimes it is 5000000, sometimes 5000001, this time it is 6376228!
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possible explaination is that the loops are keeping a private internal variable for the iterations and they periodically write that back to the "real" static i...but since there are two threads, they are clobbering all over each other and that's screwing up the overall increment.
Rob
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Rob, maybe you are saying that since i++ is really i = i + 1, that possibly the scenario below could be occuring:
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 ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
but my output is different. it prints
World
Hello
Hello
Hello
Hello
World
thanks,
anu
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,
I still cannot understand, why is everyone getting different ouptput?..
This shouldn't happened..
Any comments?
Sonir
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JLS 8.3.1.4 can help for this matter.
The differences in the output are due to the lack of synchronization.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output can't be predict, but we know one things.
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 ]
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx Younes..
My certification is tommorrow morning.
I am from India, so after 12 hours from now(approx)..
Sonir
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand my explanation or is my english not good enough???
 
sonir shah
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah..Younes..
Why do u think that your English is not proper?..
I dont think so..
Neways...
Sonir
 
Did you miss me? Did you miss this tiny ad?
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!