本文整理匯總了Java中java.util.concurrent.DelayQueue類的典型用法代碼示例。如果您正苦於以下問題:Java DelayQueue類的具體用法?Java DelayQueue怎麽用?Java DelayQueue使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
DelayQueue類屬於java.util.concurrent包,在下文中一共展示了DelayQueue類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testGet_concurrent
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
public void testGet_concurrent() {
assertTrue(ArbitraryInstances.get(BlockingDeque.class).isEmpty());
assertTrue(ArbitraryInstances.get(BlockingQueue.class).isEmpty());
assertTrue(ArbitraryInstances.get(DelayQueue.class).isEmpty());
assertTrue(ArbitraryInstances.get(SynchronousQueue.class).isEmpty());
assertTrue(ArbitraryInstances.get(PriorityBlockingQueue.class).isEmpty());
assertTrue(ArbitraryInstances.get(ConcurrentMap.class).isEmpty());
assertTrue(ArbitraryInstances.get(ConcurrentNavigableMap.class).isEmpty());
ArbitraryInstances.get(Executor.class).execute(ArbitraryInstances.get(Runnable.class));
assertNotNull(ArbitraryInstances.get(ThreadFactory.class));
assertFreshInstanceReturned(
BlockingQueue.class, BlockingDeque.class, PriorityBlockingQueue.class,
DelayQueue.class, SynchronousQueue.class,
ConcurrentMap.class, ConcurrentNavigableMap.class,
AtomicReference.class, AtomicBoolean.class,
AtomicInteger.class, AtomicLong.class, AtomicDouble.class);
}
示例2: main
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
public static void main(String[] args) throws Throwable {
final DelayQueue<Delayed> q = new DelayQueue<>();
final long t0 = System.nanoTime();
for (long i = 0; i < 1000; i++) {
final long expiry = t0 + i*10L*1000L*1000L;
q.add(new Delayed() {
public long getDelay(TimeUnit unit) {
return unit.convert(expiry - System.nanoTime(),
NANOSECONDS);
}
public int compareTo(Delayed x) {
long d = getDelay(NANOSECONDS)
- x.getDelay(NANOSECONDS);
return d < 0 ? -1 : d > 0 ? 1 : 0; }});
}
for (int i = 0; i < 300; i++)
new Thread() { public void run() {
try {
while (!q.isEmpty())
q.poll(10L, TimeUnit.SECONDS);
} catch (Throwable t) { t.printStackTrace(); }
}}.start();
}
示例3: realMain
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
private static void realMain(String[] args) throws Throwable {
Godot[] godots = new Godot[] { new Godot(), new Godot(), new Godot() };
DelayQueue<Godot> q = new DelayQueue<>(Arrays.asList(godots));
Iterator<Godot> it = q.iterator();
q.clear();
check(it.hasNext());
equal(it.next(), godots[0]);
it.remove();
check(q.isEmpty());
q.addAll(Arrays.asList(godots));
it = q.iterator();
check(it.hasNext());
it.next();
equal(it.next(), godots[1]);
it.remove();
equal(q.size(), 2);
check(q.contains(godots[0]));
check(q.contains(godots[2]));
}
示例4: testRetainAll
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
/**
* retainAll(c) retains only those elements of c and reports true if changed
*/
public void testRetainAll() {
DelayQueue q = populatedQueue(SIZE);
DelayQueue p = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
boolean changed = q.retainAll(p);
if (i == 0)
assertFalse(changed);
else
assertTrue(changed);
assertTrue(q.containsAll(p));
assertEquals(SIZE - i, q.size());
p.remove();
}
}
示例5: testPollInExecutor
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
/**
* timed poll transfers elements across Executor tasks
*/
public void testPollInExecutor() {
final DelayQueue q = new DelayQueue();
final CheckedBarrier threadsStarted = new CheckedBarrier(2);
final ExecutorService executor = Executors.newFixedThreadPool(2);
try (PoolCleaner cleaner = cleaner(executor)) {
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
assertNull(q.poll());
threadsStarted.await();
assertNotNull(q.poll(LONG_DELAY_MS, MILLISECONDS));
checkEmpty(q);
}});
executor.execute(new CheckedRunnable() {
public void realRun() throws InterruptedException {
threadsStarted.await();
q.put(new PDelay(1));
}});
}
}
示例6: testDelay
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
/**
* Delayed actions do not occur until their delay elapses
*/
public void testDelay() throws InterruptedException {
DelayQueue<NanoDelay> q = new DelayQueue<>();
for (int i = 0; i < SIZE; ++i)
q.add(new NanoDelay(1000000L * (SIZE - i)));
long last = 0;
for (int i = 0; i < SIZE; ++i) {
NanoDelay e = q.take();
long tt = e.getTriggerTime();
assertTrue(System.nanoTime() - tt >= 0);
if (i != 0)
assertTrue(tt >= last);
last = tt;
}
assertTrue(q.isEmpty());
}
示例7: testDrainTo
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
/**
* drainTo(c) empties queue into another collection c
*/
public void testDrainTo() {
DelayQueue q = new DelayQueue();
PDelay[] elems = new PDelay[SIZE];
for (int i = 0; i < SIZE; ++i) {
elems[i] = new PDelay(i);
q.add(elems[i]);
}
ArrayList l = new ArrayList();
q.drainTo(l);
assertEquals(0, q.size());
for (int i = 0; i < SIZE; ++i)
assertEquals(elems[i], l.get(i));
q.add(elems[0]);
q.add(elems[1]);
assertFalse(q.isEmpty());
assertTrue(q.contains(elems[0]));
assertTrue(q.contains(elems[1]));
l.clear();
q.drainTo(l);
assertEquals(0, q.size());
assertEquals(2, l.size());
for (int i = 0; i < 2; ++i)
assertEquals(elems[i], l.get(i));
}
示例8: main
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
public static void main(String[] args) throws Throwable {
final DelayQueue<Delayed> q = new DelayQueue<Delayed>();
final long t0 = System.nanoTime();
for (long i = 0; i < 1000; i++) {
final long expiry = t0 + i*10L*1000L*1000L;
q.add(new Delayed() {
public long getDelay(TimeUnit unit) {
return unit.convert(expiry - System.nanoTime(),
NANOSECONDS);
}
public int compareTo(Delayed x) {
long d = getDelay(NANOSECONDS)
- x.getDelay(NANOSECONDS);
return d < 0 ? -1 : d > 0 ? 1 : 0; }});
}
for (int i = 0; i < 300; i++)
new Thread() { public void run() {
try {
while (!q.isEmpty())
q.poll(10L, TimeUnit.SECONDS);
} catch (Throwable t) { t.printStackTrace(); }
}}.start();
}
示例9: realMain
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
private static void realMain(String[] args) throws Throwable {
Godot[] godots = new Godot[] { new Godot(), new Godot(), new Godot() };
DelayQueue<Godot> q = new DelayQueue<Godot>(Arrays.asList(godots));
Iterator<Godot> it = q.iterator();
q.clear();
check(it.hasNext());
equal(it.next(), godots[0]);
it.remove();
check(q.isEmpty());
q.addAll(Arrays.asList(godots));
it = q.iterator();
check(it.hasNext());
it.next();
equal(it.next(), godots[1]);
it.remove();
equal(q.size(), 2);
check(q.contains(godots[0]));
check(q.contains(godots[2]));
}
示例10: testDelay
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
/**
* Delayed actions do not occur until their delay elapses
*/
public void testDelay() throws InterruptedException {
DelayQueue<NanoDelay> q = new DelayQueue<NanoDelay>();
for (int i = 0; i < SIZE; ++i)
q.add(new NanoDelay(1000000L * (SIZE - i)));
long last = 0;
for (int i = 0; i < SIZE; ++i) {
NanoDelay e = q.take();
long tt = e.getTriggerTime();
assertTrue(System.nanoTime() - tt >= 0);
if (i != 0)
assertTrue(tt >= last);
last = tt;
}
assertTrue(q.isEmpty());
}
示例11: HostSelector
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
public HostSelector(Collection<String> baseHosts, String signature)
{
if (baseHosts.size() < 1)
{
throw new IllegalArgumentException("At least one host must be provided.");
}
this.fBaseHosts = new TreeSet(baseHosts);
this.fBlacklist = new DelayQueue();
this.fIdealHost = null;
if (signature == null) {
return;
}
int index = Math.abs(signature.hashCode()) % baseHosts.size();
Iterator it = this.fBaseHosts.iterator();
while (index-- > 0)
{
it.next();
}
this.fIdealHost = ((String)it.next());
}
示例12: HostSelector
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
public HostSelector ( Collection<String> baseHosts, String signature )
{
if ( baseHosts.size () < 1 )
{
throw new IllegalArgumentException ( "At least one host must be provided." );
}
fBaseHosts = new TreeSet<String> ( baseHosts );
fBlacklist = new DelayQueue<BlacklistEntry> ();
fIdealHost = null;
if ( signature != null )
{
// map the signature into an index in the host set
int index = Math.abs ( signature.hashCode () ) % baseHosts.size();
// iterate to the selected host
Iterator<String> it = fBaseHosts.iterator ();
while ( index-- > 0 )
{
it.next ();
}
fIdealHost = it.next ();
}
}
示例13: EntityLruCache
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
public EntityLruCache ( int maxSize, long maxObjCacheTime, TimeUnit maxObjCacheTimeUnit )
{
//A load factor > 1 along with a size limit guarantees that the map will not be resized
super(maxSize, 1.25f, true);
if (maxSize <= 0)
throw new IllegalArgumentException("Cache size must be greater than 0");
this.MAX_ENTRIES = maxSize;
this.hits = 0;
this.misses = 0;
fMaxAgeMs = TimeUnit.MILLISECONDS.convert ( maxObjCacheTime, maxObjCacheTimeUnit );
fTimers = new DelayQueue<TimerEntry> ();
fClock = null;
}
示例14: LocalDataCenterEndPointProvider
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
@VisibleForTesting
LocalDataCenterEndPointProvider(CuratorFramework curator,
InvalidationServiceEndPointAdapter endPointAdapter,
ServiceEndPoint self,
MetricRegistry metricRegistry,
LifeCycleRegistry lifeCycleRegistry,
ExecutorService delayedInvalidationService) {
_curator = curator;
_endPointAdapter = endPointAdapter;
_self = self;
_metricRegistry = metricRegistry;
_delayedInvalidationService = delayedInvalidationService;
_delayedInvalidationQueue = new DelayQueue<>();
lifeCycleRegistry.manage(this);
}
示例15: NameNodeService
import java.util.concurrent.DelayQueue; //導入依賴的package包/類
public NameNodeService() throws IOException {
URI uri = URI.create(CrailConstants.NAMENODE_ADDRESS);
String query = uri.getRawQuery();
StringTokenizer tokenizer = new StringTokenizer(query, "&");
this.serviceId = Long.parseLong(tokenizer.nextToken().substring(3));
this.serviceSize = Long.parseLong(tokenizer.nextToken().substring(5));
this.sequenceId = new AtomicLong(serviceId);
this.blockStore = new BlockStore();
this.deleteQueue = new DelayQueue<AbstractNode>();
this.fileTree = new FileStore(this);
this.fileTable = new ConcurrentHashMap<Long, AbstractNode>();
this.gcServer = new GCServer(this, deleteQueue);
AbstractNode root = fileTree.getRoot();
fileTable.put(root.getFd(), root);
Thread gc = new Thread(gcServer);
gc.start();
}