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:org.archive.util.ObjectIdentityBdbCacheTest.java

@SuppressWarnings("unchecked")
public void testReadConsistencyUnderLoad() throws Exception {
    final ObjectIdentityBdbCache<IdentityCacheableWrapper<AtomicInteger>> cbdbmap = new ObjectIdentityBdbCache();
    cbdbmap.initialize(env, "consistencyCache", IdentityCacheableWrapper.class, env.getClassCatalog());
    try {//from   w  ww  .  j av  a  2s  .  c o  m
        final AtomicInteger level = new AtomicInteger(0);
        final int keyCount = 128 * 1024; // 128K  keys
        final int maxLevel = 64;
        // initial fill
        for (int i = 0; i < keyCount; i++) {
            final String key = "" + i;
            cbdbmap.getOrUse(key, new Supplier<IdentityCacheableWrapper<AtomicInteger>>(
                    new IdentityCacheableWrapper<AtomicInteger>(key, new AtomicInteger(level.get()))));
        }
        // backward checking that all values always at level or higher
        new Thread() {
            public void run() {
                untilmax: while (true) {
                    for (int j = keyCount - 1; j >= 0; j--) {
                        int targetValue = level.get();
                        if (targetValue >= maxLevel) {
                            break untilmax;
                        }
                        assertTrue("stale value revseq key " + j,
                                cbdbmap.get("" + j).get().get() >= targetValue);
                        Thread.yield();
                    }
                }
            }
        };//.start();
          // random checking that all values always at level or higher
        new Thread() {
            public void run() {
                untilmax: while (true) {
                    int j = RandomUtils.nextInt(keyCount);
                    int targetValue = level.get();
                    if (targetValue >= maxLevel) {
                        break untilmax;
                    }
                    assertTrue("stale value random key " + j, cbdbmap.get("" + j).get().get() >= targetValue);
                    Thread.yield();
                }
            }
        };//.start();
          // increment all keys
        for (; level.get() < maxLevel; level.incrementAndGet()) {
            for (int k = 0; k < keyCount; k++) {
                int foundValue = cbdbmap.get("" + k).get().getAndIncrement();
                assertEquals("stale value preinc key " + k, level.get(), foundValue);
            }
            if (level.get() % 10 == 0) {
                System.out.println("level to " + level.get());
                if (level.get() > 0) {
                    TestUtils.forceScarceMemory();
                }
                System.out.println("OIBCT:" + cbdbmap.composeCacheSummary());
            }
            Thread.yield();
        }
    } finally {
        System.err.println("OIBCT:" + cbdbmap.composeCacheSummary());
        cbdbmap.close();
    }
    // SUCCESS
}

From source file:edu.upenn.cis.stormlite.DistributedCluster.java

public void run() {
    while (!quit.get()) {
        Runnable task = taskQueue.poll();
        if (task == null)
            Thread.yield();
        else {//from   w  ww  .ja v  a 2 s.  c  o m
            executor.execute(task);
        }
    }
}

From source file:org.geowebcache.stats.RuntimeStats.java

public void destroy() {
    if (this.statsThread != null) {
        statsThread.run = false;

        statsThread.interrupt();

        Thread.yield();
    }
}

From source file:net.chat.utils.Cache.java

@SuppressWarnings("unchecked")
public void cleanup() {

    long now = System.currentTimeMillis();
    ArrayList<K> keysToDelete = null;

    synchronized (cacheMap) {
        MapIterator itr = cacheMap.mapIterator();

        keysToDelete = new ArrayList<K>((cacheMap.size() / 2) + 1);
        K key = null;/*from ww w. j av a  2s. com*/
        CachedObject c = null;

        while (itr.hasNext()) {
            key = (K) itr.next();
            c = (CachedObject) itr.getValue();

            if (c != null && (now > (timeToLiveInMillis + c.lastAccessed))) {
                keysToDelete.add(key);
            }
        }
    }

    for (K key : keysToDelete) {
        synchronized (cacheMap) {
            cacheMap.remove(key);
        }

        Thread.yield();
    }
}

From source file:org.archive.util.ObjectIdentityBdbManualCacheTest.java

@SuppressWarnings("unchecked")
public void testReadConsistencyUnderLoad() throws Exception {
    final ObjectIdentityBdbManualCache<IdentityCacheableWrapper<AtomicInteger>> cbdbmap = new ObjectIdentityBdbManualCache();
    cbdbmap.initialize(env, "consistencyCache", IdentityCacheableWrapper.class, env.getClassCatalog());
    try {//ww w . ja v  a 2 s  .  c  om
        final AtomicInteger level = new AtomicInteger(0);
        final int keyCount = 128 * 1024; // 128K  keys
        final int maxLevel = 64;
        // initial fill
        for (int i = 0; i < keyCount; i++) {
            final String key = "" + i;
            cbdbmap.getOrUse(key, new Supplier<IdentityCacheableWrapper<AtomicInteger>>(
                    new IdentityCacheableWrapper<AtomicInteger>(key, new AtomicInteger(level.get()))));
        }
        // backward checking that all values always at level or higher
        new Thread() {
            public void run() {
                untilmax: while (true) {
                    for (int j = keyCount - 1; j >= 0; j--) {
                        int targetValue = level.get();
                        if (targetValue >= maxLevel) {
                            break untilmax;
                        }
                        assertTrue("stale value revseq key " + j,
                                cbdbmap.get("" + j).get().get() >= targetValue);
                        Thread.yield();
                    }
                }
            }
        };//.start();
          // random checking that all values always at level or higher
        new Thread() {
            public void run() {
                untilmax: while (true) {
                    int j = RandomUtils.nextInt(keyCount);
                    int targetValue = level.get();
                    if (targetValue >= maxLevel) {
                        break untilmax;
                    }
                    assertTrue("stale value random key " + j, cbdbmap.get("" + j).get().get() >= targetValue);
                    Thread.yield();
                }
            }
        };//.start();
          // increment all keys
        for (; level.get() < maxLevel; level.incrementAndGet()) {
            for (int k = 0; k < keyCount; k++) {
                IdentityCacheableWrapper<AtomicInteger> wrap = cbdbmap.get("" + k);
                int foundValue = wrap.get().getAndIncrement();
                wrap.makeDirty();
                assertEquals("stale value preinc key " + k, level.get(), foundValue);
            }
            if (level.get() % 10 == 0) {
                System.out.println("level to " + level.get());
                if (level.get() > 0) {
                    TestUtils.forceScarceMemory();
                }
                System.out.println("OIBMCT:" + cbdbmap.composeCacheSummary());
            }
            Thread.yield();
        }
    } finally {
        System.err.println("OIBMCT:" + cbdbmap.composeCacheSummary());
        cbdbmap.close();
    }
    // SUCCESS
}

From source file:netinf.node.transferdispatcher.chunkstreams.BufferFiller.java

@Override
public void run() {
    while (curr <= max && !isInterrupted()) {
        InputStream in = null;//  w ww  .j a v  a  2s. c om
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            for (String baseUrl : baseUrls) {
                try {
                    Chunk chunk = getChunkByNumber(curr);
                    LOG.log(DemoLevel.DEMO, "(TD ) Serving Chunk No: " + curr + " of total " + max + 1);
                    in = TransferDispatcher.getInstance().getStream(chunk, baseUrl);
                    curr += incr;
                    IOUtils.copy(in, out);
                    bufferQueue.put(out.toByteArray());
                    Thread.yield();
                    break; // break for loop over base urls
                } catch (NetInfNoStreamProviderFoundException e) {
                    LOG.warn("(BufferFiller ) No Streamprivder found: " + e.getMessage());
                }
            }
        } catch (InterruptedException e) {
            interrupt();
            LOG.info("(BufferFiller ) Thread interrupted");
        } catch (IOException e) {
            LOG.warn("(BufferFiller ) IOException:" + e.getMessage());
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    // signal end
    try {
        bufferQueue.put(new byte[] { -1 });
    } catch (InterruptedException e) {
        LOG.info("(BufferFiller ) Thread interrupted");
    }
    LOG.debug("(BufferFiller ) End of Thread");
}

From source file:org.apache.hadoop.hbase.rest.TestRowResource.java

private static Response deleteRow(String table, String row) throws IOException {
    StringBuilder path = new StringBuilder();
    path.append('/');
    path.append(table);// w  ww.j  a v a 2 s.  com
    path.append('/');
    path.append(row);
    Response response = client.delete(path.toString());
    Thread.yield();
    return response;
}

From source file:PureImmediate.java

public void run() {
    System.out.println("PureImmediate.run: starting main loop");
    while (true) {
        render();
        Thread.yield();
    }
}

From source file:org.apache.hadoop.hbase.stargate.TestRowResource.java

Response deleteRow(String table, String row) throws IOException {
    StringBuilder path = new StringBuilder();
    path.append('/');
    path.append(table);/*  w w  w . j  a va  2  s.  c  o  m*/
    path.append('/');
    path.append(row);
    Response response = client.delete(path.toString());
    Thread.yield();
    return response;
}

From source file:com.auditbucket.engine.endpoint.TrackEP.java

public Future<Integer> trackHeadersAsync(List<MetaInputBean> inputBeans, boolean async, String apiKey)
        throws DatagioException {
    Company company = registrationService.resolveCompany(apiKey);
    Fortress fortress = fortressService.registerFortress(company,
            new FortressInputBean(inputBeans.iterator().next().getFortress()), true);
    if (async) {/*  ww  w. j  a  v a2 s .c o m*/
        Future<Integer> batch = mediationFacade.createHeadersAsync(company, fortress, inputBeans);
        Thread.yield();
        return batch;

    } else {
        return new AsyncResult<>(mediationFacade.createHeaders(company, fortress, inputBeans));
    }
}