import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Vector; import org.refal.plus.RefalException; public class ParNub { static class Counter { private int value; Counter () { value = 0; } synchronized void increase () { value++; notify(); } synchronized int get () { try { wait(); } catch (InterruptedException e) {} return value; } } @SuppressWarnings("unchecked") public static Object[] nub (Object[] array, int n) { List[] bufs = new ArrayList[n]; for(int i = 0; i < n; i++) bufs[i] = new ArrayList(); for (Object obj : array) bufs[Math.abs(obj.hashCode()) % n].add(obj); final Vector result = new Vector(); final Counter x = new Counter(); for (final List buf : bufs) new Thread() { public void run() { try { result.addAll(Arrays.asList(Nub.Nub(buf.toArray()))); } catch (RefalException _) {} x.increase(); } }.start(); while (x.get() < n); return result.toArray(); } }