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.apporiented.hermesftp.client.FtpTestClient.java

private void executeReceive(TextReceiver l) {
    Thread t = new Thread(l);
    t.start();//from w ww  . j a v a  2 s .  c o m
    while (t.isAlive()) {
        Thread.yield();
    }
}

From source file:com.apporiented.hermesftp.client.FtpTestClient.java

private void executeReceive(RawReceiver l) {
    Thread t = new Thread(l);
    t.start();// w  ww . j av  a2s.c  om
    while (t.isAlive()) {
        Thread.yield();
    }
}

From source file:com.apporiented.hermesftp.client.FtpTestClient.java

private void executeSend(TextSender l) {
    Thread t = new Thread(l);
    t.start();//  w  w w .j a va2  s  .co  m
    while (t.isAlive()) {
        Thread.yield();
    }
}

From source file:com.apporiented.hermesftp.client.FtpTestClient.java

private void executeSend(RawSender l) {
    Thread t = new Thread(l);
    t.start();/*  www.j  a va  2 s.c  o  m*/
    while (t.isAlive()) {
        Thread.yield();
    }
}

From source file:dk.netarkivet.archive.bitarchive.distribute.BitarchiveServer.java

public void visit(BatchTerminationMessage msg) throws ArgumentNotValid {
    ArgumentNotValid.checkNotNull(msg, "BatchTerminationMessage msg");
    log.info("Received BatchTerminationMessage: " + msg);

    try {//ww  w  .  j  a v  a2  s  .  c o  m
        Thread t = batchProcesses.get(msg.getTerminateID());

        // check whether the batchjob is still running.
        if (t == null) {
            log.info("The batchjob with ID '" + msg.getTerminateID()
                    + "' cannot be found, and must have terminated " + "by it self.");
            return;
        }

        // try to interrupt.
        if (t.isAlive()) {
            t.interrupt();
        }

        // wait one second, before verifying whether it is dead.
        synchronized (this) {
            try {
                this.wait(1000);
            } catch (InterruptedException e) {
                log.trace("Unimportant InterruptedException caught.", e);
            }
        }

        // Verify that is dead, or log that it might have a problem. 
        if (t.isAlive()) {
            log.error("The thread '" + t + "' should have been terminated,"
                    + " but it is apparently still alive.");
        } else {
            log.info("The batchjob with ID '" + msg.getTerminateID() + "' has successfully been terminated!");
        }
    } catch (Throwable e) {
        // log problem and set to NotOK!
        log.error("An error occured while trying to terminate " + msg.getTerminateID(), e);
    }
}

From source file:org.hyperic.hq.measurement.server.session.AvailabilityCacheTest.java

public void testCacheTransactionThreads() throws Exception {
    Thread thread = new Thread() {
        public void run() {
            int id = 0;
            cache.beginTran();//from w  w w  . j  a va  2 s  .c o m
            DataPoint dp = new DataPoint(id, 1.0, 1);
            cache.put(new Integer(id), dp);
            dp = new DataPoint(id, 1.0, 2);
            cache.put(new Integer(id), dp);
            cache.commitTran();
        }
    };
    thread.start();

    int id = 0;
    cache.beginTran();
    DataPoint dp = new DataPoint(id, 1.0, 3);
    cache.put(new Integer(id), dp);
    dp = new DataPoint(id, 1.0, 4);
    cache.put(new Integer(id), dp);
    cache.rollbackTran();

    // don't want to hang the build
    thread.join(5000);
    if (thread.isAlive()) {
        thread.interrupt();
        assertTrue(false);
        return;
    }

    DataPoint curr = (DataPoint) cache.get(new Integer(id));
    assertTrue(2 == curr.getTimestamp());
}

From source file:org.apache.hadoop.net.unix.TestDomainSocket.java

@Test(timeout = 180000)
public void testShutdown() throws Exception {
    final AtomicInteger bytesRead = new AtomicInteger(0);
    final AtomicBoolean failed = new AtomicBoolean(false);
    final DomainSocket[] socks = DomainSocket.socketpair();
    Runnable reader = new Runnable() {
        @Override//w  w w.ja va  2  s. c o  m
        public void run() {
            while (true) {
                try {
                    int ret = socks[1].getInputStream().read();
                    if (ret == -1)
                        return;
                    bytesRead.addAndGet(1);
                } catch (IOException e) {
                    DomainSocket.LOG.error("reader error", e);
                    failed.set(true);
                    return;
                }
            }
        }
    };
    Thread readerThread = new Thread(reader);
    readerThread.start();
    socks[0].getOutputStream().write(1);
    socks[0].getOutputStream().write(2);
    socks[0].getOutputStream().write(3);
    Assert.assertTrue(readerThread.isAlive());
    socks[0].shutdown();
    readerThread.join();
    Assert.assertFalse(failed.get());
    Assert.assertEquals(3, bytesRead.get());
    IOUtils.cleanup(null, socks);
}

From source file:Main.java

public static String runScript(String script) {
    String sRet;//w  w  w .  java  2  s  . c om
    try {
        final Process m_process = Runtime.getRuntime().exec(script);
        final StringBuilder sbread = new StringBuilder();
        Thread tout = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getInputStream()), 8192);
                String ls_1;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sbread.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        tout.start();

        final StringBuilder sberr = new StringBuilder();
        Thread terr = new Thread(new Runnable() {
            public void run() {
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(m_process.getErrorStream()), 8192);
                String ls_1;
                try {
                    while ((ls_1 = bufferedReader.readLine()) != null) {
                        sberr.append(ls_1).append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        terr.start();

        m_process.waitFor();
        while (tout.isAlive()) {
            Thread.sleep(50);
        }
        if (terr.isAlive())
            terr.interrupt();
        String stdout = sbread.toString();
        String stderr = sberr.toString();
        sRet = stdout + stderr;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return sRet;
}

From source file:org.lsc.AbstractSynchronize.java

public final String getTaskFullStatus(final String syncName) {
    Thread asyncThread = asynchronousThreads.get(syncName);
    if (asyncThread != null && asyncThread.isAlive()) {
        AsynchronousRunner asyncRunner = mapSTasks.get(syncName);
        InfoCounter counter = asyncRunner.getCounter();
        return getLogStatus(counter);
    } else {/* w  w  w  .  j  a v a  2s  .c om*/
        return null;
    }
}

From source file:com.baidu.jprotobuf.mojo.PreCompileMojo.java

private void terminateThreads(ThreadGroup threadGroup) {
    long startTime = System.currentTimeMillis();
    Set<Thread> uncooperativeThreads = new HashSet<Thread>(); // these were not responsive to interruption
    for (Collection<Thread> threads = getActiveThreads(threadGroup); !threads
            .isEmpty(); threads = getActiveThreads(threadGroup), threads.removeAll(uncooperativeThreads)) {
        // Interrupt all threads we know about as of this instant (harmless if spuriously went dead (! isAlive())
        // or if something else interrupted it ( isInterrupted() ).
        for (Thread thread : threads) {
            getLog().debug("interrupting thread " + thread);
            thread.interrupt();//from   w w w.j a v  a  2s.  com
        }
        // Now join with a timeout and call stop() (assuming flags are set right)
        for (Thread thread : threads) {
            if (!thread.isAlive()) {
                continue; // and, presumably it won't show up in getActiveThreads() next iteration
            }
            if (daemonThreadJoinTimeout <= 0) {
                joinThread(thread, 0); // waits until not alive; no timeout
                continue;
            }
            long timeout = daemonThreadJoinTimeout - (System.currentTimeMillis() - startTime);
            if (timeout > 0) {
                joinThread(thread, timeout);
            }
            if (!thread.isAlive()) {
                continue;
            }
            uncooperativeThreads.add(thread); // ensure we don't process again
            if (stopUnresponsiveDaemonThreads) {
                getLog().warn("thread " + thread + " will be Thread.stop()'ed");
                thread.stop();
            } else {
                getLog().warn("thread " + thread + " will linger despite being asked to die via interruption");
            }
        }
    }
    if (!uncooperativeThreads.isEmpty()) {
        getLog().warn("NOTE: " + uncooperativeThreads.size()
                + " thread(s) did not finish despite being asked to "
                + " via interruption. This is not a problem with exec:java, it is a problem with the running code."
                + " Although not serious, it should be remedied.");
    } else {
        int activeCount = threadGroup.activeCount();
        if (activeCount != 0) {
            // TODO this may be nothing; continue on anyway; perhaps don't even log in future
            Thread[] threadsArray = new Thread[1];
            threadGroup.enumerate(threadsArray);
            getLog().debug("strange; " + activeCount + " thread(s) still active in the group " + threadGroup
                    + " such as " + threadsArray[0]);
        }
    }
}