To interrupt Java threads

Asked 2 years ago, Updated 2 years ago, 63 views

Hi, everyone. I'm doing Android programming.

I'm processing the long-lasting tasks separately through the thread Is there a way to stop a working thread?

Maybe it's because the thread you point to is different from the thread that actually works, The isAlive() or interrupt() method does not work. I'm a little embarrassed because it works so differently than I thought.

Here is an example of the code.

ParserThread thread = new ParserThread();

thread.start();

if (thread != null && thread.isAlive())
    thread.interrupt(); // isAlive() returns false and does not run

/** Thread Class */
class ParserThread extends Thread {
    @Override public void run() {
        super.run();
        // Long working areas //
        This.isAlive(); // Return true here
    }
}

No matter how much I searched the Internet, I couldn't find a way to get a working thread. Is there a way?

java thread isalive

2022-09-21 18:10

2 Answers

There is a translation about Java Thread. It was published in O'Reilly in the early 2000s.

It's hard to buy it on the market, and you can see it at the library. It's the only professional book on Java threads, so please refer to it.

And how to get a thread. ThreadGroup allows you to obtain threads.

ThreadGroup g = Thread.currentThread().getThreadGroup();
    while (true) {
        ThreadGroup g2 = g.getParent();
        if (g2 == null) {
            break;
        } } else {
            g = g2;
        }
    }
    Thread[] threads= new Thread[256];
    g.enumerate(threads)

You can obtain thread objects as shown in .

https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

Please refer to it.


2022-09-21 18:10

It's going to be live right after start(). Do it like below. (Based on JDK 1.8)

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ThreadTest2 {
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(ThreadTest2.class);

    public static void main(String[] args) {
        ChildThread thread = new ChildThread();

        // thread!= null; // thread variable cannot be null

        thread.start();
        if (!thread.isAlive()) {
            narrow new IllegalAccessError ("I shouldn't be here");
        }

        thread.interrupt();
        if (!thread.isInterrupted()) {
            narrow new IllegalAccessError ("I shouldn't be here 2");
        }
    }
}

class ChildThread extends Thread {
    private static final Logger logger = LoggerFactory.getLogger(ChildThread.class);

    @Override
    public void run() {
        doUselessThing();
        this.isAlive();
    }

    @Override
    public void interrupt() {
        logger.debug("i'm dying. :<");
        super.interrupt();
    }

    private void doUselessThing() {
        logger.debug("do useless thing: start");
            long sum = 0;
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            sum += i;
        }
        logger.debug("do useless thing: end, sum is {}", sum);
    }
}


2022-09-21 18:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.