Click here to Skip to main content
12,631,123 members (32,686 online)
   

Java

 
QuestionBest books/Tutorials to learn Java.? Pin
mbatra3111-Oct-12 1:39
membermbatra3111-Oct-12 1:39 
AnswerRe: Best books/Tutorials to learn Java.? Pin
pasztorpisti11-Oct-12 2:02
memberpasztorpisti11-Oct-12 2:02 
AnswerRe: Best books/Tutorials to learn Java.? Pin
TorstenH.11-Oct-12 3:32
memberTorstenH.11-Oct-12 3:32 
GeneralRe: Best books/Tutorials to learn Java.? Pin
mbatra3111-Oct-12 4:38
membermbatra3111-Oct-12 4:38 
GeneralRe: Best books/Tutorials to learn Java.? Pin
Richard MacCutchan11-Oct-12 12:34
mvpRichard MacCutchan11-Oct-12 12:34 
AnswerRe: Best books/Tutorials to learn Java.? Pin
mjohns0712-Oct-12 6:14
membermjohns0712-Oct-12 6:14 
AnswerRe: Best books/Tutorials to learn Java.? Pin
Gowtham Gutha15-Nov-12 7:54
memberGowtham Gutha15-Nov-12 7:54 
GeneralRe: Best books/Tutorials to learn Java.? Pin
mbatra3115-Nov-12 19:54
membermbatra3115-Nov-12 19:54 
GeneralRe: Best books/Tutorials to learn Java.? Pin
Gowtham Gutha16-Nov-12 8:13
memberGowtham Gutha16-Nov-12 8:13 
GeneralRe: Best books/Tutorials to learn Java.? Pin
Gowtham Gutha16-Nov-12 8:14
memberGowtham Gutha16-Nov-12 8:14 
QuestionFIX Day Changed Problem Pin
techGaurav8-Oct-12 22:42
membertechGaurav8-Oct-12 22:42 
AnswerRe: FIX Day Changed Problem Pin
TorstenH.8-Oct-12 23:00
memberTorstenH.8-Oct-12 23:00 
AnswerRe: FIX Day Changed Problem Pin
Nagy Vilmos11-Oct-12 21:12
memberNagy Vilmos11-Oct-12 21:12 
QuestionThreads in details Pin
CsTreval6-Oct-12 1:16
memberCsTreval6-Oct-12 1: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 Pin
Nagy Vilmos8-Oct-12 1:54
memberNagy Vilmos8-Oct-12 1:54 
Questionjava applet security issues Pin
Ponnampi5-Oct-12 19:01
memberPonnampi5-Oct-12 19:01 
AnswerRe: java applet security issues Pin
Ponnampi5-Oct-12 19:12
memberPonnampi5-Oct-12 19:12 
QuestionData Structures - ArrayDeque <T> extends AbstractList <T> Pin
Member 94884535-Oct-12 17:29
memberMember 94884535-Oct-12 17:29 
AnswerRe: Data Structures - ArrayDeque extends AbstractList Pin
TorstenH.5-Oct-12 22:36
memberTorstenH.5-Oct-12 22:36 
QuestionCreating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 3:43
memberBill.Moo4-Oct-12 3:43 
AnswerRe: Creating a class that extends more than one class. Pin
Nagy Vilmos4-Oct-12 4:00
memberNagy Vilmos4-Oct-12 4:00 
GeneralRe: Creating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 4:48
memberBill.Moo4-Oct-12 4:48 
AnswerRe: Creating a class that extends more than one class. Pin
Peter_in_27804-Oct-12 4:03
memberPeter_in_27804-Oct-12 4:03 
GeneralRe: Creating a class that extends more than one class. Pin
Bill.Moo4-Oct-12 4:59
memberBill.Moo4-Oct-12 4:59 
GeneralRe: Creating a class that extends more than one class. Pin
Peter_in_27804-Oct-12 12:36
memberPeter_in_27804-Oct-12 12:36 
AnswerRe: Creating a class that extends more than one class. Pin
pasztorpisti4-Oct-12 13:14
memberpasztorpisti4-Oct-12 13:14 
QuestionRe: Creating a class that extends more than one class. Pin
Monster Maker21-Oct-12 22:08
memberMonster Maker21-Oct-12 22:08 
AnswerRe: Creating a class that extends more than one class. Pin
Gowtham Gutha15-Nov-12 7:59
memberGowtham Gutha15-Nov-12 7:59 
QuestionThread names Pin
CsTreval4-Oct-12 2:19
memberCsTreval4-Oct-12 2:19 
AnswerRe: Thread names Pin
Nagy Vilmos4-Oct-12 3:54
memberNagy Vilmos4-Oct-12 3:54 
AnswerRe: Thread names Pin
Gowtham Gutha15-Nov-12 8:11
memberGowtham Gutha15-Nov-12 8:11 
Questionget ethernet IP address Pin
hari3013-Oct-12 20:30
memberhari3013-Oct-12 20:30 
AnswerRe: get ethernet IP address Pin
TorstenH.3-Oct-12 20:58
memberTorstenH.3-Oct-12 20:58 
AnswerRe: get ethernet IP address Pin
jschell4-Oct-12 12:44
memberjschell4-Oct-12 12:44 
QuestionJava Based Websevice Pin
ashish121693-Oct-12 2:55
groupashish121693-Oct-12 2:55 
AnswerRe: Java Based Websevice Pin
Richard MacCutchan3-Oct-12 3:34
mvpRichard MacCutchan3-Oct-12 3:34 
AnswerRe: Java Based Websevice Pin
TorstenH.3-Oct-12 3:36
memberTorstenH.3-Oct-12 3:36 
QuestionHow can I change a 8-bit bitmap into a 4-bit/1-bit bitmap? Pin
Kangvampire2-Oct-12 23:11
memberKangvampire2-Oct-12 23:11 
QuestionCreating a Thread Pin
CsTreval2-Oct-12 9:17
memberCsTreval2-Oct-12 9:17 
AnswerRe: Creating a Thread Pin
jschell2-Oct-12 9:46
memberjschell2-Oct-12 9:46 
AnswerRe: Creating a Thread Pin
Richard MacCutchan2-Oct-12 10:32
mvpRichard MacCutchan2-Oct-12 10:32 
GeneralRe: Creating a Thread Pin
CsTreval2-Oct-12 21:53
memberCsTreval2-Oct-12 21:53 
GeneralRe: Creating a Thread Pin
TorstenH.3-Oct-12 0:31
memberTorstenH.3-Oct-12 0:31 
AnswerRe: Creating a Thread Pin
Peter_in_27802-Oct-12 23:05
memberPeter_in_27802-Oct-12 23:05 
AnswerRe: Creating a Thread Pin
Gowtham Gutha15-Nov-12 8:15
memberGowtham Gutha15-Nov-12 8:15 
Questionpure java based code needed to read shedule and send an sms Pin
ask.sagaram2-Oct-12 8:08
memberask.sagaram2-Oct-12 8:08 
AnswerRe: pure java based code needed to read shedule and send an sms Pin
jschell2-Oct-12 9:50
memberjschell2-Oct-12 9:50 
GeneralRe: pure java based code needed to read shedule and send an sms Pin
ask.sagaram3-Oct-12 8:53
memberask.sagaram3-Oct-12 8:53 
QuestionAWT Native interface Pin
AhmdAxf1-Oct-12 3:43
memberAhmdAxf1-Oct-12 3:43 
QuestionHardware Pin
stib_markc28-Sep-12 19:58
memberstib_markc28-Sep-12 19:58 

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.


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 5 Dec 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid