public interface Transfer {
void action(Transfer transfer);
}
public class TransferImp implements Transfer {
public synchronized action(Transfer transfer) {
synchronized (transfer) {
...
}
}
}
public class TransferThread implements Runnable {
private Transfer from;
private Transfer to;
public TransferThread(Transfer from, Transfer to) {
this.from = from;
this.to = to;
}
public void run() {
from.action(to);
}
}
public class Test {
public void static main(String[] args) {
// 如果是Web环境下你只要将地下申明的a,b,c,e,f对象放入全局就行(如static,application,session等),根据你实际情况
Transfer a = new TransferImp();
Transfer b = new TransferImp();
Transfer c = new TransferImp();
Transfer e = new TransferImp();
Transfer f = new TransferImp();
Thread t1 = new Thread(new TransferThread(a,b));
Thread t2 = new Thread(new TransferThread(c,b));
Thread t3 = new Thread(new TransferThread(e,f));
t1.start();
t2.start();
t3.start();
}
}