I’m writing a java GUI that performs certain operations through the serial port. Since I don’t want it to block while waiting for a response I figured having a thread do it was a better way, but now I’m faced with two options and I don’t know which would be more efficient:
- Case 1: Create a single thread for each asynchronous task.
- Case 2: Keep a single thread sleeping and have it perform different tasks by changing an atomic integer’s value and then interrupting it.
None of these operations will be processor intensive, but I still wonder if it’s wise to keep a thread in memory permanently.
I would use a single threaded ExecutorService like
You may want a different thread for reading and writing to the serial device.