TimerTask 中 ArrayList 的 add() 导致所有元素都相同
add() of ArrayList in TimerTask results in all elements being the same
我尝试向 TimerTask 中的 ArrayList 添加一些值。
但是在 TimerTask 的下一个实例中,所有先前添加的元素都设置为与当前元素相同。
我做了一个小样本来说明我的问题:
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 程序员...
有人知道为什么会这样吗?
非常感谢!
将 l_dataContainer 的实例化移动到 运行 方法中
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());
}
}
}
我尝试向 TimerTask 中的 ArrayList 添加一些值。 但是在 TimerTask 的下一个实例中,所有先前添加的元素都设置为与当前元素相同。
我做了一个小样本来说明我的问题:
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 程序员... 有人知道为什么会这样吗?
非常感谢!
将 l_dataContainer 的实例化移动到 运行 方法中
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());
}
}
}