public class Client { public static void main(String[] args) throws Throwable { List a = new ArrayList(); List b = new ArrayList(); new Collector("collector1", a, b).collect(); new Collector("collector2", b, a).collect(); } static class Collector { private String name = ""; private Object lockA = null; private Object lockB = null; public Collector(String name, Object a, Object b) { this.name = name; this.lockA = a; this.lockB = b; } public void collect() { new Thread(new Runnable() { @Override public void run() { synchronized (lockA) { System.out.println(name + " got first..."); try { Thread.currentThread().sleep(10); } catch (InterruptedException e) { } synchronized (lockB) { System.out.println(name + " got all."); } } } }, name).start(); } } }