class TestThreadMethod extends Thread{ 
        public static int shareVar = 0; 
        public TestThreadMethod(String name){ 
            super(name); 
        } 
        public synchronized void run(){ 
            for(int i=0; i <=40; i++){  //每个线程sleep以后,重新进入,那里保存了上一次本线程的i值
                                         //如果是栈,那么只能取栈顶元素。那么肯定不行吧??
                System.out.print(Thread.currentThread().getName()); 
               
                System.out.println(" : " + i); 
                   if(i==40) System.out.print(Thread.currentThread().getName()
                                                            +"  over\n**********************\n");
               try{ Thread.sleep(20); }  
                      catch(InterruptedException e) {}
            } 
      
        } 
    } 
 class TestThread{ 
        public static void main(String[] args){ 
            TestThreadMethod t1 = new TestThreadMethod("t1"); 
            TestThreadMethod t2 = new TestThreadMethod("t2"); 
            TestThreadMethod t3 = new TestThreadMethod("t3"); 
            
            t1.start(); 
            t2.start();
            t3.start();
            
            t1.setPriority(Thread.MAX_PRIORITY);
        } 

解决方案 »

  1.   

    你的t1 t2 t3分别有自己的栈  他们是异步线程
    每次操作只取出自己的i 然后++ 没有问题啊
      

  2.   

    3.5.2 Java Virtual Machine Stacks
    Each Java virtual machine thread has a private Java virtual machine stack, created at the same time as the thread.3 A Java virtual machine stack stores frames (§3.6). A Java virtual machine stack is analogous to the stack of a conventional language such as C: it holds local variables and partial results, and plays a part in method invocation and return. Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous. 
    The Java virtual machine specification permits Java virtual machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java virtual machine stacks are of a fixed size, the size of each Java virtual machine stack may be chosen independently when that stack is created. A Java virtual machine implementation may provide the programmer or the user control over the initial size of Java virtual machine stacks, as well as, in the case of dynamically expanding or contracting Java virtual machine stacks, control over the maximum and minimum sizes.4 The following exceptional conditions are associated with Java virtual machine stacks:
    If the computation in a thread requires a larger Java virtual machine stack than is permitted, the Java virtual machine throws a StackOverflowError. If Java virtual machine stacks can be dynamically expanded, and expansion is attempted but insufficient memory can be made available to effect the expansion, or if insufficient memory can be made available to create the initial Java virtual machine stack for a new thread, the Java virtual machine throws an OutOfMemoryError.