我可以使用两个在Java中作为锁的对象来实现阻塞队列吗?(Can I implement blocking queue with two objects serving as locks in Java?)
我试图通过实现阻塞队列类来理解Java的synchronized关键字,wait()和notify()。 本文描述了阻塞队列实现之一。 但是我想知道是否可以使用两个对象作为锁来实现阻塞队列? 以下代码是否正确?
public class BlockingQueue { private List<Object> queue = new LinkedList<Object>(); private int limit; private Object slots = new Object(); private Object objs = new Object(); public BlockingQueue(int limit) { this.limit = limit; } private synchronized void enqueue(Object o) throws InterruptedException { if (queue.size() == limit) slots.wait(); objs.notify(); queue.add(o); } private synchronized Object dequeue() throws InterruptedException { if (queue.size() == 0) objs.wait(); slots.notify(); return queue.remove(0); } }
I am trying to understand Java's synchronized keyword, wait(), and notify() by implementing a blocking queue class. One of the blocking queue implementation is described by this article. However I wonder if it is possible to use two objects serving as lock to implement blocking queue? Is the below code correct?
public class BlockingQueue { private List<Object> queue = new LinkedList<Object>(); private int limit; private Object slots = new Object(); private Object objs = new Object(); public BlockingQueue(int limit) { this.limit = limit; } private synchronized void enqueue(Object o) throws InterruptedException { if (queue.size() == limit) slots.wait(); objs.notify(); queue.add(o); } private synchronized Object dequeue() throws InterruptedException { if (queue.size() == 0) objs.wait(); slots.notify(); return queue.remove(0); } }
原文:https://stackoverflow.com/questions/35167262
最满意答案
从这里开始
http://www.microsoft.com/windowsembedded/en-us/develop/windows-embedded-products-for-developers.aspx
搜索MSDN,你会发现你在找什么
start here
http://www.microsoft.com/windowsembedded/en-us/develop/windows-embedded-products-for-developers.aspx
search MSDN, you will find what you are looking for
相关问答
更多-
对于C / C ++ 应用程序开发,Visual Studio 2008和Motorola SDK就足够了。 另一方面,BSP适用于处理内核OAL和设备驱动程序等低级代码的开发人员。 Platform Builder 7.0是Visual Studio 2008的一个插件,可以构建和调试BSP代码,也不是应用程序开发所必需的。 如果您只打算开发C#应用程序,您甚至可能不需要Motorola SDK,但可能只针对Visual Studio中的通用Windows CE设备。 For C/C++ applicat ...
-
我创建了解决方案。 SQL Server Compact Edition由7个dll组成: sqlceme40.dll未记录的本机平面API库(.net System.Data.SqlServerCe.dll程序集是围绕这个DLL的包装器) sqlceca40.dll实现Engine , Replication , Error和其他一些COM对象的COM dll sqlceoledb40.dll一个COM DLL,用于实现SSCE的OLEdb提供程序(允许使用ADO) sqlcese40.dll 未知 sq ...
-
SQL Server Compact是否从Visual Studio 2013停产?(Is SQL Server Compact discontinued from Visual Studio 2013?)[2023-02-02]
是的,SQL Server Compact已被弃用(请参阅此Connect项目的注释 )。 您应该使用SQL Server Express或SQL LocalDB。 一些帖子: http://blogs.msdn.com/b/sqlexpress/archive/2011/07/12/introducing-localdb-a-better-sql-express.aspx http://blogs.msdn.com/b/jerrynixon/archive/2012/02/26/sql-express-v ... -
Windows Embedded Compact 7:开发?(Windows Embedded Compact 7: Developing?? Visual Studio 2010?? Templates?)[2023-01-29]
从这里开始 http://www.microsoft.com/windowsembedded/en-us/develop/windows-embedded-products-for-developers.aspx http://blogs.msdn.com/b/usisvde/archive/2009/08/11/windows-embedded-development-center-updated-on-msdn.aspx 搜索MSDN,你会发现你在找什么 start here http://www.mi ... -
Windows Embedded Compact 2013 - 初始应用程序焦点(Windows Embedded Compact 2013 - Initial Application Focus)[2022-08-28]
没有鼠标点击我无法实现这一点 - 但我使用SendInput来合成鼠标点击,这样用户就不必这样做了。 HRESULT MainPage::OnLoaded(__in IXRDependencyObject* pRoot) { UNREFERENCED_PARAMETER(pRoot); HRESULT hr = InitializeComponent(); if (FAILED(hr)) { goto Error; } if (m_pLo ... -
要为Windows Embedded Compact 2013创建应用程序,需要VS2013或VS2015专业人士使用Application Builder插件和PC上安装的有效SDK。 如果您开发托管应用程序,则还需要检入.NET运行时已经是映像的一部分,或者找到一种方法在设备上永久部署和安装它(实现此目的的方式可能会根据设备配置,注册表类型等等。) To create applications for Windows Embedded Compact 2013 you need VS2013 or VS ...
-
我建议你在注册表中将TCPSendQueueDelay设置为0。 ( https://technet.microsoft.com/en-us/subscriptions/securedownloads/ee495047.aspx )默认设置为4(以毫秒为单位),在我们的应用程序中,当交换许多小数据包时,这会导致严重的延迟。 这显然是WEC7中的一个新设置,在许多小数据包被交换的情况下,导致tcp / ip性能更差。 这将降低某些通信模式的性能,例如相当频繁地发送的大块数据。 Microsoft代表向我们确认 ...
-
在Windows Embedded Compact(Windows CE)7.0中签署CAB文件(Sign CAB file in Windows Embedded Compact (Windows CE) 7.0)[2023-03-07]
有专门用于CAB签名的MSDN部分 。 确切的过程取决于您想要的证书颁发机构 - 这意味着您可以将其发送给Verisign等人并将其签名,或者您可以自签名并随身携带自己的证书。 There's an MSDN section specifically for CAB signing. The exact process depends on the certificate authority you want for it - meaning you can send it to someone like ... -
编译Windows Embedded Compact 7的boost库(Compiling boost library for Windows Embedded Compact 7)[2022-10-08]
看起来这个过程的文档是稀疏的,因为它需要对构建过程进行非平凡的更新才能使用Windows CE。 这个问题最全面的教程似乎就在这里 。 既然你明确地标记了这个boost-asio ,我还专门研究了Boost的这个组件。 这是Boost邮件列表中的一个帖子 ,详细介绍了这个库,包括您可能遇到的潜在故障情况, JAM文件修改以及帮助您构建的批处理文件 。* 对于那些阅读此内容的人: 请在您开始工作后分享您的经验。 尽管各种担心Boost对于嵌入式应用程序来说太过重要,但 Boost提供了使用单独的软件包来满足您 ... -
我有完全相同的问题。 我的解决方案是为pda安装SDK。 我是MC32N0。 你可以在这里找到sdk I had exactly the same problem. The solution for me was to install the SDK for the pda. Mine was MC32N0. You can find the sdk here