Click here to Skip to main content
       

Java

 
QuestionBest books/Tutorials to learn Java.? Pinmembermbatra3111-Oct-12 0:39 
AnswerRe: Best books/Tutorials to learn Java.? Pinmemberpasztorpisti11-Oct-12 1:02 
AnswerRe: Best books/Tutorials to learn Java.? PinmemberTorstenH.11-Oct-12 2:32 
GeneralRe: Best books/Tutorials to learn Java.? Pinmembermbatra3111-Oct-12 3:38 
GeneralRe: Best books/Tutorials to learn Java.? PinmvpRichard MacCutchan11-Oct-12 11:34 
AnswerRe: Best books/Tutorials to learn Java.? Pinmembermjohns0712-Oct-12 5:14 
AnswerRe: Best books/Tutorials to learn Java.? PinmemberGowtham Gutha15-Nov-12 6:54 
GeneralRe: Best books/Tutorials to learn Java.? Pinmembermbatra3115-Nov-12 18:54 
GeneralRe: Best books/Tutorials to learn Java.? PinmemberGowtham Gutha16-Nov-12 7:13 
GeneralRe: Best books/Tutorials to learn Java.? PinmemberGowtham Gutha16-Nov-12 7:14 
QuestionFIX Day Changed Problem PinmembertechGaurav8-Oct-12 21:42 
AnswerRe: FIX Day Changed Problem PinmemberTorstenH.8-Oct-12 22:00 
AnswerRe: FIX Day Changed Problem PinmemberNagy Vilmos11-Oct-12 20:12 
QuestionThreads in details PinmemberCsTreval6-Oct-12 0:16 
See the following code:
Unsafe Counter
public class Counter implements Counting
{
    private int value = 0;
 
    public int incrementAndGet()
    {
        return this.value++;
    }
 
    public int decrementAndGet()
    {
        return this.value--;
    }
}
CounterClient
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
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 PinmemberNagy Vilmos8-Oct-12 0:54 
Questionjava applet security issues PinmemberPonnampi5-Oct-12 18:01 
AnswerRe: java applet security issues PinmemberPonnampi5-Oct-12 18:12 
QuestionData Structures - ArrayDeque <T> extends AbstractList <T> PinmemberMember 94884535-Oct-12 16:29 
AnswerRe: Data Structures - ArrayDeque extends AbstractList PinmemberTorstenH.5-Oct-12 21:36 
QuestionCreating a class that extends more than one class. PinmemberBill.Moo4-Oct-12 2:43 
AnswerRe: Creating a class that extends more than one class. PinmemberNagy Vilmos4-Oct-12 3:00 
GeneralRe: Creating a class that extends more than one class. PinmemberBill.Moo4-Oct-12 3:48 
AnswerRe: Creating a class that extends more than one class. PinmemberPeter_in_27804-Oct-12 3:03 
GeneralRe: Creating a class that extends more than one class. PinmemberBill.Moo4-Oct-12 3:59 
GeneralRe: Creating a class that extends more than one class. PinmemberPeter_in_27804-Oct-12 11:36 
AnswerRe: Creating a class that extends more than one class. Pinmemberpasztorpisti4-Oct-12 12:14 
QuestionRe: Creating a class that extends more than one class. PinmemberMonster Maker21-Oct-12 21:08 
AnswerRe: Creating a class that extends more than one class. PinmemberGowtham Gutha15-Nov-12 6:59 
QuestionThread names PinmemberCsTreval4-Oct-12 1:19 
AnswerRe: Thread names PinmemberNagy Vilmos4-Oct-12 2:54 
AnswerRe: Thread names PinmemberGowtham Gutha15-Nov-12 7:11 
Questionget ethernet IP address Pinmemberhari3013-Oct-12 19:30 
AnswerRe: get ethernet IP address PinmemberTorstenH.3-Oct-12 19:58 
AnswerRe: get ethernet IP address Pinmemberjschell4-Oct-12 11:44 
QuestionJava Based Websevice Pingroupashish121693-Oct-12 1:55 
AnswerRe: Java Based Websevice PinmvpRichard MacCutchan3-Oct-12 2:34 
AnswerRe: Java Based Websevice PinmemberTorstenH.3-Oct-12 2:36 
QuestionHow can I change a 8-bit bitmap into a 4-bit/1-bit bitmap? PinmemberKangvampire2-Oct-12 22:11 
QuestionCreating a Thread PinmemberCsTreval2-Oct-12 8:17 
AnswerRe: Creating a Thread Pinmemberjschell2-Oct-12 8:46 
AnswerRe: Creating a Thread PinmvpRichard MacCutchan2-Oct-12 9:32 
GeneralRe: Creating a Thread PinmemberCsTreval2-Oct-12 20:53 
GeneralRe: Creating a Thread PinmemberTorstenH.2-Oct-12 23:31 
AnswerRe: Creating a Thread PinmemberPeter_in_27802-Oct-12 22:05 
AnswerRe: Creating a Thread PinmemberGowtham Gutha15-Nov-12 7:15 
Questionpure java based code needed to read shedule and send an sms Pinmemberask.sagaram2-Oct-12 7:08 
AnswerRe: pure java based code needed to read shedule and send an sms Pinmemberjschell2-Oct-12 8:50 
GeneralRe: pure java based code needed to read shedule and send an sms Pinmemberask.sagaram3-Oct-12 7:53 
QuestionAWT Native interface PinmemberAhmdAxf1-Oct-12 2:43 
QuestionHardware Pinmemberstib_markc28-Sep-12 18:58 

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

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


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 11 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid