Example usage for java.lang Thread yield

List of usage examples for java.lang Thread yield

Introduction

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

Prototype

public static native void yield();

Source Link

Document

A hint to the scheduler that the current thread is willing to yield its current use of a processor.

Usage

From source file:Main.java

@Override
public void run() {
    try {//from   w w  w. ja  v a 2  s  . com
        while (true) {
            queue.take();
            System.out.println("Took item " + m_count.incrementAndGet());
            final long start = System.currentTimeMillis();
            while ((System.currentTimeMillis() - start) < 100) {
                Thread.yield(); // Spin wait
            }
        }
    } catch (Exception ex) {
        System.out.println("Interrupted. Count: " + m_count.get());
    }
}

From source file:com.auditbucket.helper.DeadlockRetry.java

public static Command execute(Command command, String block, int maxRetry) throws DatagioException {
    // Deadlock re-try fun
    int retryCount = 0;
    while (retryCount < maxRetry) {
        try {/*  ww  w  .ja  v  a 2  s .c o  m*/
            return command.execute();
        } catch (RuntimeException re) {
            // ToDo: Exceptions getting wrapped in a JedisException. Can't directly catch the DDE hence the instanceof check
            if (re.getCause() instanceof NotFoundException || re.getCause() instanceof DeadlockDetectedException
                    || re.getCause() instanceof InvalidDataAccessResourceUsageException
                    || re.getCause() instanceof DataRetrievalFailureException) {
                logger.debug("Deadlock Detected. Entering retry [{}]", block);
                Thread.yield();
                retryCount++;
                if (retryCount == maxRetry) {
                    // http://www.slideshare.net/neo4j/zephyr-neo4jgraphconnect-2013short
                    logger.error("Deadlock retries exceeded in [{}]", block);
                    throw (re);
                }
            } else {
                logger.error("DeadlockRetry error could not be handled {}", re.getMessage());
                throw (re);
            }
        }
    }
    return null;
}

From source file:com.thoughtworks.go.plugin.infra.monitor.AbstractDefaultPluginJarLocationMonitorTest.java

protected void waitAMoment() throws InterruptedException {
    Thread.yield();
    Thread.sleep(2000);
}

From source file:org.apache.streams.util.ComponentUtils.java

/**
 * Certain types of queues will return null when calling {@link java.util.Queue#poll()} due to many factors depending
 * on the type of queue.  <code>pollWhileNotEmpty</code> will poll the queue until an item from the queue is returned
 * or the queue is empty.  If the queue is empty it will return NULL.
 * @param queue//from  w  w  w  .j  a  v a  2  s.c  o  m
 * @param <T>
 * @return
 */
public static <T> T pollWhileNotEmpty(Queue<T> queue) {
    T item = queue.poll();
    while (!queue.isEmpty() && item == null) {
        Thread.yield();
        item = queue.poll();
    }
    return item;
}

From source file:Main.java

/**
 * Compute e^x to a given scale.//from w w w. j a v  a  2s.  c o m
 * Break x into its whole and fraction parts and
 * compute (e^(1 + fraction/whole))^whole using Taylor's formula.
 * @param x the value of x
 * @param scale the desired scale of the result
 * @return the result value
 */
public static BigDecimal exp(BigDecimal x, int scale) {
    // e^0 = 1
    if (x.signum() == 0) {
        return BigDecimal.valueOf(1);
    }

    // If x is negative, return 1/(e^-x).
    else if (x.signum() == -1) {
        return BigDecimal.valueOf(1).divide(exp(x.negate(), scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    // Compute the whole part of x.
    BigDecimal xWhole = x.setScale(0, BigDecimal.ROUND_DOWN);

    // If there isn't a whole part, compute and return e^x.
    if (xWhole.signum() == 0)
        return expTaylor(x, scale);

    // Compute the fraction part of x.
    BigDecimal xFraction = x.subtract(xWhole);

    // z = 1 + fraction/whole
    BigDecimal z = BigDecimal.valueOf(1).add(xFraction.divide(xWhole, scale, BigDecimal.ROUND_HALF_EVEN));

    // t = e^z
    BigDecimal t = expTaylor(z, scale);

    BigDecimal maxLong = BigDecimal.valueOf(Long.MAX_VALUE);
    BigDecimal result = BigDecimal.valueOf(1);

    // Compute and return t^whole using intPower().
    // If whole > Long.MAX_VALUE, then first compute products
    // of e^Long.MAX_VALUE.
    while (xWhole.compareTo(maxLong) >= 0) {
        result = result.multiply(intPower(t, Long.MAX_VALUE, scale)).setScale(scale,
                BigDecimal.ROUND_HALF_EVEN);
        xWhole = xWhole.subtract(maxLong);

        Thread.yield();
    }
    return result.multiply(intPower(t, xWhole.longValue(), scale)).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:org.sipfoundry.sipxconfig.common.BackgroundTaskQueue.java

/**
 * This is one of the methods that can be used to actively wait till queue is empty Do not use
 * it unless you need it in testing./*from   w  w w . j a  v a  2  s .  c o m*/
 */
public void yieldTillEmpty() {
    Runnable sentinel = new Runnable() {
        public void run() {
            // do nothing
        }
    };
    m_queue.add(sentinel);
    while (!m_queue.isEmpty()) {
        Thread.yield();
    }
}

From source file:MainClass.java

public void run() {

    try {/*w  ww.j ava 2s .  c om*/
        BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            if (stopped)
                return;
            String theLine = userInput.readLine();
            if (theLine.equals("."))
                break;
            byte[] data = theLine.getBytes();
            DatagramPacket output = new DatagramPacket(data, data.length, server, port);
            socket.send(output);
            Thread.yield();
        }
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:com.ottogroup.bi.asap.component.strategy.YieldMessageWaitStrategy.java

/**
 * @see com.mnxfst.distos.strategy.MessageWaitStrategy#waitFor()
 */
public void waitFor() throws InterruptedException {
    Thread.yield();
}

From source file:com.thoughtworks.go.plugin.infra.monitor.AbstractDefaultPluginJarLocationMonitorTest.java

protected void waitUntilNextRun(DefaultPluginJarLocationMonitor monitor) throws InterruptedException {
    long previousRun = monitor.getLastRun();
    int numberOfTries = 0;
    while (previousRun >= monitor.getLastRun() && numberOfTries < NO_OF_TRIES_TO_CHECK_MONITOR_RUN) {
        Thread.yield();
        Thread.sleep(500);//www.ja v  a2 s  .  c om
        numberOfTries++;
    }
    if (numberOfTries >= NO_OF_TRIES_TO_CHECK_MONITOR_RUN) {
        throw new RuntimeException("Number of tries exceeded, but monitor thread hasn't run yet");
    }
}

From source file:org.apache.streams.util.ComponentUtils.java

public static String pollUntilStringNotEmpty(Queue queue) {

    String result = null;//from w w  w.  ja v a 2  s .  c o m
    do {
        synchronized (ComponentUtils.class) {
            try {
                result = (String) queue.remove();
            } catch (Exception e) {
            }
        }
        Thread.yield();
    } while (result == null && !StringUtils.isNotEmpty(result));

    return result;
}