【Java】线程死锁
2021/7/24 12:16:38
本文主要是介绍【Java】线程死锁,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
以下可能造成死锁的代码是?
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:线程饥饿死锁。
这篇关于【Java】线程死锁的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16Maven资料入门指南
- 2024-11-16Maven资料入门教程
- 2024-11-16MyBatis Plus资料:新手入门教程与实践指南
- 2024-11-16MyBatis-Plus资料入门教程:快速上手指南
- 2024-11-16Mybatis资料入门教程:新手必看指南
- 2024-11-16MyBatis资料详解:新手入门与初级实战指南
- 2024-11-16MyBatisPlus资料:初学者入门指南与实用教程
- 2024-11-16MybatisPlus资料详解:初学者入门指南
- 2024-11-16MyBatisX资料:新手入门与初级教程
- 2024-11-16RESTful接口资料详解:新手入门指南