Example usage for java.lang Thread isInterrupted

List of usage examples for java.lang Thread isInterrupted

Introduction

In this page you can find the example usage for java.lang Thread isInterrupted.

Prototype

public boolean isInterrupted() 

Source Link

Document

Tests whether this thread has been interrupted.

Usage

From source file:Main.java

public static void main(String[] args) {
    System.out.println("#1:" + Thread.interrupted());

    Thread mainThread = Thread.currentThread();
    mainThread.interrupt();/* w  w w.j a v  a2s  .c om*/

    System.out.println("#2:" + mainThread.isInterrupted());

    System.out.println("#3:" + mainThread.isInterrupted());

    System.out.println("#4:" + Thread.interrupted());

    System.out.println("#5:" + mainThread.isInterrupted());
}

From source file:ThreadDemo.java

public static void main(String args[]) {

    Thread t = new Thread(new ThreadDemo());
    System.out.println("Executing " + t.getName());
    t.start();//from w w  w.j  a  v a  2s  . c  om

    if (!t.isInterrupted()) {
        t.interrupt();
    }
    try {
        t.join();
    } catch (InterruptedException e) {
    }
}

From source file:TryThread.java

public static void main(String[] args) {
    Thread first = new TryThread("A ", "a ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    first.start();// w ww.  j a  va2 s .  c  o m
    second.start();
    third.start();
    try {
        Thread.sleep(3000);
    } catch (Exception e) {
        System.out.println(e);
    }
    first.interrupt();
    second.interrupt();
    third.interrupt();
    if (first.isInterrupted()) {
        System.out.println("First thread has been interrupted.");
    }
}

From source file:org.openspaces.pu.container.standalone.StandaloneProcessingUnitContainer.java

/**
 * Allows to run the standalone processing unit container. Uses the {@link
 * StandaloneProcessingUnitContainerProvider} and the parameters provided in order to configure
 * it.//from  w  w w.j  a  v a 2  s . co  m
 *
 * <p> The following parameters are allowed: <ul> <li><b>[location]</b>: The location of the
 * processing unit archive. See {@link org.openspaces.pu.container.standalone.StandaloneProcessingUnitContainerProvider#StandaloneProcessingUnitContainerProvider(String)}.
 * This parameter is required and must be at the end of the command line.</li> <li><b>-config
 * [configLocation]</b>: Allows to add a Spring application context config location. See {@link
 * org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainerProvider#addConfigLocation(String)}.
 * This is an optional parameter and it can be provided multiple times.</li> <li><b>-properties
 * [beanName] [properties]</b>: Allows to inject {@link org.openspaces.core.properties.BeanLevelProperties},
 * see {@link org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainerProvider#setBeanLevelProperties(org.openspaces.core.properties.BeanLevelProperties)}.
 * [beanName] is optional, if not used, the properties will set the {@link
 * org.openspaces.core.properties.BeanLevelProperties#setContextProperties(java.util.Properties)}.
 * If used, will inject properties only to the bean registered under the provided beanName
 * within the Spring context (see {@link org.openspaces.core.properties.BeanLevelProperties#setBeanProperties(String,
 * java.util.Properties)}). The [properties] can either start with <code>embed://</code> which
 * mean they will be provided within the command line (for example:
 * <code>embed://propName1=propVal1;propName2=propVal2</code>) or they can follow Spring {@link
 * org.springframework.core.io.Resource} lookup based on URL syntax or Spring extended
 * <code>classpath</code> prefix (see {@link org.springframework.core.io.DefaultResourceLoader}).</li>
 * <li><b>-cluster [cluster parameters]</b>: Allows to configure {@link
 * org.openspaces.core.cluster.ClusterInfo}, see {@link org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainerProvider#setClusterInfo(org.openspaces.core.cluster.ClusterInfo)}.</li>
 * The following parameters are allowed: <code>total_members=1,1</code> (1,1 is an example
 * value), <code>id=1</code> (1 is an example value), <code>backup_id=1</code> (1 is an example
 * value) and <code>schema=primary_backup</code> (primary_backup is an example value). No
 * parameter is required. For more information regarding the Space meaning of this parameters
 * please consult GigaSpaces reference documentation within the Space URL section. </ul>
 */
public static void main(String[] args) throws Exception {
    GSLogConfigLoader.getLoader();
    showUsageOptionsOnHelpCommand(args);
    if (args.length == 0) {
        printUsage();
        System.exit(1);
    }

    logger.info("Starting with args: " + Arrays.toString(args) + "\n"
            + RuntimeInfo.getEnvironmentInfoIfFirstTime());
    try {
        final ProcessingUnitContainer container = createContainer(args);
        logger.info("Started successfully");

        // Use the MAIN thread as the non daemon thread to keep it alive
        final Thread mainThread = Thread.currentThread();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    logger.info("Shutdown hook triggered");
                    container.close();
                    logger.info("Shutdown complete");
                } finally {
                    mainThread.interrupt();
                }
            }
        });
        while (!mainThread.isInterrupted()) {
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException e) {
                // do nothing, simply exit
            }
        }
    } catch (Exception e) {
        printUsage();
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainer.java

/**
 * Allows to run the integrated processing unit container. Uses the {@link
 * org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainerProvider} and the
 * parameters provided in order to configure it. <p/> <p/> The following parameters are allowed:
 * <ul> <li><b>-config [configLocation]</b>: Allows to add a Spring application context config
 * location. See {@link org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainerProvider#addConfigLocation(String)}.
 * This is an optional parameter and it can be provided multiple times.</li> <li><b>-properties
 * [beanName] [properties]</b>: Allows to inject {@link org.openspaces.core.properties.BeanLevelProperties},
 * see {@link org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainerProvider#setBeanLevelProperties(org.openspaces.core.properties.BeanLevelProperties)}.
 * [beanName] is optional, if not used, the properties will set the {@link
 * org.openspaces.core.properties.BeanLevelProperties#setContextProperties(java.util.Properties)}.
 * If used, will inject properties only to the bean registered under the provided beanName
 * within the Spring context (see {@link org.openspaces.core.properties.BeanLevelProperties#setBeanProperties(String,
 * java.util.Properties)}). The [properties] can either start with <code>embed://</code> which
 * mean they will be provided within the command line (for example:
 * <code>embed://propName1=propVal1;propName2=propVal2</code>) or they can follow Spring {@link
 * org.springframework.core.io.Resource} lookup based on URL syntax or Spring extended
 * <code>classpath</code> prefix (see {@link org.springframework.core.io.DefaultResourceLoader}).</li>
 * <li><b>-cluster [cluster parameters]</b>: Allows to configure {@link
 * org.openspaces.core.cluster.ClusterInfo}, see {@link org.openspaces.pu.container.integrated.IntegratedProcessingUnitContainerProvider#setClusterInfo(org.openspaces.core.cluster.ClusterInfo)}.</li>
 * The following parameters are allowed: <code>total_members=1,1</code> (1,1 is an example
 * value), <code>id=1</code> (1 is an example value), <code>backup_id=1</code> (1 is an example
 * value) and <code>schema=primary_backup</code> (primary_backup is an example value). No
 * parameter is required. For more information regarding the Space meaning of this parameters
 * please consult GigaSpaces reference documentation within the Space URL section. </ul>
 *//*w  w  w  .  ja v  a2  s . c  o m*/
public static void main(String[] args) throws Exception {
    GSLogConfigLoader.getLoader();

    //when calling with space-instance script, and no arguments were passed, we concatenate --help
    //IntegratedProcessingUnitContainer can also run without arguments and load pu.xml
    showUsageOptionsOnHelpCommand(args);

    logger.info("Starting with args: " + Arrays.toString(args) + "\n"
            + RuntimeInfo.getEnvironmentInfoIfFirstTime());
    try {
        final ProcessingUnitContainer container = createContainer(args);
        logger.info("Started successfully");

        // Use the MAIN thread as the non daemon thread to keep it alive
        final Thread mainThread = Thread.currentThread();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                try {
                    logger.info("Shutdown hook triggered");
                    container.close();
                    logger.info("Shutdown complete");
                } finally {
                    mainThread.interrupt();
                }
            }
        });
        while (!mainThread.isInterrupted()) {
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException e) {
                // do nothing, simply exit
            }
        }
    } catch (Exception e) {
        printUsage();
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:Main.java

public static boolean isInterrupted(Thread t) {
    return t.isInterrupted();
}

From source file:Main.java

public static void interrupt(Thread thread) {
    if (null != thread && !thread.isInterrupted()) {
        thread.interrupt();/*w  w  w .  j ava 2  s.co m*/
    }
}

From source file:Main.java

public static void dumpAllThreadStacks() {
    System.out.println("stack trace");

    Map<Thread, StackTraceElement[]> map = Thread.getAllStackTraces();
    for (Thread t : map.keySet()) {
        StackTraceElement[] elements = map.get(t);

        System.out.println(t);/*w w w .j  a  v  a2s  . c  o  m*/
        System.out.println("isDaemon : " + t.isDaemon() + ", isAlive : " + t.isAlive() + ", isInterrupted : "
                + t.isInterrupted());

        for (StackTraceElement e : elements) {
            System.out.println("\t" + e);
        }
        System.out.println("");
    }
}

From source file:org.trafodion.rest.ResourceChecker.java

/**
 * Helper function: print the threads/* ww w  .j a  va2 s. c om*/
 */
public static void printThreads() {
    Set<Thread> threads = Thread.getAllStackTraces().keySet();
    System.out.println("name; state; isDameon; isAlive; isInterrupted");
    for (Thread t : threads) {
        System.out.println(t.getName() + ";" + t.getState() + ";" + t.isDaemon() + ";" + t.isAlive() + ";"
                + t.isInterrupted());
    }
}

From source file:com.l2jfree.lang.L2Thread.java

public static List<String> getStats(Thread t) {
    List<String> list = new FastList<String>();

    list.add(t.toString() + " - ID: " + t.getId());
    list.add(" * State: " + t.getState());
    list.add(" * Alive: " + t.isAlive());
    list.add(" * Daemon: " + t.isDaemon());
    list.add(" * Interrupted: " + t.isInterrupted());
    for (ThreadInfo info : ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { t.getId() }, true,
            true)) {//from  ww w.j  a  v a2  s.c om
        for (MonitorInfo monitorInfo : info.getLockedMonitors()) {
            list.add("==========");
            list.add(" * Locked monitor: " + monitorInfo);
            list.add("\t[" + monitorInfo.getLockedStackDepth() + ".]: at " + monitorInfo.getLockedStackFrame());
        }

        for (LockInfo lockInfo : info.getLockedSynchronizers()) {
            list.add("==========");
            list.add(" * Locked synchronizer: " + lockInfo);
        }

        list.add("==========");
        for (StackTraceElement trace : info.getStackTrace())
            list.add("\tat " + trace);
    }

    return list;
}