Data Race program in Java sounds Different!!!!

Status
Not open for further replies.

sganesh

Journeyman
Here is a java code ..
SOURCE
DataRace.java

class Counter
{
public static long counter=0;
}
class usecounter implements Runnable{
public void run()
{

for(int i=0;i<3;i++)
{
Counter.counter++;
System.out.println(Counter.counter +" ");
}
}

}

public class DataRace {
public static void main(String args[])
{
usecounter c=new usecounter();
Thread t1=new Thread(c);
Thread t2=new Thread(c);
Thread t3=new Thread(c);
t1.start();
t2.start();
t3.start();
}
}
i run the same program in Two Diiferent environment in XP and openSuse11.
xp uses jdk1.6.0_05
open suse11 uses 1.6.0_06

LOOK at output when i run that code in XP
C:\Documents and Settings\Ganesh\My Documents\NetBeansProjects\SCJP\src>javac Da
taRace.java

C:\Documents and Settings\Ganesh\My Documents\NetBeansProjects\SCJP\src>java Dat
aRace
1 2 3 4 5 6 7 8 9

LOOK Below when i run that same code in opensuse11
localhost:~/Desktop # java -version
java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode)

localhost:~/Desktop # javac DataRace.java
/usr/lib/gcc/i586-suse-linux/4.3/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
localhost:~/Desktop # java DataRace
Exception in thread "main" java.lang.NoClassDefFoundError: DataRace
Caused by: java.lang.ClassNotFoundException: DataRace
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
l
Why compilation in Linux environment fails??
wat exact output the program?
 

chandru.in

In the zone
I guess you JDK installation is corrupt in Linux. Also, it seems to be using gcj for compilation. Post output of javac -version.
 
OP
sganesh

sganesh

Journeyman
To Chandru..
ya,wat u said is right,Even i tried to run hello world program in linux,its giving some error,jdk is corrupted,then
Another question arises in my mind,
The answer of above code is 1 2 3 4 5 6 7 8 9
But if read the article in this page,they ve interpreted this program wrongly,
They says the output will be some random number?
Source:
 

chandru.in

In the zone
Yes the exact sequence of output is not predictable in this program. The reason is that, access Counter.counter is not synchronized. So one thread may increment is but before printing it, another thread may start and increment it further. Even within the println statement, the data might have been read but not yet displayed on console before another thread starts off.

For example in my box it gives the below output.

Code:
2 
3 
2 
5 
6 
4 
7 
9 
8

Synchronization is the answer for this problem. If you see the immediately following section you'd understand it.
 
Status
Not open for further replies.
Top Bottom