Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 14-12-2008, 12:43 PM   #1 (permalink)
Broken In
 
sganesh's Avatar
 
Join Date: Nov 2007
Location: Chennai
Posts: 113
Default Data Race program in Java sounds Different!!!!


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.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
l
Why compilation in Linux environment fails??
wat exact output the program?
sganesh is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 14-12-2008, 02:49 PM   #2 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Data Race program in Java sounds Different!!!!

I guess you JDK installation is corrupt in Linux. Also, it seems to be using gcj for compilation. Post output of javac -version.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 14-12-2008, 03:17 PM   #3 (permalink)
Broken In
 
sganesh's Avatar
 
Join Date: Nov 2007
Location: Chennai
Posts: 113
Default Re: Data Race program in Java sounds Different!!!!

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:
sganesh is offline  
Old 14-12-2008, 04:47 PM   #4 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Data Race program in Java sounds Different!!!!

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.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 14-12-2008, 04:53 PM   #5 (permalink)
Broken In
 
sganesh's Avatar
 
Join Date: Nov 2007
Location: Chennai
Posts: 113
Default Re: Data Race program in Java sounds Different!!!!

ya correct...
i got it!!.
Thanks
sganesh is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java programming..how can I distribute java program....? Manojap Programming 5 16-05-2009 02:46 PM
java program not run Amir.php Programming 5 17-02-2008 05:07 PM
help in java program shivi4 QnA (read only) 3 08-09-2005 07:27 AM
please help in java program shivi4 QnA (read only) 10 24-08-2005 06:56 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:03 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2