All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jtrim2.cancel.ThreadInterrupter Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
package org.jtrim2.cancel;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

final class ThreadInterrupter implements Runnable {
    private final Lock mainLock;
    private Thread thread;

    public ThreadInterrupter(Thread thread) {
        assert thread != null;
        this.mainLock = new ReentrantLock();
        this.thread = thread;
    }

    public void stopInterrupt() {
        mainLock.lock();
        try {
            thread = null;
        } finally {
            mainLock.unlock();
        }
    }

    @Override
    public void run() {
        mainLock.lock();
        try {
            Thread currentThread = thread;
            if (currentThread != null) {
                currentThread.interrupt();
            }
        } finally {
            mainLock.unlock();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy