我做了一个小样本来展示我的问题:
package connection;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import controls.PlotValuesContainer;
public class Test
{
private ArrayList<PlotValuesContainer> m_logData;
private Timer m_timer;
public Test()
{
m_logData = new ArrayList<PlotValuesContainer>();
m_timer = new Timer();
m_timer.scheduleAtFixedRate(new UpdateThread(), 500, 500);
}
public static void main(String[] args)
{
new Test();
}
private class UpdateThread extends TimerTask
{
private PlotValuesContainer l_dataContainer = new PlotValuesContainer();
@Override
public boolean cancel()
{
return super.cancel();
}
@Override
public void run()
{
//store time stamp
l_dataContainer.setTimeStamp(java.time.LocalDateTime.now());
//store signal values
final short[] l_Array1to256 = new short[256];
for (int i = 0; i < l_Array1to256.length; i++)
{
l_Array1to256[i] = (short) (Math.random() * Short.MAX_VALUE);
}
l_dataContainer.setSignal(l_Array1to256);
if (m_logData.size() > 0)
{
System.out.println("A - oldest: " + m_logData.get(0).getTimeStamp().toString());
}
m_logData.add(l_dataContainer);
System.out.println("B - newest: " + m_logData.get((m_logData.size() - 1)).getTimeStamp().toString());
for (int i = 0; i < m_logData.size(); i++)
{
System.out.println("C - all: " + m_logData.get(i).getTimeStamp().toString());
}
}
}
}
我得到的是:
B - newest: 2019-11-29T08:15:32.882 C - all: 2019-11-29T08:15:32.882 A - oldest: 2019-11-29T08:15:33.335 B - newest: 2019-11-29T08:15:33.335 C - all: 2019-11-29T08:15:33.335 C - all: 2019-11-29T08:15:33.335 A - oldest: 2019-11-29T08:15:33.836 B - newest: 2019-11-29T08:15:33.836 C - all: 2019-11-29T08:15:33.836 C - all: 2019-11-29T08:15:33.836 C - all: 2019-11-29T08:15:33.836 A - oldest: 2019-11-29T08:15:34.335 B - newest: 2019-11-29T08:15:34.335 C - all: 2019-11-29T08:15:34.335 C - all: 2019-11-29T08:15:34.335 C - all: 2019-11-29T08:15:34.335 C - all: 2019-11-29T08:15:34.335 A - oldest: 2019-11-29T08:15:34.836 B - newest: 2019-11-29T08:15:34.836 C - all: 2019-11-29T08:15:34.836 C - all: 2019-11-29T08:15:34.836 C - all: 2019-11-29T08:15:34.836 C - all: 2019-11-29T08:15:34.836 C - all: 2019-11-29T08:15:34.836
我不是经验丰富的Java程序员...有人知道为什么会这样吗?非常感谢!
private class UpdateThread extends TimerTask
{
private PlotValuesContainer l_dataContainer;
@Override
public boolean cancel()
{
return super.cancel();
}
@Override
public void run()
{
l_dataContainer = new PlotValuesContainer();
//store time stamp
l_dataContainer.setTimeStamp(java.time.LocalDateTime.now());
//store signal values
final short[] l_Array1to256 = new short[256];
for (int i = 0; i < l_Array1to256.length; i++)
{
l_Array1to256[i] = (short) (Math.random() * Short.MAX_VALUE);
}
l_dataContainer.setSignal(l_Array1to256);
if (m_logData.size() > 0)
{
System.out.println("A - oldest: " + m_logData.get(0).getTimeStamp().toString());
}
m_logData.add(l_dataContainer);
System.out.println("B - newest: " + m_logData.get((m_logData.size() - 1)).getTimeStamp().toString());
for (int i = 0; i < m_logData.size(); i++)
{
System.out.println("C - all: " + m_logData.get(i).getTimeStamp().toString());
}
}
}