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

public static void copy(InputStream in, File dst) throws IOException {
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = buffers.get();
    int len;//from   ww  w .j  av a2s.c  o m
    while ((len = in.read(buf)) > 0) {
        Thread.yield();
        out.write(buf, 0, len);
    }
    out.close();
}

From source file:Main.java

/**
 * Certain types of queues will fail to {@link java.util.Queue#offer(Object)} an item due to many factors
 * depending on the type of queue. <code>offerUntilSuccess</code> will not return until the item has been
 * successfully queued onto the desired queue
 * @param entry item to queue//  ww  w  . j  a  v  a  2s  .  c o  m
 * @param queue queue to add the entry to
 * @param <T>
 */
public static <T> void offerUntilSuccess(T entry, Queue<T> queue) {
    boolean success;
    do {
        success = queue.offer(entry);
        Thread.yield();
    } while (!success);
}

From source file:ThreadDemo.java

public void run() {
    try {/*from  w w  w  .j a v a  2 s .  com*/
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + "yielding control...");
    Thread.yield();

    System.out.println(Thread.currentThread().getName() + " has finished executing.");
}

From source file:Main.java

/**
 * Method to add a random delay or if none to yield to give any waiting
 * threads a chance. This helps make test threaded code more random to track
 * synchronized issues./*from   ww  w  .  j  a  v a 2s  .  c om*/
 *
 * @param millis
 *            up to the many ms
 * @return actual sleep time
 */
public static final int sleepUpTo(final long millis) {
    try {
        if (millis > 0) {
            int realSleep = (int) Math.floor(Math.random() * (millis + 1));
            if (realSleep > 0) {
                Thread.sleep(realSleep);
                return realSleep;
            } else {
                // Give any waiting threads a go
                Thread.yield();
                return 0;
            }
        }
    } catch (final InterruptedException e) {
        // It's not an error that we were Interrupted!! Don't log the
        // exception !
        return -1;
    }
    return 0;
}

From source file:FlagComm.java

public void run() {
    for (int i = 0; i < 5; i++) {
        while (isValid) {
            Thread.yield();
        }/*from   w  ww .  j  a va  2 s. c  o  m*/
        theValue = (int) (Math.random() * 256);
        System.out.println("sending " + theValue);
        isValid = true;
    }
}

From source file:Main.java

/**
 * Compute the square root of x to a given scale, x >= 0. Use Newton's
 * algorithm.//from w  w w .jav  a 2  s.  c  o m
 * 
 * @param x
 *            the value of x
 * @return the result value
 */
public static BigDecimal sqrt(BigDecimal x) {
    // Check that x >= 0.
    if (x.signum() < 0) {
        throw new ArithmeticException("x < 0");
    }

    // n = x*(10^(2*SCALE))
    BigInteger n = x.movePointRight(SCALE << 1).toBigInteger();

    // The first approximation is the upper half of n.
    int bits = (n.bitLength() + 1) >> 1;
    BigInteger ix = n.shiftRight(bits);
    BigInteger ixPrev;

    // Loop until the approximations converge
    // (two successive approximations are equal after rounding).
    do {
        ixPrev = ix;

        // x = (x + n/x)/2
        ix = ix.add(n.divide(ix)).shiftRight(1);

        Thread.yield();
    } while (ix.compareTo(ixPrev) != 0);

    return new BigDecimal(ix, SCALE);
}

From source file:PriorityCompete.java

private void runWork() {
    Thread.yield();

    while (noStopRequested) {
        if (yield) {
            Thread.yield();/*from   w  ww .ja v  a  2  s .c  o m*/
        }

        count++;

        for (int i = 0; i < 1000; i++) {
            double x = i * Math.PI / Math.E;
        }
    }
}

From source file:Main.java

/**
 * Compute x^exponent to a given scale. Uses the same algorithm as class
 * numbercruncher.mathutils.IntPower.//from   w w  w. j  a  va  2s . c  o m
 * 
 * @param x
 *            the value x
 * @param exponent
 *            the exponent value
 * @param scale
 *            the desired scale of the result
 * @return the result value
 */
public static BigDecimal intPower(BigDecimal x, long exponent, int scale) {
    // If the exponent is negative, compute 1/(x^-exponent).
    if (exponent < 0) {
        return BigDecimal.valueOf(1).divide(intPower(x, -exponent, scale), scale, BigDecimal.ROUND_HALF_EVEN);
    }

    BigDecimal power = BigDecimal.valueOf(1);

    // Loop to compute value^exponent.
    while (exponent > 0) {

        // Is the rightmost bit a 1?
        if ((exponent & 1) == 1) {
            power = power.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        }

        // Square x and shift exponent 1 bit to the right.
        x = x.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);
        exponent >>= 1;

        Thread.yield();
    }

    return power;
}

From source file:FlagComm.java

public void run() {
    while (true) {
        while (!theSender.isValid) {
            Thread.yield();
        }//  ww w.  java2 s.c  o m
        System.out.println("received " + theSender.theValue);
        theSender.isValid = false;
    }
}

From source file:org.acdd.ext.bundleInfo.maker.ApkPreProcess.java

public static void preProcess(String mDir) {
    Collection<File> apkFiles = org.apache.commons.io.FileUtils.listFiles(new File(mDir),
            new String[] { "apk" }, true);

    for (File file : apkFiles) {
        String pkgName = PackageLite.parse(file.getAbsolutePath()).packageName;

        pkgName = "lib" + pkgName.replaceAll("\\.", "_") + ".so";
        File targetFile = new File(mDir + File.separator + pkgName);
        if (targetFile.exists())
            targetFile.delete();/*from ww  w. j  a  v a 2  s. c  o m*/

        System.out.println("rename: " + file.getName() + " -> " + pkgName);
        while (!file.renameTo(targetFile)) {
            System.gc();
            Thread.yield();
        }
        System.out.println("ApkPreProcess.preProcess() processed " + pkgName);
    }
}