以下可能造成死锁的代码是?
A.
public class LeftRightLock { private final Object left = new Object(); private final Object right = new Object(); public void functionA() { synchronized (left) { synchronized (right) { doSomething(); } } } public void functionB() { synchronized (right) { synchronized(left) { doSomething(); } } } }
B.
public void transferMony(Account fromAccount, Account toAccount, int amount) { synchronized (fromAccount) { synchronized(toAccount) { fromAccount.debit(amount); toAccount.credit(amount); } } }
C.
public class Taxi { private Point location; private Point destinztion; private final Dispatcher dispatcher; public Taxi(Dispatcher dispatcher) { this.dispatcher = dispatcher; } public synchronized Point getLocation() { return location; } public synchronized void setLocation(Point location) { this.location = location; if (this.location.equals(destinztion)) { dispatcher.notifyAvailable(this); } } } public class Dispatcher { private final Set<Taxi> taxis = new HashSet<>(); private final Set<Taxi> availableTaxis = new HashSet<>(); public synchronized void notifyAvailable(Taxi taxi) { availableTaxis.add(taxi); } public synchronized Image getImage() { final Image image = new Image(); for (final Taxi taxi : taxis) { image.drawMarket(taxi.getLocation()); } return image; } }
D.
private final ExecutorService executor = Executors.newSingleThreadExecutor(); public void renderPage() throws InterruptedException, ExecutionException { Future<String> page = executor.submit(new RenderPageTask()); frame.set(page.get()); } public class RenderPageTask implements Callable<String> { @Override public String call() throws Exception { final Future<String> header = executor.submit(new LoadFileTask("head.html")); final Future<String> foot = executor.submit(new LoadFileTask("foot.html")); return header.get() + "page" + foot.get(); } }
A:锁顺序死锁。
B:动态锁顺序死锁。
C:协作对象之间发生死锁。
D:线程饥饿死锁。