0

I have an ArrayList of LinkedList to store my sensors captured data in a thread ( using synchronized on the ArrayList object)

upon capture, sensor events are added, I can see the linkedlist increasing, but the first and last element are ALWAYS the last capture event .. something is wrong somewhere ... ?

// main thread  ('samplingFifoQueues'  passed as an arg to be synchronized ...)
    public volatile LinkedList<SensorEvent>  accelFifoQueue = new LinkedList<SensorEvent>();
    public volatile LinkedList<SensorEvent>  magnetFifoQueue = new 
    .......

    public volatile ArrayList<LinkedList<SensorEvent>> samplingFifoQueues = new ArrayList<LinkedList<SensorEvent>>();

In my capture thread , I add the sensor events

    LinkedList<SensorEvent>  accelFifoQueue;
    LinkedList<SensorEvent>  magnetFifoQueue;
    ......

    ArrayList<LinkedList<SensorEvent>> samplingFifoQueues;

    .....
    public void onSensorChanged(SensorEvent sensorEvent) {  
      ...     
      synchronized (samplingFifoQueues)  {
           switch (sensorEvent.sensor.getType())  {
           case Sensor.TYPE_ACCELEROMETER:  
              Log.d(TAG, "new accel sensor event at: " + sensorEvent.timestamp );
              accelFifoQueue.add(sensorEvent);
              SensorEvent  nse = accelFifoQueue.getLast();
              SensorEvent  lse = accelFifoQueue.getFirst();
              Log.d(TAG, "accel: " + accelFifoQueue.size() + " elements,  last at: " + nse.timestamp + " , first at: " + lse.timestamp);
              break;
              ....

The log output indicate first and last timestamp after each new event , and the first is ALWAYS = to the last, even if the size is increasing :

new accel sensor event at: 1391793870096061895
accel: 1 elements,  last at: 1391793870096061895 , first at: 1391793870096061895

new accel sensor event at: 1391793870117302130
accel: 2 elements,  last at: 1391793870117302130 , first at: 1391793870117302130

new accel sensor event at: 1391793870121208380
accel: 3 elements,  last at: 1391793870121208380 , first at: 1391793870121208380

new accel sensor event at: 1391793870129020880
accel: 4 elements,  last at: 1391793870129020880 , first at: 1391793870129020880
  • is it possible that the sensorEvent object is always the same? can you look at it's hashCode? – njzk2 Feb 07 '14 at 17:56
  • found the issue in post : http://stackoverflow.com/questions/13164107/how-to-make-a-copy-of-the-android-sensor-sensorevent-object –  Feb 07 '14 at 18:21

2 Answers2

1

as per answer in post : How to make a copy of the Android Sensor SensorEvent Object

the sensorEvent is overwritten... it's a system class so I need to do my own clone object...

Community
  • 1
  • 1
0

This is because you are using ArrayList, and ArrayList was stored sensor event objet's reference, so, all you added to this array list may be the same object's reference, this is why elements in array list are ALWAYS the last capture event.

FarmerLi
  • 330
  • 1
  • 3
  • 12