Madhav Sapre

Greenhorn
+ Follow
since Jan 03, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Madhav Sapre

Hi Simon,
As pointed out earlier by someone else, p here is not defined as class variable. Secondly, if you define i as volatile, that will force each thread to refresh its local copy of i before it actually tries to modify i. So Try following code.

public class Unstable implements Runnable {
private int p = 0;
private volatile int i = 0;

public void run() {
for(; i <= 10; i++) {
System.out.println("Thread " + Thread.currentThread().getName() + " i=" + i + " p=" + p++);
}
}
}