user2075927
user2075927

Reputation: 21

Incrementing and decremening a single shared variable with multiple Threads

When incrementing and decrementing multiple threads using a single shared variable, how can I ensure that the threads count in a syncronized way and don't skip any value.

I have created a seperate class in which I have 3 different methods, one to increment, another to decrement and the last one to return the value. they are all synchronized as well.

The result shows an example:

As you can see the threads are decrementing but then it jumps to "291" which should not happen as I am using a shared variable.

*******************EDIT********

CODE:- SHARED VARIABLE CLASS

public class shareVar extends Thread
{
    private static int sharedVariable = 0;


    public synchronized static void increment(){
        sharedVariable++;
    }

    public synchronized static void decrement(){
        sharedVariable--;
    }

    public  static int value(){
        return sharedVariable;
    }
}

----- Incrementing Class

sVar incrementVal = new sVar();

public synchronized void display(){

    for(int countVal = 0; countVal<=max; countVal++ ){
            incrementVal.increment();
            System.out.println("This is " + threadName + " iteration: " + incrementVal.value() + " of " + max);
            //this.yield();
    }
    System.out.println("*THIS " + threadName + " IS FINISH " 
                                    + "INCREMENTING *");

}

public void run(){

    display();
}

Upvotes: 1

Views: 11510

Answers (2)

Eng.Fouad
Eng.Fouad

Reputation: 117587

Consider using AtomicInteger:

public class Foo
{
    private AtomicInteger counter = new AtomicInteger(0);

    public void increment()
    {
        counter.incrementAndGet();
    }

    public void decrement()
    {
        counter.decrementAndGet();
    }

    public int getValue()
    {
        return counter.get();
    }
}

or using the synchronized methods:

public class Foo
{
    private volatile int counter;

    public synchronized void increment()
    {
        counter++;
    }

    public synchronized void decrement()
    {
        counter--;
    }

    public int getValue()
    {
        return counter;
    }
}

Upvotes: 6

fish
fish

Reputation: 1050

Not sure if I understand your question correctly, but your output looks the way it is just because another thread (Thread_4) gets to work on the value before Thread_5 outputs it.

There are a number of operations going on in each iteration (simplified list, in reality there are more than these):

  1. Increment/decrement
  2. Get the current value
  3. Create the output String
  4. Output the output String

And another thread may get a turn between any of these operations. So it could be that Thread_5 does what it does, then other threads get turns and only after a while Thread_5 outputs the result.

If you want the new values to be output in order, you need to output the current value inside the synchronized block, ie. the increment/decrement methods.

Upvotes: 0

Related Questions