Type Compatibility LinkedList


LinkedList<ByteBuffer> dataQueue;
dataQueue = Collections.synchronizedList(new LinkedList<ByteBuffer>());

comes back incompatible type


LinkedList<ByteBuffer> dataQueue;
dataQueue = new LinkedList<ByteBuffer>();

works fine, but I know is not synchronized.


List<ByteBuffer> dataQueue;
dataQueue = Collections.synchronizedList(new LinkedList<ByteBuffer>());

does not give me access to the poll() command that I want.

Will this be safe for the second example above?


      public synchronized void addDataToQueue(ByteBuffer b) {

            dataQueue.add(b);
      }
      

What IDE are you using? I set it up in JBuilder 2005 personal edition and it compiled fine.

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.nio.ByteBuffer;

public class Test {
    public Test() {
    }
    public static void main(String args[]) {
        List<ByteBuffer> dataQueue; 
        dataQueue = Collections.synchronizedList(new LinkedList<ByteBuffer>());     
    }
}

JCreator 3 Pro. very odd

Not odd, the code you’ve compiled is the code the MB said compiled.

It also makes sense since Collection.synchronisedList can’t guarantee its going to return you a LinkedList (since by definition a Java LinkedList class is unsynchronised) and would have to replace it with some wrapped list.

Synchronising a list probably isn’t that tricky, maybe wrap it in your own type instead of using generics, expose only those things you absolutely need (one of the benefits of not using generics, ick!) and hopefully this will make it much easier to consider the synchonisation.

Kev