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

Thread: Synchronized a integer variable in Java

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

    Default Synchronized a integer variable in Java

    Hi all

    I am trying to synchronized an integer variable in java.

    An example is as below:

    class Myclass{
    private static int numOfball

    public synchronized void getBall()
    {
    synchronized(numOfball)
    {

    }



    }

    The statement synchronized(numOfball) give me a problem.Can please advise how to synchronized numOfball??

    Thank you.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Synchronized a integer variable in Java

    Quote Originally Posted by hellokitty25 View Post
    Hi all

    I am trying to synchronized an integer variable in java.
    This doesn't make sense. What do you mean by "synchronize" a variable. This cannot be done in Java, period. You can synchronize on an *object* not a variable, and it's often an object that is not used to hold a value, but rather simply to act as a lock.
    An example is as below:

    class Myclass{
    private static int numOfball

    public synchronized void getBall()
    {
    synchronized(numOfball)
    {

    }



    }

    The statement synchronized(numOfball) give me a problem.Can please advise how to synchronized numOfball??
    If you have a compiler error, post it here as well. But most important -- tell us what you're trying to achieve, not how you're trying to achieve it because your approach again is not only impossible, it doesn't make sense.

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

    Default Re: Synchronized a integer variable in Java

    Hi

    What I am trying to achieved is that I will have 10 objects of Myclass which is a thread. The numOfball inside must not be overwrite.

    Regards

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Synchronized a integer variable in Java

    Quote Originally Posted by hellokitty25 View Post
    What I am trying to achieved is that I will have 10 objects of Myclass which is a thread. The numOfball inside must not be overwrite.
    If it's to be a constant, then make it a final variable. Or if I'm misunderstanding you, please clarify your problem some more.

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

    Default Re: Synchronized a integer variable in Java

    Hi I am trying to create 10 objects of Myclass which extends Thread in the main class. The numOfball is passed to the Myclass objects and return to the main class.
    The numOfball must be synchronized to ensure that it is correct each time an object of Myclass is created

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Synchronized a integer variable in Java

    This looks like a homework assignment. Please post the actual assignment text. Let's see exactly what your requirements are.

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

    Default Re: Synchronized a integer variable in Java

    Hi

    I almost got the output. Just that as this is a thread, the numOfballs must be synchronized. Otherwise the numOfball will not be correct.

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Synchronized a integer variable in Java

    Quote Originally Posted by hellokitty25 View Post
    Hi

    I almost got the output. Just that as this is a thread, the numOfballs must be synchronized. Otherwise the numOfball will not be correct.
    Again, a variable cannot be synchronized. What probably must be synchronized is the method that sets this variable. Make this variable volatile, and then you will need to use a shared Object that it can synchronize perhaps a static public Object held by a variable called "lock". Whatever you do, do not synchronize on an object of the class that you make 10 instances of.

Similar Threads

  1. Synchronized block in java
    By me_shankara in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 12th, 2012, 11:43 PM
  2. synchronized block in java
    By me_shankara in forum Threads
    Replies: 1
    Last Post: December 3rd, 2012, 11:04 PM
  3. Synchronized block vs Synchronized method
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:51 AM
  4. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  5. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM