zoukankan      html  css  js  c++  java
  • 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。得到的教训是,线程中的实例的成员变量应当在其构造函数中初始化。

     
  • 相关阅读:
    hdu 2665 划分树
    概率模型与条件随机场
    shell中各种括号的作用()、(())、[]、[[]]、{}
    今日BBC
    小贝_mysql主从复制作用以及案例
    c++ builder 版CreateAnonymousThread用法
    安卓UI适配限定符
    编译3.10内核 出现错误 “undefined reference to...." 解决方法
    iptables的4表5链(未完)
    已有iptables表的查看
  • 原文地址:https://www.cnblogs.com/myboat/p/13036146.html
Copyright © 2011-2022 走看看