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.
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 Executor
s to operate in that mode?
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.
Executor
is perfectly thread-safe, and it isn't going to interact with your threads if the tasks you are submitting don't do so. In other words, it all depends on the type of tasks you are giving to it. – Cosmetic