The java-output doesn't give the right result?
Hi all,
This is my code, which I've made for my school assignment:
Code :
public class Main {
public static void main (String[] args) throws InterruptedException {
Stand st = new Stand(0);
System.out.println("Begin-stand is : " + st.getPunten());
GelijkspelThread gt = new GelijkspelThread(st);
WinnenThread wt = new WinnenThread(st);
gt.start();
wt.start();
gt.join();
wt.join();
System.out.println("End-stand is : " + st.getPunten());
}
}
Code :
public class Stand {
private int doelsaldo;
public Stand(int d) { doelsaldo = d; }
public synchronized void win(int punt) { doelsaldo = doelsaldo + punt; }
public synchronized void gelijk(int punt) { doelsaldo = doelsaldo + punt; }
public int getPunten() { return doelsaldo; }
}
Code :
public class WinnenThread extends Thread {
private Stand deStand;
public WinnenThread(Stand s) { deStand = s; }
public void run() {
for (int i=0; i<2000000; i++) { deStand.win(3); }
}
}
Code :
public class GelijkspelThread extends Thread {
private Stand deStand;
public GelijkspelThread(Stand s) { deStand = s; }
public void run() {
for (int i=0; i<2000000; i++) { deStand.gelijk(1); }
}
}
After running my code above in Eclipse, I got the following output:
Quote:
Begin-stand is : 0
End-stand is : 8000000
But according to the school assignment from the Reader the output should be:
Quote:
Begin-stand is : 0
End-stand is : 7999982
This is a quote from the relevant school assignment:
Quote:
The WinnenThread should add 3 points to the punt (dutch word for points) of Stand and the GelijkThread should add 1 point.
And both Threads must run 2.000.000 times. Question: Is the end-stand 8000000 or maybe not?
Am I doing something wrong or is there any solution to make the "End-stand"-output to 7999982?
Thanks!
Re: The java-output doesn't give the right result?
If you want anyone to compile and execute your code, put all the code in one file.
Re: The java-output doesn't give the right result?
Quote:
Originally Posted by
Norm
If you want anyone to compile and execute your code, put all the code in one file.
Hi,
I'm really sorry, but according to my school assignment, I must do it this way.
Luckily it is not a lot codes, so I think Java-experts can helping me out without compiling it....
Thanks!
Re: The java-output doesn't give the right result?
Basic math:
3 * 2'000'000 = 6'000'000
1 * 2'000'000 = 2'000'000
Add them together and you get 8'000'000.
How can 7'999'982 be the right output?
Re: The java-output doesn't give the right result?
Ok, I'll let the next guy handle this.
BTW you can change the code for testing and then change it back before handing in the assignment.
Re: The java-output doesn't give the right result?
Quote:
Originally Posted by
OutputStream
Basic math:
3 * 2'000'000 = 6'000'000
1 * 2'000'000 = 2'000'000
Add them together and you get 8'000'000.
How can 7'999'982 be the right output?
Yup, That's exactly what I'm thinking....
so maybe it's a little mistake in the school assignment...?
or maybe I should remove "synchronized" ?
or maybe adjust the "for-lus" ?
just an idea....
Re: The java-output doesn't give the right result?
If you remove the synchronized keyword from both methods then you would probably get different results every time you run the program.
There is nothing wrong with the loops, they are looping exactly 2'000'000 times.
I'd say there is a mistake in your assignment.
Re: The java-output doesn't give the right result?
Quote:
Originally Posted by
OutputStream
If you remove the synchronized keyword from both methods then you would probably get different results every time you run the program.
There is nothing wrong with the loops, they are looping exactly 2'000'000 times.
I'd say there is a mistake in your assignment.
Okay, I'll complain my teacher.... =P
Re: The java-output doesn't give the right result?
Quote:
Originally Posted by
OutputStream
If you remove the synchronized keyword from both methods then you would probably get different results every time you run the program.
There is nothing wrong with the loops, they are looping exactly 2'000'000 times.
I'd say there is a mistake in your assignment.
I've asked my teacher, he said that if I remove the "synchronized" then I'll get a random result.
So if I remove "synchronized" then I'll get that random output, if I add "synchronized" then the result will be 8000000.