2013年12月13日星期五

Threading a small problem

public class SecondThread implements Runnable{
    int i = 0;
    public void run() {
        for(i = 0;i < 50;i++){
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
    }
    
    public static void main(String[] args) {
        for(int i = 0;i<50;i++){
            System.out.println(Thread.currentThread().getName()+":"+i);
            if(i == 20){
                Runnable target = new SecondThread();
                new Thread(target, "task1").start();
                new Thread(target, "task2").start();
            }
        }
    }
}

perform the above code , the execution result is such a
task1: 41
task2: 40
task2: 42
on the results of these analyzes , the following analysis
My question is, since the target object in memory references only one in task1 thread executes the i + + i after the memory value becomes 41 , but in turn task2 thread execution output 41 is a time value should not change is 40.
------ Solution ---------------------------- ----------------
Because the landlord did not give you two thread synchronization , causing the thread to run up on the more casual , what situations are likely to occur ,
you say is this:
task2 would be executed print task2: 40, results because System.out.println (Thread.currentThread () getName () + ":". + i); it not the end of the sentence calling (Executive to half ) , the thread time slice is task1 seize ( or due to the time to give up CPU), but because the statement is not executed, there is no print output , the value of i is 40, the thread state is saved , then the value will be saved ( remember , 40 )
good , continue to walk , turn task1 executed , execution + + i, value increases by 1 , so then printed task1: 41.
over time slice to , task2 execution , remove the last saved state of the thread and found i was 40 , executing the print statement (task2: 40), another cycle , this time due to the value programmed i task1 has 41 , then after task2 after + + i, value becomes 42 , the output print task2: 42


------ there is the most important thing is System.out.println (Thread.currentThread () getName () + ":" + i.); this statement is not executed, but the value of i as a thread state is saved , so the next to take to the old values.
------ Solution ---------------------------------------- ----
I know the landlord 's confused , do you think the two threads on this variable , then the value of this variable should not be the order of the case.

This time you have to understand that each thread has a thread stack , for efficiency , using a caching strategy , a copy will be variable to its own thread stack frame , thus failing to ensure that the first time Get the latest updates , or updates of the latest value of the.

So this time if using , volatile keyword , you can avoid this from happening.

If you do not understand , welcome to ask further questions .

can go and see "java Concurrent Programming Guide," will be very clear.
------ For reference only -------------------------------------- -
thread state ? Have not heard , there is information you
------ For reference only ----------------------------- ----------
3 threads execute simultaneously , who grabbed the running time slices are random, what print are likely to , ah, if you're running several times, each print is different landlord or look it
thread execution mechanism

没有评论:

发表评论