适用于非线程安全代码的执行器 [英] Executor suitable for non thread-safe code

查看:74
本文介绍了适用于非线程安全代码的执行器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用线程池开发一些最终将成为多线程的代码

I am developing some code that will eventually be multithreaded, using a thread pool Executor. The tasks executed by the thread pool will make call-backs and (sometimes) submit further tasks to the task queue. I would like to develop the code single threaded first, get it right (I'm using Test Driven Development) and only then make the alterations to ensure thread safety (locks, etc). To do that, I need an Executor that is safe to use with non thread-safe code.

思考,这意味着我需要一个单线程的 Executor .也就是说,它导致所有工作都由调用线程完成.JRE是否提供这样的 Executor ?还是可以将其 Executor 之一配置为在该模式下运行?

I think that means I need an Executor that is single-threaded. That is, it is causes all work to be done by the calling thread. Does the JRE provide such an Executor? Or is it possible to configure one of its Executors to operate in that mode?

我已经在使用 谦虚对象 测试模式来测试我的大多数代码-线程化.但是,我的某些代码必须 Executor 计划并重新提交任务,这样做会很简单.测试 代码是这里的挑战.这些任务将更新一个共享对象,该共享对象保存其结果和输入数据.我想延迟使共享对象成为线程安全的,直到我实现并调试了调度和重新提交代码.

I am already using the Humble Object testing pattern to test most of my code single-threaded. However, some of my code must interact with an Executor, or perhaps an ExecutorService, because it is about scheduling and resubmission of tasks, and it will do so in a non-trivial manner. Testing that code is the challenge here. The tasks update a shared object, which holds their results and input data. I want to delay having to make that shared object thread-safe until I have the scheduling and resubmission code implemented and debugged.

推荐答案

如果代码真的只需要 ,则

If the code really needs only an Executor, and not a (much more complex) ExecutorService, it is easy to implement your own single-threaded executor that does precisely what is needed. The API documentation of Executor even shows you how to do so:

class DirectExecutor implements Executor {
   public void execute(Runnable r) {
     r.run();
   }
}


如果代码确实需要 ExecutorService ,则可能线程安全保证:


If the code does need an ExecutorService, it is possible that the single thread executor provided byExecutors.newSingleThreadExecutor() is adequate for testing the non thread-safe code, despite the resulting program having two threads (the thread running the unit tests and the single thread-pool thread of the ExecutorService). This is because an ExecutorService must provide the following thread-safety guarantees:

    在将 Runnable Callable 任务提交给 ExecutorService happen-before 操作>该任务执行的任何操作,
  • 依次通过 Future.get()检索结果.
  • Actions in a thread prior to the submission of a Runnable or Callable task to an ExecutorService happen-before any actions taken by that task,
  • which in turn happen-before the result is retrieved via Future.get().

因此,如果运行单元测试的线程对所有提交的任务执行 Future.get(),则对任何共享对象的所有更改都将被安全地发布,并且运行单元的线程测试可以安全地检查那些共享对象.

Therefore, if the thread running the unit tests does a Future.get() for all the submitted tasks, all changes to any shared objects will have been safely published, and the thread running the unit tests may safely examine those shared objects.

这篇关于适用于非线程安全代码的执行器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆