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个回答

c为什么都是25,因为在main方法中循环只有5次,执行速度非常快,创建了5个MyThread需要的时候可以忽略不计。当创建完后,都还没开始执行子线
程的run方法。但这种结果不是必然的。比如在for循环时,让主线程sleep 100毫秒,你会看到不一样的结果。

     public static void main(String[] args) {
        System.out.println("Run in main thread");
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Run in main thread");
            Thread aThread = new Thread(new MyThread());
            aThread.start();

        }

    }
 Run in main thread
Run in main thread
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=20,c=21,STATIC_FINAL_INT=20,d=21
Run in main thread
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=21,c=22,STATIC_FINAL_INT=20,d=21
Run in main thread
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=22,c=23,STATIC_FINAL_INT=20,d=21
Run in main thread
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=1,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=2,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=3,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=4,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=5,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=6,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=7,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=8,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
i=9,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=23,c=24,STATIC_FINAL_INT=20,d=21
Run in main thread
i=0,finalIntNoStatic=20,a=21,staticInt=20,b=21,finalInt=24,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=2,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=24,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=5,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=24,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=8,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=24,c=25,STATIC_FINAL_INT=20,d=21

u013894427
二仪式 麻烦看一下我后面的回答对不对。谢谢。
3 年多之前 回复
u013894427
二仪式 回复wolf犭良: b和d也是静态变量属于类变量,如果b和d不被重新赋值,那么b和d为什么会在每个线程中都保持21,而不是像c一样递增?
3 年多之前 回复
ldz_wolf
wolf犭良 在创建对象的时候就已经对变量进行赋值了,而run方法是要在thread.start()之后才会被调用。c是静态变量属于类变量,在类被第一次调用就会赋值第一次赋值后,后面再创建对象不会 再赋值,而finalInt每次创建对象是都会被创建并赋值
3 年多之前 回复
u013894427
二仪式 没有延时全是25,是否可以理解为函数运行是在赋值之后才运行的。那么第二个问题,c和finalInt的赋值顺序是怎样的,他俩在各个线程中是不同的?
3 年多之前 回复
 static int b=20;
 static int staticInt=b++;
 //相当于
 static int b=20;
 b=b+1;
 static int staticInt=b;
 //b是静态变量属于类变量,所以第二次及之后不会执行 static int b=20;
 //static int staticInt=b++;属于初始化,只会执行一次,所以staticInt的值一直是20;
 //b=b+1属于staticInt的初始化语句,所以之后也不会执行。所以b的值一直是21
ldz_wolf
wolf犭良 是这样的。
3 年多之前 回复
 static int c=20;
 final int staticInt=c++;
 //相当于
 static int c=20;
 c=c+1;
 final int finalInt=c;
 //c是静态变量属于类变量,所以第二次及之后不会执行 static int c=20;
 //finalInt不是静态变量,所以其初始语句会继续执行。
 //final int finalInt=c++;属于初始化,不止执行一次,所以finalInt的值会一直增大;
 //c=c+1属于finalInt的初始化语句,所以会继续执行。所以c的值一直会一直增大;

  static int d=20;
 final static int STATIC_FINAL_INT=d++;
 //相当于
 static int d=20;
 d=d+1;
 final static int STATIC_FINAL_INT=d;
 //d是静态变量属于类变量,所以第二次及之后不会执行 static int c=20;
 //STATIC_FINAL_INT 是静态变量,所以其初始语句不会继续执行。
 //fina staticl int STATIC_FINAL_INT=d++;属于初始化,只执行一次,所以STATIC_FINAL_INT的值不会改变;
 //d=d+1属于STATIC_FINAL_INT的初始化语句,所以不会继续执行。所以d的值不会改变;

更正:d++应该是赋值之后才进行+1操作的。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问