InterruptMonitor

Inheritance: java.lang.Object

All Implemented Interfaces: com.aspose.imaging.multithreading.IInterruptMonitor

public class InterruptMonitor implements IInterruptMonitor

Represents information about interruption.

Constructors

ConstructorDescription
InterruptMonitor()Initializes a new instance of the InterruptMonitor class.

Methods

MethodDescription
getThreadLocalInstance()Gets the IInterruptMonitor instance which is unique for each thread.
setThreadLocalInstance(IInterruptMonitor value)Sets the IInterruptMonitor instance which is unique for each thread.
isThreadInterrupted()Returns true if interrupt monitor for current thread exists, and it was interrupted otherwise false.
isInterrupted()Gets the value indicating whether operations should be interrupted.
interrupt()Sends a request to interrupt operations.

Example: The following example shows how to interrupt the long process of image conversion.

/**
 * <p>This is helper class which initiates image conversion and waits for its interruption.</p>
 */
class Worker implements Runnable {
    /**
     * The path to the input image.
     */
    private final String inputPath;

    /**
     * The path to the output image.
     */
    private final String outputPath;

    /**
     * The save options.
     */
    private final com.aspose.imaging.ImageOptionsBase saveOptions;

    /**
     * The interrupt monitor.
     */
    private final com.aspose.imaging.multithreading.InterruptMonitor monitor;

    /**
     * <p>Initializes a new instance of the {#link #Worker} class.</p>
     *
     * @param inputPath   The path to the input image.
     * @param outputPath  The path to the output image.
     * @param saveOptions The save options.
     * @param monitor     The interrupt monitor.
     */
    public Worker(String inputPath, String outputPath, com.aspose.imaging.ImageOptionsBase saveOptions, com.aspose.imaging.multithreading.InterruptMonitor monitor) {
        this.inputPath = inputPath;
        this.outputPath = outputPath;
        this.saveOptions = saveOptions;
        this.monitor = monitor;
    }

    /**
     * <p>Converts an image from one format to another. Handles interruption.</p>
     */
    public void run() {
        try {
            com.aspose.imaging.Image image = com.aspose.imaging.Image.load(this.inputPath);

            // Set a thread-local instance of the interrupt monitor.
            com.aspose.imaging.multithreading.InterruptMonitor.setThreadLocalInstance(this.monitor);

            try {
                image.save(this.outputPath, this.saveOptions);
            } catch (com.aspose.imaging.coreexceptions.OperationInterruptedException e) {
                System.out.printf(
                        "The worker thread #%s has been interrupted at %s\r\n",
                        java.lang.Thread.currentThread().getId(),
                        new java.util.Date());
            } finally {
                image.dispose();

                // Reset the thread-local instance of the interrupt monitor.
                com.aspose.imaging.multithreading.InterruptMonitor.setThreadLocalInstance(null);
            }
        } catch (java.lang.Exception e) {
            // Print detailed information about any unexpected exception.
            System.out.println(e);
        }
    }
}

// Here is the main example using the Worker class.
String baseDir = "c:\\temp\\";

com.aspose.imaging.multithreading.InterruptMonitor monitor = new com.aspose.imaging.multithreading.InterruptMonitor();
Worker worker = new Worker(baseDir + "big.png", baseDir + "big.bmp", new com.aspose.imaging.imageoptions.BmpOptions(), monitor);

// Start the worker in a dedicated thread.
Thread thread = new Thread(worker);
thread.start();

try {
    // Do some meaningful work here
    Thread.sleep(2000);

    // Request to interrupt the worker thread
    monitor.interrupt();
    System.out.printf("Interrupting the worker thread #%s at %s", thread.getId(), new java.util.Date());

    // Wait for interruption.
    thread.join();
} catch (InterruptedException e) {
    System.out.println(e);
}

System.out.println("Done. Press ENTER to exit.");
System.in.read();

// The output may look like this:
// Interrupting the worker thread #11 at Tue Aug 06 17:57:52 YEKT 2019
// The worker thread #11 has been interrupted at Tue Aug 06 17:57:59 YEKT 2019
// Done. Press ENTER to exit.

InterruptMonitor()

public InterruptMonitor()

Initializes a new instance of the InterruptMonitor class.

getThreadLocalInstance()

public static IInterruptMonitor getThreadLocalInstance()

Gets the IInterruptMonitor instance which is unique for each thread.

Returns: IInterruptMonitor

setThreadLocalInstance(IInterruptMonitor value)

public static void setThreadLocalInstance(IInterruptMonitor value)

Sets the IInterruptMonitor instance which is unique for each thread.

Parameters:

ParameterTypeDescription
valueIInterruptMonitor

isThreadInterrupted()

public static boolean isThreadInterrupted()

Returns true if interrupt monitor for current thread exists, and it was interrupted otherwise false.

Returns: boolean - true if interrupt monitor for current thread exists, and it was interrupted otherwise false.

isInterrupted()

public boolean isInterrupted()

Gets the value indicating whether operations should be interrupted.

Returns: boolean

interrupt()

public void interrupt()

Sends a request to interrupt operations.

Example: The following example shows how to interrupt the long process of image conversion.

/**
 * <p>This is helper class which initiates image conversion and waits for its interruption.</p>
 */
class Worker implements Runnable {
    /**
     * The path to the input image.
     */
    private final String inputPath;

    /**
     * The path to the output image.
     */
    private final String outputPath;

    /**
     * The save options.
     */
    private final com.aspose.imaging.ImageOptionsBase saveOptions;

    /**
     * The interrupt monitor.
     */
    private final com.aspose.imaging.multithreading.InterruptMonitor monitor;

    /**
     * <p>Initializes a new instance of the {#link #Worker} class.</p>
     *
     * @param inputPath   The path to the input image.
     * @param outputPath  The path to the output image.
     * @param saveOptions The save options.
     * @param monitor     The interrupt monitor.
     */
    public Worker(String inputPath, String outputPath, com.aspose.imaging.ImageOptionsBase saveOptions, com.aspose.imaging.multithreading.InterruptMonitor monitor) {
        this.inputPath = inputPath;
        this.outputPath = outputPath;
        this.saveOptions = saveOptions;
        this.monitor = monitor;
    }

    /**
     * <p>Converts an image from one format to another. Handles interruption.</p>
     */
    public void run() {
        try {
            com.aspose.imaging.Image image = com.aspose.imaging.Image.load(this.inputPath);

            // Set a thread-local instance of the interrupt monitor.
            com.aspose.imaging.multithreading.InterruptMonitor.setThreadLocalInstance(this.monitor);

            try {
                image.save(this.outputPath, this.saveOptions);
            } catch (com.aspose.imaging.coreexceptions.OperationInterruptedException e) {
                System.out.printf(
                        "The worker thread #%s has been interrupted at %s\r\n",
                        java.lang.Thread.currentThread().getId(),
                        new java.util.Date());
            } finally {
                image.dispose();

                // Reset the thread-local instance of the interrupt monitor.
                com.aspose.imaging.multithreading.InterruptMonitor.setThreadLocalInstance(null);
            }
        } catch (java.lang.Exception e) {
            // Print detailed information about any unexpected exception.
            System.out.println(e);
        }
    }
}

// Here is the main example using the Worker class.
String baseDir = "c:\\temp\\";

com.aspose.imaging.multithreading.InterruptMonitor monitor = new com.aspose.imaging.multithreading.InterruptMonitor();
Worker worker = new Worker(baseDir + "big.png", baseDir + "big.bmp", new com.aspose.imaging.imageoptions.BmpOptions(), monitor);

// Start the worker in a dedicated thread.
Thread thread = new Thread(worker);
thread.start();

try {
    // Do some meaningful work here
    Thread.sleep(2000);

    // Request to interrupt the worker thread
    monitor.interrupt();
    System.out.printf("Interrupting the worker thread #%s at %s", thread.getId(), new java.util.Date());

    // Wait for interruption.
    thread.join();
} catch (InterruptedException e) {
    System.out.println(e);
}

System.out.println("Done. Press ENTER to exit.");
System.in.read();

// The output may look like this:
// Interrupting the worker thread #11 at Tue Aug 06 17:57:52 YEKT 2019
// The worker thread #11 has been interrupted at Tue Aug 06 17:57:59 YEKT 2019
// Done. Press ENTER to exit.