Java

Java

Made by DeepSource

Avoid using deprecated Thread methods JAVA-E1107

Bug risk
Major

Deprecated methods from java.lang.Thread such as Thread.stop() or Thread.suspend() should not be used as they can cause instability.

By using methods like Thread.stop(), any locks held within the affected thread will be released at once, possibly leading to inconsistencies and logical bugs.

This issue will be raised upon usage of the following methods:

Bad Practice

// in main thread
try {
    someThread.stop();
} catch (...) {
    ...
}

Recommended

The main thread can call a function on the worker side that determines if the worker should exit.

class Main {
    // ...

    // in main thread:
    if (shouldQuitWorker) {
        worker.stop();
    }

    // ...
}

In the worker thread, you could check a flag to see if you should stop the thread.

class Worker implements Runnable {
    // ...

    // Remember, we can't synchronize on a primitive, so we must box the value here.
    private Boolean stopFlag = false;

    @Overrride
    void run() {
        while (true) {

            // ...

            synchronized (stopFlag) {
                if (stopFlag) break;
            }

            // ...
        }
    }

    // ...

    public void stop() {
        synchronized(stopFlag) {
            stopFlag = true;
        }
    }
}

You could also use Thread.interrupt(), which will set the interrupted state of the thread in question, to notify the target thread.

The main thread can call Thread.interrupt() to serve as a stop notification.

class Main {
    // ...

    if (shouldQuitWorker) {
        workerThread.interrupt();
    }

    // ...
}

In the worker thread, you'd just need to check if the thread is interrupted.

class Worker implements Runnable {
    // ...

    private Boolean stopFlag = false;

    @Overrride
    void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                // ...
            }
        } catch (InterruptedException e) {
            // ...
        } finally {
            // handle cleanup...
        }
    }
}

This will also cause side effects such as throwing an InterruptedException if the thread is currently blocked on a call to wait(), join() or sleep(), so be careful.