Java数组内容自行更改

时间:2017-07-03 12:34:38

标签: java arraylist data-distribution-service

我有一个程序,它根据点列表(下面的DynamicIoRunnable)从RTI DDS请求值。 DyamicIoRunnable将为预定义列表中的每个点请求一个数据实例,它将数据放入一个列表中,一旦它通过预定义列表中的所有项目,将其传递到处理器线程(下面的DDSProcessor - 这被缩减为相关部分)通过共享的BlockingQueue。此处理器线程将处理列表中的每个项目,并为每个项目创建自定义对象(PointData)。 要调试此问题,DynamicIoRunnable只有一个预定义的点,因此我可以轻松识别它。问题是,我将PointData添加到列表中并在构建时将其打印出来。 add之前的print语句总是显示正确的值,但是当我在每次添加后使用print语句遍历数组时,在几个项之后,之前的项会更改为不同的数据,我不确定如何。任何人都可以看到可以改变的地方?

我的样本结果如下。它显示了几个周期后的第一个损坏(最初添加到列表中的前两个项目完全改变 - 这确实会在更多项目上变得更糟,并且总是更旧的项目,但我看不到关于数据更改的一致模式到,什么时候会改变,或者有多少会改变)。

共享队列

public static BlockingQueue<Object> messageProcQueue = new LinkedBlockingQueue<Object>();

DynamicIoRunnable

 public void run()
{
    try
    {
        List<PointData> temp = new ArrayList();

        for (PredefData a : TrendApp.predefPointList)
        {
            if (a.getDdsHandle() != null)
            {
                Object ioData = dds.getTopicRef().get(pv.getTypeString(a.getPointType().value())).read_io(a.getDdsHandle(), io -> io);

                if (ioData != null)
                {
                    PointData val = new PointData(a.getMachineId(), a.getPointType(), a.getIoId(), a.getSubfield(), a.getConfigId(), ioData);
                    temp.add(val);
                }
            }
        }

        TrendApp.messageProcQueue.put(temp);
    } catch (Exception e)
    {
        errorLog.error("Unable to add dynamic IO to message processing queue");
        e.printStackTrace();
    }
}

DDSProcessor

private static List<PointData> writeBuffer = new ArrayList<PointData>();
if (a instanceof PointData)
                {
                    PointData req = (PointData) a;

                    // Add new point to the buffer

                    System.out.println("Enter Point Data: " + req.getData());
                    writeBuffer.add(req);

                    for (int x = 0; x < writeBuffer.size(); x++)
                    {
                        System.out.println("Building Buffer = " + writeBuffer.get(x).getData());
                    }
}

PointData对象

private int machineId;
private jPointType pointType;
private int ioId;
private String subfield;
private int configId;
private long timestamp;
private Object data;

public PointData(int machineId, jPointType pointType, int ioId, String subfield, int configId, Object data)
{
    this.machineId = machineId;
    this.ioId = ioId;
    this.pointType = pointType;
    this.subfield = subfield;
    this.configId = configId;
    this.data = data;
}

示例结果

Add Point Data to list:
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 974.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 224
value: 0.0

Building Buffer = :
machineId: 1186
ioId: 224
value: 0.0

Building Buffer = :
machineId: 1186
ioId: 224
value: 0.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0


Add Point Data to list: :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 224
value: 0.0

Building Buffer = :
machineId: 1186
ioId: 224
value: 0.0

Building Buffer = :
machineId: 1186
ioId: 224
value: 0.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

Building Buffer = :
machineId: 1186
ioId: 31
value: 956.0

0 个答案:

没有答案