Click here to Skip to main content
15,881,248 members
Home / Discussions / Java
   

Java

 
AnswerRe: Best books/Tutorials to learn Java.? Pin
Gowtham Gutha15-Nov-12 6:54
Gowtham Gutha15-Nov-12 6:54 
GeneralRe: Best books/Tutorials to learn Java.? Pin
mbatra3115-Nov-12 18:54
mbatra3115-Nov-12 18:54 
GeneralRe: Best books/Tutorials to learn Java.? Pin
Gowtham Gutha16-Nov-12 7:13
Gowtham Gutha16-Nov-12 7:13 
GeneralRe: Best books/Tutorials to learn Java.? Pin
Gowtham Gutha16-Nov-12 7:14
Gowtham Gutha16-Nov-12 7:14 
QuestionFIX Day Changed Problem Pin
techGaurav8-Oct-12 21:42
techGaurav8-Oct-12 21:42 
AnswerRe: FIX Day Changed Problem Pin
TorstenH.8-Oct-12 22:00
TorstenH.8-Oct-12 22:00 
AnswerRe: FIX Day Changed Problem Pin
Nagy Vilmos11-Oct-12 20:12
professionalNagy Vilmos11-Oct-12 20:12 
QuestionThreads in details Pin
Neo101016-Oct-12 0:16
Neo101016-Oct-12 0:16 
See the following code:
Unsafe Counter
Java
public class Counter implements Counting
{
    private int value = 0;

    public int incrementAndGet()
    {
        return this.value++;
    }

    public int decrementAndGet()
    {
        return this.value--;
    }
}

CounterClient
Java
public class CounterClient implements Runnable
{
    //shared data    
    private Counting counter;

    public CounterClient(Counting counter)
    {
        this.counter = counter;
    }

    public void run()
    {
        while(true)
        {
            System.out.println("Incrementing from " + Thread.currentThread().getName() );                
            System.out.println("Counter value in " + Thread.currentThread().getName() + " = " + counter.incrementAndGet());                
            
            try
            {
                Thread.sleep(2000L);
            } catch (InterruptedException ex)
            {
                Logger.getLogger(CounterClient.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
}

TestCounter
Java
public class TestCounter
{
    public static void main(String args[])
    {
        Counter counter = new Counter();
        SafeCounter safeCounter = new SafeCounter();
        
        Executor exec = Executors.newCachedThreadPool();
        
        exec.execute(new CounterClient(counter));
        exec.execute(new CounterClient(counter));        
        /*
        exec.execute(new CounterClient(safeCounter));
        exec.execute(new CounterClient(safeCounter));
         */
         
    }
    
}


I did a test run for this code and one of the outputs was this:
Incrementing from pool-1-thread-1
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-1 = 0
Counter value in pool-1-thread-2 = 1
Incrementing from pool-1-thread-2
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-2 = 2
Counter value in pool-1-thread-1 = 3
Incrementing from pool-1-thread-2
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 4
Counter value in pool-1-thread-2 = 5
Incrementing from pool-1-thread-2
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 6
Counter value in pool-1-thread-2 = 7
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 8
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-2 = 9
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-2 = 10
Incrementing from pool-1-thread-1
Counter value in pool-1-thread-1 = 11
Incrementing from pool-1-thread-1
Incrementing from pool-1-thread-2
Counter value in pool-1-thread-2 = 13
Counter value in pool-1-thread-1 = 12


I am trying to understand what exactly is going on in the thread activity here that is not thread safe. As you can see, some values are out of order: e.g. 13,12

This is what I can deduct from the output:
Thread-1 increments (value++)
Thread-2 increments (value++)

Thread-1's value should be 1 now, but I see this:
Counter value in pool-1-thread-1 = 0
Is this because thread-1 is still reading and didn't write the result of the register back to memory?

Why is it 0? Didn't it just increment?
Thread-2's value is 1. That's logical.

This is all a bit confusing to me and I would like to know exactly what's going on in that output.

Thanks
AnswerRe: Threads in details Pin
Nagy Vilmos8-Oct-12 0:54
professionalNagy Vilmos8-Oct-12 0:54 
Questionjava applet security issues Pin
Ponnampi5-Oct-12 18:01
Ponnampi5-Oct-12 18:01 
AnswerRe: java applet security issues Pin
Ponnampi5-Oct-12 18:12
Ponnampi5-Oct-12 18:12 
QuestionData Structures - ArrayDeque <T> extends AbstractList <T> Pin
Member 94884535-Oct-12 16:29
Member 94884535-Oct-12 16:29 
AnswerRe: Data Structures - ArrayDeque extends AbstractList Pin
TorstenH.5-Oct-12 21:36
TorstenH.5-Oct-12 21:36 
QuestionCreating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 2:43
Bill.Moo4-Oct-12 2:43 
AnswerRe: Creating a class that extends more than one class. Pin
Nagy Vilmos4-Oct-12 3:00
professionalNagy Vilmos4-Oct-12 3:00 
GeneralRe: Creating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 3:48
Bill.Moo4-Oct-12 3:48 
AnswerRe: Creating a class that extends more than one class. Pin
Peter_in_27804-Oct-12 3:03
professionalPeter_in_27804-Oct-12 3:03 
GeneralRe: Creating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 3:59
Bill.Moo4-Oct-12 3:59 
GeneralRe: Creating a class that extends more than one class. Pin
Peter_in_27804-Oct-12 11:36
professionalPeter_in_27804-Oct-12 11:36 
AnswerRe: Creating a class that extends more than one class. Pin
pasztorpisti4-Oct-12 12:14
pasztorpisti4-Oct-12 12:14 
QuestionRe: Creating a class that extends more than one class. Pin
Monster Maker21-Oct-12 21:08
Monster Maker21-Oct-12 21:08 
AnswerRe: Creating a class that extends more than one class. Pin
Gowtham Gutha15-Nov-12 6:59
Gowtham Gutha15-Nov-12 6:59 
QuestionThread names Pin
Neo101014-Oct-12 1:19
Neo101014-Oct-12 1:19 
AnswerRe: Thread names Pin
Nagy Vilmos4-Oct-12 2:54
professionalNagy Vilmos4-Oct-12 2:54 
AnswerRe: Thread names Pin
Gowtham Gutha15-Nov-12 7:11
Gowtham Gutha15-Nov-12 7:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.