Hi all,
This is my code, which I've made for my school assignment:
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()); } }
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; } }
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); } } }
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:
But according to the school assignment from the Reader the output should be:Begin-stand is : 0
End-stand is : 8000000
This is a quote from the relevant school assignment:Begin-stand is : 0
End-stand is : 7999982
Am I doing something wrong or is there any solution to make the "End-stand"-output to 7999982?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?
Thanks!