Re: Vector (was Re: Change character in string)
On 2009-03-13, Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> wrote:
Based on how long it took you to spot it, it's quite likely that
RGB fell for the same misread, when he/she tried it in eclipse.
This case closed. (unless RGB speaks up, that is)
The contest is still open, with (now corrected):
? List<Foo> lf= Collections.synchronizedList(new ArrayList<Foo>());
? List<Foo> lf= new Vector<Foo>();
Scores so far:
half a point to the former for being in line with other
Collection implementations' synchronized wrappings.
quarter of a point to the latter for being shorter to write.
half a point to the latter for being "slightly faster" according to Sun.
I am going to take this as an invitation to vent my pet peeve with
Vector again (even if I don't really understand what the contest is
all about). My apologies if it has been mentioned before in this
thread - I'm something of a latecomer to the discussion.
In the context of the above quoted text, the following program works
fine with a synchronized ArrayList but deadlocks with Vector. This has
been the case forever and I just checked it now with JDK6_12 on
Ubuntu: still a deadlock.
Switch the comment around in createList() to deadlock or not deadlock.
As far as I can recall, the difference between the two is that Vector
synchronizes equals() while the synchronizedList() wrapper
doesn't. It's been a while though so don't take that as gospel.
Cheers,
Bent D
import java.util.*;
public class Test
{
public static void main(String args[])
{
List<Integer> a = createList();
List<Integer> b = createList();
populate(a);
populate(b);
Thread t1 = new Thread(new MyRunnable(a, b)); // spot the difference
Thread t2 = new Thread(new MyRunnable(b, a)); // spot the difference
t1.start();
t2.start();
}
private static List<Integer> createList()
{
//return Collections.synchronizedList(new ArrayList<Integer>());
return new Vector<Integer>();
}
private static void populate(List<Integer> l)
{
for (int ii = 0; ii < 1000; ++ii) l.add(ii);
}
static class MyRunnable implements Runnable
{
List<Integer> list1, list2;
public MyRunnable(List<Integer> l1, List<Integer> l2)
{
list1 = l1;
list2 = l2;
}
public void run()
{
for (int ii = 0; ii < 1000; ++ii) list1.equals(list2);
}
}
}
--
Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
powered by emacs