0

Threads add a lot of verbal to the code and make it harder to understand and reason about. Look at this code for example:

public class ConnectionListener implements Runnable {
            
    private Thread thread;
    private boolean running;
    
    public void start() {
        if (!running) {
            thread = new Thread(this);
            thread.start();
        }
    }
    
    public void stop() {
        if (running) {
            running = false;
            thread.interrupt();
        }
    }
    
    @Override
    public void run() {
        running = true;
        
        while (running) {
            // Do some crap
        }
    }
    
}

The whole concern of this class should be listening for connection requests from the network. But look how many lines of code are added just for managing a thread. Is there any way to make this code cleaner?! I don't want to see the thread = new Thread();, not the thread variable and not any of the stop()/start() methods!

Of course I know what the ExecutorService is... But what if I want to manage a long-running thread? By long-running thread, I mean a thread with a life cycle long as the application's life cycle.

Do you have any good solution for me? A way to remove the thread creation and management concerns from a class without making the class extend another class?

InSaNiTy
  • 150
  • 1
  • 9
  • 3
    I'm looking at the number of lines, but don't see anything especially problematic? You're not the JVM, so you don't care how many lines of code the boilerplate is: it's just boilerplate code. As long as the code follows java conventions and is easy enough to read, using a good code editor lets you fold the parts you don't care about, while keeping the parts you do expanded. And the JVM sure doesn't care, because why would it? The compiled code is nowhere near as many lines. – Mike 'Pomax' Kamermans Jan 08 '22 at 23:40
  • 1
    You can maintain an `ExecutorService` object for the entire lifecycle of your app just as well as you can maintain a `Thread` object. And an `ExecutorService` [can be single-threaded](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/concurrent/Executors.html#newSingleThreadExecutor()) if you wish. – Basil Bourque Jan 08 '22 at 23:42
  • @BasilBourque Good solution. – InSaNiTy Jan 08 '22 at 23:53
  • @BasilBourque I accept this as an answer for me. Thank you. – InSaNiTy Jan 08 '22 at 23:59
  • 1
    I suggest you write and accept an Answer to your own Question, explaining your solution, for the sake of posterity. – Basil Bourque Jan 09 '22 at 04:50

1 Answers1

0

I solved the problem by using a single-threaded executor service. I've also read about the performance differences between Plain Thread, ThreadPool and SingleThreadExecutor - SingleThreadExecutor VS plain thread.

Using a single thread executor allows me to start a single thread and manage it using its Future. See code example:

public void func(String[] args) {
   ExecutorService es = Executors.newSingleThreadExecutor();
   Future<?> f = es.submit(Some Runnable);
}

Thanks to @BasilBourque that gave me this solution in the comments.

InSaNiTy
  • 150
  • 1
  • 9