二仪式 2016-08-12 03:06 采纳率: 0%
浏览 2492

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条回答 默认 最新

  • wolf犭良 2016-08-12 04:44
    关注

    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
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制