- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的 Java 代码中使用 BlockingQueue 时存在潜在的竞争条件,我想知道如何修改代码来避免这种情况:
private static BlockingQueue<FileToFTP> ftpQueue = new LinkedBlockingQueue<FileToFTP>();
private static boolean shuttingDown = false;
private static FileToFTP pendingFile = null;
private static int uploadsPending = 0;
private static Thread ftpThread = new Thread(new Runnable() {
public void run() {
try {
for(;;) {
FileToFTP f2f = ftpQueue.take(); // this blocks until there is something to take
// FIXME: If the main thread takes over right
// at this instant, then uploadsRemaining()
// is not correct!!
uploadsPending = 1;
int qs = ftpQueue.size();
logIt(qs, f2f);
pendingFile = f2f;
if(!doUploadFile(f2f.getPath(), f2f.getFilename(), f2f.getRenameTo(), f2f.isBinary())) {
if(shuttingDown) {
log.info("Upload " + f2f + " failed!");
} else {
ftpQueue.offer(f2f); // put it back on to retry later
}
uploadsPending = 0;
} else {
pendingFile = null;
uploadsPending = 0;
}
if(qs == 0) logIt("");
}
} catch (InterruptedException consumed) {
// Allow thread to exit
}
}
});
public static int uploadsRemaining() {
return ftpQueue.size() + uploadsPending;
}
请参阅代码中的“FIXME”注释。谢谢!!
最佳答案
也许我误解了你想要的,但听起来你最好使用 ExecutorService
实际运行事物。您可以使用 Exectors.newSingleThreadExecutor()
创建它们或 Executors.newFixedThreadPool(2)
(2 是它使用的线程数的示例)。
然后您可以 .execute
或 .submit
Runnable
上 ExecutorService
。 submit
将返回 Future<T>
对象可用于跟踪提交给 ExecutorService 的特定作业的状态。
话虽如此,您可能需要创建一个新类来执行此操作,如 Runnable
/Callable
类需要有 FileToFTP
其中的变量。 (旁注:我尽量避免内部类,所以如果您想知道为什么我不建议这样做......)
问题就变成了“我如何知道有多少文件正在等待处理?”不幸的是,唯一简单的方法是使其成为另一个类的静态属性......使用静态 get/set 方法或作为公共(public)/ protected 属性。安 AtomicInteger
是理想的选择,因此您可能需要考虑在调用类中使用它作为 protected 静态类。它具有专用的递增/递减命令以及调整值和返回值的命令。
关于Java 线程竞争条件与 BlockingQueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466855/
我的理解是1.5加入了callable,runnable接口(interface)保持原样,防止世界末日。为什么我不能实例化 ThreadPoolExecutor (core, max, tu, un
我正在制作一个包含两个线程的应用程序:其中一个向 LinkedBlockingQueue 写入一个值,另一个正在读取。我正在使用 ScheduledExecutorService 在某个时间段内以秒为
我正在尝试 Autowiring 参数化的阻塞队列: @Bean(name = "saveProductQueue") public BlockingQueue saveProductQueue()
我正在实现一个程序,其中主线程将各种消息推送到工作线程,而工作线程将工作结果推送回主线程。 为此,我计划使用两个队列,一个用于推送到工作线程,另一个用于从中拉出。 据我了解,线程会缓存对象,因此如果它
我正在使用 RabbitMQ,它默认为消费者使用 LinkedBlockingQueue 。它有一个阻塞的 nextDelivery() 方法,该方法基本上调用队列上的 take() 。 但是如果在调
我有 n 个生产者线程通过 BlockingQueue 为 1 个消费者线程提供数据。我正在使用 .put 和 .take (后者当 .peek != null 时)。除了明显在传输过程中不可避免的数
我已经实现了一个带有套接字线程池的两人游戏。每个玩家都连接到自己的线程。我按照this添加了一个消息队列系统文章。 问题是消息滞后。第一个玩家的第一个响应会按预期添加到 messageQueue 中。
据我所知,BlockingCollection 使用非繁忙等待,这是对新项目/回调的通知。所以我不明白它是如何阻塞的,但我认为我可能混合了阻塞的线程和阻塞的共享对象访问? 最佳答案 这是一个很好的解释
我的 Android 应用程序有一个长时间运行的后台服务,据我所知,它在应用程序的主线程中运行,因此,任何耗时或阻塞的任务都应移至单独的线程。 现在,情况是这样的,我不明白/困惑: 当我从一个 Act
我在数据库前面使用 LinkedBlockingQueue。一个线程写入队列,另一个线程从队列读取。 我认为两个并发写入是不可能的。但是是否有可能一个线程写入而另一个线程同时从队列中读取呢?如果没有,
我有一个在单个后台线程上处理工作事件的 BlockingQueue。各种线程调用 add 将一些工作添加到队列中,单个后台线程调用 take 获取工作并一次处理一个。最终可能是停止处理工作的时候了,我
我在多线程系统中使用 BlockingQueue,其中同步块(synchronized block)将项目添加到列表中。有时它不会将项目添加到列表中,它遗漏的项目是随机的。我尝试将以下行添加到代码中,
我有以下阻塞队列; final BlockingQueue blockingQueue = new LinkedBlockingQueue(); 在哪里 public class Message
这个问题的标题让我怀疑这是否存在,但仍然: 我感兴趣的是是否有 Java 的 BlockingQueue 的实现,它受大小限制,从不阻塞,而是在尝试入队太多元素时抛出异常。 编辑 - 我将 Block
假设我有 BlockingQueue 并且一些线程被称为 take() 但此时队列是空的。假设我以某种方式知道将来不会有新元素出现在队列中。如何释放那些被称为 take() 的线程等待?谢谢! p
我想要一个线程安全的容器,它会阻止调用者,直到有项目可用为止。项目将以每秒 1000 秒的速度添加到此容器中,但不会以相同的速度排出。因此,我希望容器不允许重复。我围绕 LinkedBlockingQ
我正在使用 BlockingQueue(LinkedBlockingQueue) 在多个线程之间同步数据。请看下图。 主线程是一个生产者,它生产对象,然后将它们放入每个消费者的队列中(线程2-10)。
我有一个容量为 1 的 BlockingQueue。它存储收到的股票的最后价格。价格保留在队列中,直到客户端轮询队列。然后,我有一个名为 getLatestPrice() 的方法,它应该返回股票的最新
我的 Java 代码中使用 BlockingQueue 时存在潜在的竞争条件,我想知道如何修改代码来避免这种情况: private static BlockingQueue ftpQueue = ne
我有一种情况需要 BlockingQueue 上的方法 peekWait。我会将此方法描述为 retrieves but not remove the head of the queue, waiti
我是一名优秀的程序员,十分优秀!