u013894427
二仪式
2016-08-12 03:06

Java多线程 final和static变量

写了一个多线程中有关final和static的例子,遇到问题了。

 public class MyThread implements Runnable
{
    int i = 0;
    int a = 20;
    static int b = 20;
    static int c = 20;
    static int d = 20;
    final int finalIntNoStatic = a++;
    static int staticInt = b++;
    final int finalInt = c++;
    static final int STATIC_FINAL_INT = d++;

    @Override
    public void run()
    {
        while (i < 10)
        {
            System.out.println("i=" + i + ",finalIntNoStatic=" + finalIntNoStatic + ",a=" + a
                    + ",staticInt=" + staticInt + ",b=" + b + ",finalInt=" + finalInt + ",c=" + c
                    + ",STATIC_FINAL_INT=" + STATIC_FINAL_INT + ",d=" + d);
            i++;
        }
    }
}
 public class MultiThread 
{
    public static void main(String[] args)
    {

        for (int i = 0; i < 5; i++)
        {
            Thread aThread=new Thread(new MyThread());
            aThread.start();
        }
        System.out.println("Run in main thread");
    }
}

最后打印结果为:

 i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
Run in main thread
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=25,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=25,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,c=25,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=25,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=25,STATIC_FINAL_INT=20,d=21

我的问题是为什么c的值一直是25,,编译和运行过程中finalInt和c的赋值顺序是怎样的

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

5条回答

为你推荐

换一换