Example usage for java.lang Thread isAlive

List of usage examples for java.lang Thread isAlive

Introduction

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

Prototype

public final native boolean isAlive();

Source Link

Document

Tests if this thread is alive.

Usage

From source file:com.buaa.cfs.utils.Shell.java

private static void joinThread(Thread t) {
    while (t.isAlive()) {
        try {//from  ww w  .  j  a v  a  2 s  .  c  om
            t.join();
        } catch (InterruptedException ie) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Interrupted while joining on: " + t, ie);
            }
            t.interrupt(); // propagate interrupt
        }
    }
}

From source file:net.sf.taverna.t2.servicedescriptions.impl.ServiceDescriptionRegistryImpl.java

private static void joinThreads(Collection<? extends Thread> threads, long descriptionThreadTimeoutMs) {
    long finishJoinBy = currentTimeMillis() + descriptionThreadTimeoutMs;
    for (Thread thread : threads) {
        // No shorter timeout than 1 ms (thread.join(0) waits forever!)
        long timeout = Math.max(1, finishJoinBy - currentTimeMillis());
        try {//from w w w  .  j  av  a  2s . c om
            thread.join(timeout);
        } catch (InterruptedException e) {
            currentThread().interrupt();
            return;
        }
        if (thread.isAlive())
            logger.debug("Thread did not finish " + thread);
    }
}

From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java

@SuppressWarnings("deprecation")
private static void clearReferencesThreads(ClassLoader classLoader) {
    Thread[] threads = getThreads();

    // Iterate over the set of threads
    for (Thread thread : threads) {
        if (thread != null) {
            ClassLoader ccl = thread.getContextClassLoader();
            if (ccl != null && ccl == classLoader) {
                // Don't warn about this thread
                if (thread == Thread.currentThread()) {
                    continue;
                }/*from  w  w  w  . ja  va 2 s.  c  o  m*/

                // Skip threads that have already died
                if (!thread.isAlive()) {
                    continue;
                }

                // Don't warn about JVM controlled threads
                ThreadGroup tg = thread.getThreadGroup();
                if (tg != null && JVM_THREAD_GROUP_NAMES.contains(tg.getName())) {
                    continue;
                }

                // TimerThread is not normally visible
                if (thread.getClass().getName().equals("java.util.TimerThread")) {
                    clearReferencesStopTimerThread(thread);
                    continue;
                }

                if (logger.isLoggable(Level.FINE))
                    logger.fine("A thread named [" + thread.getName()
                            + "] started but has failed to stop it. This is very likely to create a memory leak.");

                // Don't try an stop the threads unless explicitly
                // configured to do so
                if (!clearReferencesStopThreads) {
                    continue;
                }

                // If the thread has been started via an executor, try
                // shutting down the executor
                try {
                    Field targetField = thread.getClass().getDeclaredField("target");
                    targetField.setAccessible(true);
                    Object target = targetField.get(thread);

                    if (target != null && target.getClass().getCanonicalName()
                            .equals("java.util.concurrent.ThreadPoolExecutor.Worker")) {
                        Field executorField = target.getClass().getDeclaredField("this$0");
                        executorField.setAccessible(true);
                        Object executor = executorField.get(target);
                        if (executor instanceof ThreadPoolExecutor) {
                            ((ThreadPoolExecutor) executor).shutdownNow();
                        }
                    }
                } catch (Exception e) {
                    logger.log(Level.WARNING, "Failed to terminate thread named [" + thread.getName() + "]", e);
                }

                // This method is deprecated and for good reason. This is
                // very risky code but is the only option at this point.
                // A *very* good reason for apps to do this clean-up
                // themselves.
                thread.stop();
            }
        }
    }
}

From source file:ThreadDemo.java

public void run() {

    Thread t = Thread.currentThread();
    // tests if this thread is alive
    System.out.println("status = " + t.isAlive());
}

From source file:Main.java

public void run() {

    Thread t = Thread.currentThread();
    System.out.print(t.getName());
    System.out.println(", status = " + t.isAlive());
}

From source file:Main.java

public void run() {

    Thread t = Thread.currentThread();
    System.out.print(t.getName());

    System.out.println(", status = " + t.isAlive());
}

From source file:org.quickserver.util.pool.thread.ThreadObjectFactory.java

public boolean validateObject(Object obj) {
    if (obj == null)
        return false;
    Thread thread = (Thread) obj;
    return thread.isAlive();
}

From source file:Main.java

public void run() {

    Thread t = Thread.currentThread();
    System.out.print(t.getName());
    // checks if this thread is alive
    System.out.println(", status = " + t.isAlive());
}

From source file:de.doering.dwca.flickr.OccurrenceExport.java

private void export() throws IOException {
    // loop over years
    int year = 1900 + new Date().getYear();
    while (year >= MIN_YEAR) {
        searchYear(year);/*from  w  w  w  .j a v a 2s . c o m*/
        year--;
    }

    // Wait until all threads are finish
    int running = 0;
    do {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        running = 0;
        for (Thread thread : threads) {
            if (thread.isAlive()) {
                running++;
            }
        }
        //log.debug("We have " + running + " running searches. ");
    } while (running > 0);
    log.info("Finished flickr export with {} records", writer.getRecordsWritten());
}

From source file:de.doering.dwca.flickr.OccurrenceExport.java

private void searchYear(int year) {
    if (threads.size() < THREADS) {
        threads.add(startThread(year));/*from   w ww  .  ja  va  2  s  .  co m*/
    } else {
        // wait until one thread is finished
        log.debug("Waiting for a thread to finish");
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            Iterator<Thread> iter = threads.iterator();
            while (iter.hasNext()) {
                Thread t = iter.next();
                if (!t.isAlive()) {
                    log.debug("Thread " + t.getName() + " finished");
                    iter.remove();
                }
            }
        } while (threads.size() == THREADS);

        threads.add(startThread(year));
    }
}