Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: The java-output doesn't give the right result?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The java-output doesn't give the right result?

    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:

    Begin-stand is : 0
    End-stand is : 8000000
    But according to the school assignment from the Reader the output should be:

    Begin-stand is : 0
    End-stand is : 7999982
    This is a quote from the relevant school assignment:
    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!
    Last edited by pinotje; May 30th, 2011 at 05:31 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    Last edited by Norm; May 30th, 2011 at 04:52 PM.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The java-output doesn't give the right result?

    Quote Originally Posted by Norm View Post
    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!

  4. #4
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default 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?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.

  6. #6
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The java-output doesn't give the right result?

    Quote Originally Posted by OutputStream View Post
    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....

  7. #7
    Member OutputStream's Avatar
    Join Date
    Apr 2011
    Posts
    32
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 3 Posts

    Default 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.

  8. #8
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The java-output doesn't give the right result?

    Quote Originally Posted by OutputStream View Post
    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

  9. #9
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The java-output doesn't give the right result?

    Quote Originally Posted by OutputStream View Post
    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.

Similar Threads

  1. Replies: 4
    Last Post: February 23rd, 2011, 09:20 AM
  2. Trying to Give Coins For Change
    By bengregg in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 27th, 2011, 04:21 PM
  3. how to give inputs to my simple java program
    By pokuri in forum What's Wrong With My Code?
    Replies: 11
    Last Post: January 8th, 2011, 07:36 PM
  4. Sting format give more truble
    By ahmethbaai in forum Java Theory & Questions
    Replies: 1
    Last Post: December 17th, 2009, 01:19 PM
  5. New give thanks feature
    By JavaPF in forum The Cafe
    Replies: 0
    Last Post: April 11th, 2009, 11:14 AM