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 19 of 19

Thread: How to synchronised a variable in a class such that the variable will remain each time a thread is run

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Hi all

    Suppose I have a main method that do the following:
    Ball [] ball = new Ball[5];
    private int numOfball = 10;

    for (int i = 0; i < ball.length i++)
    {
    ball[i] = new Ball(i,numOfball);
    ball[i].start();

    numOfball= ball.getNum();

    }

    Inside the Ball class I have the following:
    public class Ball extends Thread
    {
    private static int numOfball;
    private int id;



    public Ball(int id,int numOfball)
    {
    this.id = id;
    this.numOfball = numOfball;
    }


    public synchronized int getNum()
    {
    System.out.println("Number left:" + numOfball);
    if(numOfball>0)
    {
    System.out.println("Ball" + id + " throw);
    Thread.sleep(id);
    numOfball--;
    System.out.println("Ball " + id + " return." );

    }else
    {
    System.out.println( "There are no more ball!" );
    numOfball = 10;
    }

    return numOfball;
    }

    public void run()
    {
    getNum();
    }

    }
    Each time I call the ball class in my main method, the numOfball seem to be the same for a few times.May I know how to make the numOfball in the Ball class such that it will be synchronized and not repeated?

    Thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    how to make the numOfball in the Ball class such that it will be synchronized and not repeated?
    Not sure what "synchronized" means here.
    Can you post some output from the program that shows what you are talking about? Add some comments to explain what the problem is.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    The problem is that you're setting the value of numOfBall in your constructor for the Ball class. This is going to reset your static variable each time. You really shouldn't set the value of static variables in a constructor anyways. What is this program supposed to be doing? If you give us a description it may help to understand what's going on.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Hi all

    What I am trying to do is this:

    The output should be:
    Ball0 throw
    Ball6 throw
    Ball7 throw
    Ball8 throw
    Ball0 return
    Ball6 return....
    When numOfball reach 0 after each Ball throw,it shd display the message "There are no more ball!"

    However my output is
    Ball0 throws
    Ball4 throws
    Ball8 throws
    Ball4 return
    and so on
    the numOfballs after each throw is not what I want.
    For example after Ball0 throws, the numOfballs is reduced by 1 to 9.But when comes to Ball4 throws the number still remain as 10 in which it should be 9 .

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Please post a small complete program that compiles, executes and shows the problem.
    Be sure to wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    This is definitely because you are setting the numOfballs in your constructor for Ball. You are resetting it to 10 with every object. It should be set once at the beginning of the program.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    public class TestBall
    {
     
        public static void main(String[] args)
    	{
    	     Ball [] ball = new Ball[5];
     int numOfball = 2;
     
    for (int i = 0; i < ball.length; i++)
     {
     ball[i] = new Ball(i,numOfball);
     ball[i].start();
     
    numOfball= ball[i].getNum();
     
     }
      }
     
    }

    import java.util.*;
     
    public class Ball extends Thread
     { 
    private static int numOfball;
     private int id;
     
     
     
    public Ball(int id,int numOfball)
     {
     this.id = id;
     this.numOfball = numOfball;
     }
     
     
    public synchronized int getNum()
     {
     System.out.println("Number left:" + numOfball);
     if(numOfball>0)
     {
     System.out.println("Ball" + id + " throw");
     try{
     Thread.sleep(id);
     }catch (InterruptedException e){}
     
     numOfball--;
     System.out.println("Ball " + id + " return." );
     
     }else
     {
     System.out.println( "There are no more ball!" );
     numOfball = 2;
     }
     
    return numOfball;
     }
     
    public void run()
     {
     getNum();
     }
     
    }

    Output is:

    Number left:2
    Ball0 throw
    Ball 0 return.
    Number left:1
    Ball0 throw
    Number left:1
    Ball1 throw
    Ball 0 return.
    Ball 1 return.
    Number left:-1
    There are no more ball!
    Number left:2
    Ball1 throw
    Number left:2
    Ball3 throw
    Number left:2
    Ball2 throw
    Ball 2 return.
    Ball 1 return.
    Ball 3 return.
    Number left:-1
    There are no more ball!
    Number left:-1
    Ball4 throw
    Ball 4 return.
    Number left:1
    Ball4 throw
    Ball 4 return.


    There should be only one ball0 throw not a repeat of ball0 throw. the same go for the rest.Also Number left:1 should be only one time after reduced by one each time a thread is run

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Why does each constructor set the value of the static variable? Each one is changing the value that the last one set.
    There is only one copy of a static variable shared among all the class objects.

    BTW The formatting of the posted code is very poor which makes the code hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Hi in the main program there is a return value which will change the value
    numOfball= ball[i].getNum();

  10. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    You are setting your static variable in the constructor. This is wrong unless you have some sort of justification for it. Can you confirm you understand this error?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    If I do not set it,is there any other ways to get the numOfball in the main program?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    ways to get the numOfball in the main program
    Can you describe what values the numOfball variable is supposed to hold? Should each instance of the Ball class have its own value? Making it static means that all instances share the ONE variable.

    If you wanted a method to have access to a static variable, create a static getVariable() method that returns its value.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Make a static setter for numOfball

    Example:
    	public static void main(String[] args) {
     
    		Book.setTitle("Test");
    	}
     
     
    	private static String title;
     
    	public static void setTitle(String newTitle){
    	       title = newTitle;
    	}
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  14. #14
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Hi all instances should shared one value.That is the numOfball will be shared by the instances each time a thread is started.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Then why does the constructor set the value of numOfball? Each call of a constructor will change its value?

    Why not set it one time from outside of the constructor?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Hi In my main program the inital value is 2 and I need to pass it to the class as shown below:
    public static void main(String[] args)
    {
    Ball [] ball = new Ball[5];
    int numOfball = 2;
    for (int i = 0; i < ball.length; i++)
    {
    ball[i] = new Ball(i,numOfball);
    ball[i].start();

    numOfball= ball[i].getNum();

    }


    I try to use the set method to set the value.But the output is still the same.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Why does the code copy the value of numOfball from the Ball object it just created?

    WHere is the code that calls a set method ONE TIME to set the value of numOfball that all the Ball objects to use.

    Can you explain what the numOfball value is supposed to be used for?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    Hi To cut short:

    What I want is the Ball class which extend Thread and the main method which start the thread.
    The output should be given that the numOfball initial value is 2 and after 2 it will have numOfball to 2 again :
    Ball 1 throw
    Ball 4 throw
    There are no more ball!
    Ball 3 throw
    Ball 4 return
    Ball 3 return
    Ball 2 throw
    There are no more ball!
    Ball 1 return
    Ball 5 throw

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,661
    Thanks
    45
    Thanked 1,960 Times in 1,934 Posts

    Default Re: How to synchronised a variable in a class such that the variable will remain each time a thread is run

    What is the value of numOfball used for?

    What does a message with "throw" mean? How does that message relate to the value of numOfball?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Also the formatting of the code needs to be fixed. The indentations are not right and make the code hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How can i store time in a variable for comparsion?
    By steven_bishop in forum Java Theory & Questions
    Replies: 3
    Last Post: December 2nd, 2012, 12:45 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 01:09 PM
  3. First time poster looking for help accessing a variable from a separate class.
    By royalcrown28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 19th, 2012, 11:41 PM
  4. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM
  5. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM