springboot中类成员生成位置的疑问

在idea中将一个旧项目改造为springboot框架,发现一个非常奇怪的现象。原项目为桌面项目,用的JDK为是jdk 1.4,在去掉界面后,将其做为一个线程加入到系统中。其主类代码如下:

@SpringBootApplication
public class SrtiApplication {
   Runnable runner = new Runnable(){ 

    @Override
    public void run() {
      // TODO Auto-generated method stub
        Transport.main(null);//旧的主程序在这里启动
      }

    };

    Thread mt = new Thread(runner);

    mt.start();

public static void main(String[] args) {
        SpringApplication.run(SrtiApplication.class, args);
    }

}

Transport类中,有一个Vector类成员_fedtable。另一个线程给_fedtable添加数据,而主线程中提取并处理数据。

public class Transport{

    Vector _fedtable = new FederateTable();
    ''''''
    void main(String[] args){
        Message mes = callbackQueue.dequeue();
        mes.dispatch();  
    }

    private class Message{
            int val;
            public Message(int value){
                val = value;
             }
             public dispatch(){
                for(int i =0 ; _fedtable.size(); i++){
                    ....}
  }        

发现这里size总为0,后来将_fedtable = new FederateTable();放到构造函数中,发现其不再为0

分析原因,很难说不是IDEA的一个BUG。得到的教训是,线程中的实例的成员变量应当在其构造函数中初始化。

 
原文地址:https://www.cnblogs.com/myboat/p/13036146.html