The consensus, when it comes to multi-threading & concurrency in Java-land, is to isolate the code you want multi-threaded as a “task” and to submit that task to some kind of executor service or thread pool.
What I’m wondering is:
- Can a task be any method of any object, or does it have to be something special (if so, what)?
- How do you determine whether this task is CPU-, IO-bound or other? What are some “dead giveaways” or other deciding factors?
Thanks in advance for any clarity here!
Take a look at
ExecutorService:submit(Callable)submit(Runnable)Your task should probably be one of the two interfaces (which can always call other methods).
Your task is probably IO bound if you perform any input/output operations within it: IE, reading to a file, writing to a file, reading from a socket, writing to a socket, etc.