a 是全局变量 肯定是共享的,
  for(int i=0;i<20;i++)  20次执行太快了
你把数调大一点,执行看下效果

解决方案 »

  1.   

    那是因为你的i一直都是0, 把i改成从1开始试试。 
    for(int i=1;i<20;i++)
      

  2.   

    共不共享换个简单的写法就能看到了啊
    public class Machine implements Runnable {
        private int a = 1; // t1和t2是否共享这个a?    public void run() {
            a++;
            System.out.println(a);    }    public static void main(String[] args) throws InterruptedException {
            Machine machine = new Machine();
            Thread t1 = new Thread(machine);
            Thread t2 = new Thread(machine);
            t1.start();
            t2.start();
        }
    }
    结果:
    2
    3
      

  3.   

    程序本身没问题,你这样试下:在Thread.yield();之前加入Thread.sleep(1000);
      

  4.   

    这个代码执行下来结果肯定是这样的。原因是
    a+=i;
    Thread.yield();
    a-=i;
    这三句交替执行了。其实a的值是有改变的,但是每次在打印的时候a又修改回1了。
    yield方法造成的结果。
    如果你为了达到你目的你可以修改成#7的代码。
      

  5.   

    把Thread.yield(); a-=i;去掉,楼主就好理解了
    关于多线程概念,http://www.itzlk.com/io/index.jhtml讲的比较详细
      

  6.   

    WindowsIn the Hotspot implementation, the way that Thread.yield() works has changed between Java 5 and Java 6. In Java 5, Thread.yield() calls the Windows API call Sleep(0). This has the special effect of clearing the current thread's quantum and putting it to the end of the queue for its priority level. In other words, all runnable threads of the same priority (and those of greater priority) will get a chance to run before the yielded thread is next given CPU time. When it is eventually re-scheduled, it will come back with a full full quantum, but doesn't "carry over" any of the remaining quantum from the time of yielding. This behaviour is a little different from a non-zero sleep where the sleeping thread generally loses 1 quantum value (in effect, 1/3 of a 10 or 15ms tick). In Java 6, this behaviour was changed. The Hotspot VM now implements Thread.yield() using the Windows SwitchToThread() API call. This call makes the current thread give up its current timeslice, but not its entire quantum. This means that depending on the priorities of other threads, the yielding thread can be scheduled back in one interrupt period later. (See the section on thread scheduling for more information on timeslices.) Thread.yield();
    这个就不是个靠谱的东西。不要考虑多线程先后顺序的问题,没准的。使用这个方法是为了让CPU有时间thread scheduling。这个可以解释为什么不同电脑打出来的不一样。
    t1和t2是共享这个a,其他人已经解释清楚了。
      

  7.   

    我修改了一下程序:
    public class Machine implements Runnable
    {
       private int a=1;   //t1和t2是否共享这个a?
       public void run()
       {
          for(int i=0;i<20;i++)
          {
             System.out.println(Thread.currentThread().getName()+
                                ","+"a+"+i+"="+(a+=i));
             Thread.yield();
             System.out.println(Thread.currentThread().getName()+
                                ","+"a-"+i+"="+(a-=i));
             System.out.println(Thread.currentThread().getName()+","+"a="+a);
          }
       }
       public static void main(String[] args) throws InterruptedException
       {
         Machine machine=new Machine();
         Thread t1=new Thread(machine);
         Thread t2=new Thread(machine);
         t1.start();
         t2.start();
        }
    }运行后发现打印结果中有以下内容:
    Thread-0,a+1=2
    Thread-1,a+1=3说明共享了,谢谢大家!