Rudy Broersma
Rudy Broersma

Reputation: 115

Java / Android: LinkedList race condition

I am using a LinkedList in an android application, where I encounter race conditions. 1 thread adds data to the LinkedList while another removes them when they are retrieved. I encountered the siuation where my newly added objects are never handled.

I Googled for Java synchronized objects, but either I am thinking too easy or I am missing something. From what I've read "Object synchronization" is the more difficult of the two, so I was wondering if just enclosing a piece of code with sychronized(object) { }; is enough?

So, I have:

public void function addMove(Object move) {
  synchronized(list) {
    list.add(move);
  };
};

public void function second() {
   synchronized(list) {
   // iterate through the list
   list.clear;
  };
};

Is this really all I need?

Upvotes: 1

Views: 1104

Answers (4)

Zagrev
Zagrev

Reputation: 2020

I don't know if Android supports Java 5, but java.util.concurrent contains lots of excellent classes to support queues, such as ConcurrentLinkedQueue.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

- In your case if your list is properly coupled with the Object and there is no other way to access it except this only method then i think that would be enough.

- Moreover the below approach can also be used:

public class Test{

 private LinkedList<Object> list = new LinkedList<Object>();

 public void function addMove(Object move) {
  synchronized(this) {
    list.add(move);
  }
}

public void function second() {
   synchronized(this) {
   list.clear;
  }
 }


}

//////////////////////////////////Edited Part///////////////////////

- Synchronization is done to protect the crucial state of data and are applied on to the method or atomic statements accessing that field whose data is to be protected.

- Every object has one and only one lock, which is to be obtained by a thread to access the synchronized methods or atomic statements which has access to its instance variables.

- Similarly every Class has one and only one lock, which is to be obtained by a thread to access the synchronized methods or atomic statements which has access to its static variable.

- Now when an thread gains access over the lock of an object then after that it gets the access to its synchronized methods or atomic statements, and if at that time another thread tries to access the same object lock, then that thread is denied the access and it moves to the blocked state.

Upvotes: 3

Steve Benett
Steve Benett

Reputation: 12933

Yes this is the way how you can control the access of threads for your object. All you have to do is to look on the timing of your methods. The clear command should only be called if there was data added to the list.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691933

If the list is properly encapsulated into your object (i.e. no other object has access to the list), and if these are the only methods accessing the list, then yes, this is all you need.

The key is that every access to the list must be done in a synchronized block. And every synchonized block must synchronize on the same object (list, in your code example).

Upvotes: 1

Related Questions