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.asakusafw.bulkloader.transfer.StreamFileListProvider.java

@Override
public final void waitForComplete() throws IOException, InterruptedException {
    synchronized (running) {
        for (Iterator<Thread> iter = running.iterator(); iter.hasNext();) {
            Thread next = iter.next();
            if (next.isAlive()) {
                next.join();// w ww  .  jav  a  2 s  . c o m
            }
            iter.remove();
        }
    }
    waitForDone();
}

From source file:it.jnrpe.server.ThreadTimeoutWatcher.java

/**
 * Kills the thread identified by the specified thread data
 * @param td The thread data//w w w. j a  v  a2 s .  c  o  m
 */
private void killThread(CThreadData td) {
    Thread t = td.getThread();
    if (t.isAlive()) {
        if (m_Logger.isInfoEnabled())
            m_Logger.warn("Killing thread. It was running since "
                    + ((System.currentTimeMillis() - td.getStartTime()) / 1000) + " seconds ago");
        ((JNRPEServerThread) t).stopNow();
    }
}

From source file:net.bluehornreader.service.Service.java

public void waitForThreadsToFinish() {
    long start = System.currentTimeMillis();
    for (;;) {// ww w . j a  v  a  2  s.  c o  m
        boolean foundAlive = false;
        if (isAlive()) {
            foundAlive = true;
            LOG.info(String.format("Thread %s is still alive", getName()));
            synchronized (this) {
                notify();
            }
        }
        for (Thread thread : getChildThreads()) {
            if (thread.isAlive()) {
                foundAlive = true;
                LOG.info(String.format("Thread %s is still alive", thread.getName()));
                synchronized (thread) { //ttt2 make sure this works, as Idea complains: create a separate test case; (the thing is that the object is not
                    // local, but taken from a global collection)
                    thread.notify();
                }
            }
        }
        if (foundAlive) {
            try {
                if (System.currentTimeMillis() - start > 10000) { // ttt2 maybe make configurable
                    LOG.warn("Some threads are still running. Giving up on shutting them down.");
                    return;
                }

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                LOG.error("Exception sleeping while trying to shut down child threads", e);
            }
        } else {
            return;
        }
    }
}

From source file:br.com.pense.produto.controller.MultiThreadsTest.java

@Test
public void MultiThreads() {
    System.out.println(">>> MultiThreads: " + NRO_THREADS);
    System.out.println("- Disparando threads!");
    SemanticEngineThread s;/*w ww .  j  a  v  a2s  . co m*/
    Thread t;
    List<Thread> lst = new ArrayList<Thread>();
    for (int i = 0; i < NRO_THREADS; i++) {
        s = new SemanticEngineThread();
        s.setId(i);
        t = new Thread(s);
        lst.add(t);
        t.start();
    }

    System.out.println("- Monitorando " + NRO_THREADS + " threads!");
    List<Thread> auxList = new ArrayList<Thread>();
    while (!lst.isEmpty()) {
        //            auxList.clear();
        auxList = new ArrayList<Thread>();
        for (Thread x : lst) {
            if (x.isAlive()) {
                auxList.add(x);
            }
        }
        lst = auxList;
        //            System.out.println("- "+ lst.size() +" ainda vivas.");
    }
    System.out.println("- Finalizado!");
}

From source file:com.thoughtworks.go.config.BackgroundMailSender.java

private ValidationBean execute(Runnable action) {
    Thread thread = new Thread(action);
    thread.start();//from w w w .  j  a va  2  s .  c  o  m
    try {
        thread.join(timeout);
        if (thread.isAlive()) {
            thread.interrupt();
            return ValidationBean.notValid(ERROR_MESSAGE);
        }
        return validation;
    } catch (InterruptedException e) {
        LOGGER.error("Timed out when sending an email. Please check email configuration.");
        return ValidationBean.notValid(ERROR_MESSAGE);
    }
}

From source file:org.bhave.experiment.data.TestKafkaConsumer.java

@Test
public void testConfigureKafkaConsumer() throws InterruptedException {

    KafkaDataConsumer consumer = new KafkaDataConsumer();

    Configuration config = new PropertiesConfiguration();
    config.addProperty(KafkaDataConsumer.HOST_CFG, "localhost");
    config.addProperty(KafkaDataConsumer.PORT_CFG, KafkaDataConsumer.DEFAULT_PORT_CFG);
    config.addProperty(KafkaDataConsumer.TOPIC_CFG, "test");
    config.addProperty(KafkaDataConsumer.P_PRODUCER_ID, 0);

    consumer.loadConfiguration(config);/*from www  . j  av a2  s  . co  m*/

    Configuration configuration = consumer.getConfiguration();
    assertNotNull(configuration);

    consumer.addExporter(new StdOutDataExporter());

    Thread consumerThread = new Thread(consumer);
    consumerThread.setName("Consumer Thread");
    consumerThread.start();

    assertTrue(consumerThread.isAlive());
    //this should terminate the thread in which it is running
    consumer.finish();

    Thread.sleep(1000);
    assertFalse(consumerThread.isAlive());

    System.out.println("Kafka Consumer Thread terminated");

}

From source file:com.sonatype.nexus.perftest.ClientSwarm.java

public void stop() throws InterruptedException {
    for (Thread thread : threads) {
        for (int i = 0; i < 3 && thread.isAlive(); i++) {
            thread.interrupt();//from  w w w. j  av a  2s .  c  om
            thread.join(1000L);
        }
        if (thread.isAlive()) {
            StringBuilder sb = new StringBuilder(
                    String.format("Thread %s ignored interrupt flag\n", thread.getName()));
            for (StackTraceElement f : thread.getStackTrace()) {
                sb.append("\t").append(f.toString()).append("\n");
            }
            System.err.println(sb.toString());
        }
    }
}

From source file:org.echocat.jomon.spring.ContextLoadThreadGroup.java

public void join() throws InterruptedException {
    for (final Thread thread : this) {
        if (thread.isAlive()) {
            thread.join();/*  w  w  w  .j  a v a 2s . c om*/
        }
    }
    throwRuntimeExceptionOn();
}

From source file:org.huahinframework.manager.queue.QueueManager.java

@Override
public void run() {
    log.info("QueueManager start");

    try {/* w  w w  . ja v  a  2  s .  c om*/
        List<Thread> threads = new ArrayList<Thread>();
        for (;;) {
            Map<String, Queue> runQueueMap = QueueUtils.readRemoveQueue(queuePath);
            for (Entry<String, Queue> entry : runQueueMap.entrySet()) {
                QueueUtils.removeQueue(queuePath, entry.getValue());
            }

            Map<String, Queue> queueMap = QueueUtils.readQueue(queuePath);
            if (queueMap.isEmpty()) {
                Thread.sleep(POLLING_SECOND);
                continue;
            }

            int runnings = JobUtils.listJob(org.apache.hadoop.mapreduce.JobStatus.State.RUNNING, jobConf)
                    .size();
            int preps = JobUtils.listJob(org.apache.hadoop.mapreduce.JobStatus.State.PREP, jobConf).size();
            if (jobQueueLimit > 0 && (runnings + preps) >= jobQueueLimit) {
                Thread.sleep(POLLING_SECOND);
                continue;
            }

            List<Thread> removes = new ArrayList<Thread>();
            for (Thread t : threads) {
                if (!t.isAlive()) {
                    removes.add(t);
                }
            }
            threads.removeAll(removes);

            if (jobQueueLimit > 0 && threads.size() >= jobQueueLimit) {
                Thread.sleep(POLLING_SECOND);
                continue;
            }

            Queue queue = null;
            for (Queue q : queueMap.values()) {
                queue = q;
                break;
            }

            if (queue.getType() == Queue.TYPE_HIVE && properties.getHiveserver() == null) {
                queue.setRun(true);
                queue.setMessage("hiveserver not found.");
                QueueUtils.registerQueue(queuePath, queue);
                continue;
            }

            Thread runQueue = null;
            switch (queue.getType()) {
            case Queue.TYPE_JAR:
                runQueue = new RunQueue(JobUtils.getJobConf(properties), queuePath, queue);
                break;
            case Queue.TYPE_HIVE:
                runQueue = new RunHiveQueue(properties, queuePath, queue);
                break;
            case Queue.TYPE_PIG:
                runQueue = new RunPigQueue(properties, queuePath, queue);
                break;
            }

            runQueue.start();
            threads.add(runQueue);

            queue.setRun(true);
            QueueUtils.registerQueue(queuePath, queue);
        }
    } catch (Exception e) {
        e.printStackTrace();
        log.error(e);
    }

    log.info("QueueManager end");
}

From source file:org.echocat.jomon.spring.ContextLoadThreadGroup.java

public boolean join(@Nonnull Duration duration) throws InterruptedException {
    boolean result = true;
    final Iterator<Thread> i = iterator();
    final StopWatch stopWatch = new StopWatch();
    while (result && i.hasNext()) {
        final Thread thread = i.next();
        if (thread.isAlive()) {
            thread.join(duration.minus(stopWatch.getCurrentDuration()).in(MILLISECONDS));
        }//from  ww w.  ja va  2s.  c o  m
        result = !thread.isAlive();
    }
    if (result) {
        throwRuntimeExceptionOn();
    }
    return result;
}