當前位置: 首頁>>代碼示例>>Java>>正文


Java BlockableQueue.setBlocking方法代碼示例

本文整理匯總了Java中org.magnos.util.BlockableQueue.setBlocking方法的典型用法代碼示例。如果您正苦於以下問題:Java BlockableQueue.setBlocking方法的具體用法?Java BlockableQueue.setBlocking怎麽用?Java BlockableQueue.setBlocking使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.magnos.util.BlockableQueue的用法示例。


在下文中一共展示了BlockableQueue.setBlocking方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testPeekInterrupt

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testPeekInterrupt()
{
	final AtomicReference<Thread> thread = new AtomicReference<Thread>();
	final BlockableQueue<String> q = new BlockableQueue<String>();
	q.setBlocking(true);
	q.setTimeout(500);
	
	GroupTask.initialize(1);
	GroupTask.add(new Runnable() {
		public void run() {
			thread.set(Thread.currentThread());
			assertNull( q.peek() );
		}
	});
	GroupTask.begin();
	
	sleep(100);
	
	thread.get().interrupt();
	
	GroupTask.finish();
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:24,代碼來源:TestBlockableQueue.java

示例2: testPollInterrupt

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testPollInterrupt()
{
	final AtomicReference<Thread> thread = new AtomicReference<Thread>();
	final BlockableQueue<String> q = new BlockableQueue<String>();
	q.setBlocking(true);
	q.setTimeout(500);
	
	GroupTask.initialize(1);
	GroupTask.add(new Runnable() {
		public void run() {
			thread.set(Thread.currentThread());
			assertNull( q.poll() );
		}
	});
	GroupTask.begin();
	
	sleep(100);
	
	thread.get().interrupt();
	
	GroupTask.finish();
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:24,代碼來源:TestBlockableQueue.java

示例3: testNonblocking

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testNonblocking()
{
	BlockableQueue<String> q = new BlockableQueue<String>();
	q.setBlocking(false);
	
	assertTrue( q.offer("Hello") );
	assertTrue( q.offer("World") );

	assertEquals( "Hello", q.peek() );
	assertEquals( "Hello", q.poll() );
	
	assertEquals( "World", q.peek() );
	assertEquals( "World", q.poll() );
	
	assertNull( q.poll() );
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:18,代碼來源:TestBlockableQueue.java

示例4: testIsBlocking

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testIsBlocking()
{
	BlockableQueue<String> q = new BlockableQueue<String>();
	
	q.setBlocking(true);
	assertTrue( q.isBlocking() );
	
	q.setBlocking(false);
	assertFalse( q.isBlocking() );
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:12,代碼來源:TestBlockableQueue.java

示例5: testPeekTimeout

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testPeekTimeout()
{
	BlockableQueue<String> q = new BlockableQueue<String>();
	q.setBlocking(true);
	q.setTimeout(100);
	
	assertNull( q.peek() );
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:10,代碼來源:TestBlockableQueue.java

示例6: testPollTimeout

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testPollTimeout()
{
	BlockableQueue<String> q = new BlockableQueue<String>();
	q.setBlocking(true);
	q.setTimeout(100);
	
	assertNull( q.poll() );
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:10,代碼來源:TestBlockableQueue.java

示例7: testWakeup

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testWakeup()
{
	final BlockableQueue<String> q = new BlockableQueue<String>();
	q.setBlocking(true);
	
	GroupTask.initialize(1);
	GroupTask.add(new Runnable() {
		public void run() {
			sleep(500);
			q.wakeup();
			sleep(500);
			q.wakeup();
		}
	});
	GroupTask.begin();
	
	watch.start("Before poll...");
	assertNull( null, q.poll() );
	watch.stop(" poll awoken (%.3f), done.\n");
	
	watch.start("Before peek...");
	assertNull( null, q.peek() );
	watch.stop(" peek awoken (%.3f), done.\n");
	
	GroupTask.finish();
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:28,代碼來源:TestBlockableQueue.java

示例8: testSingleReaderManyWriters

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testSingleReaderManyWriters()
{
	final int MESSAGES = 10000;
	final int WRITERS = 10;
	final int MESSAGES_PER_WRITER = MESSAGES / WRITERS;
	
	final AtomicInteger nullCounter = new AtomicInteger();
	final BlockableQueue<Integer> q = new BlockableQueue<Integer>();
	q.setBlocking(true);
	
	Runnable reader = new Runnable() {
		public void run() {
			int total = 0;
			double minWait = Double.MAX_VALUE;
			double maxWait = -Double.MAX_VALUE;
			Stopwatch watch = new Stopwatch();
			while (total < MESSAGES) {
				watch.start();
				Integer value = q.poll();
				watch.stop();
				if (value != null) {
					minWait = Math.min(minWait, watch.seconds());
					maxWait = Math.max(maxWait, watch.seconds());
					total++;	
				} else {
					nullCounter.incrementAndGet();
				}
			}
			System.out.format("Wait: min[%.6fs] max[%.6fs]\n", minWait, maxWait);
		}
	};
	
	Runnable writer = new Runnable() {
		public void run() {
			for (int i = 0; i < MESSAGES_PER_WRITER; i++) {
				assertTrue( q.offer(i) );
				// Causes the reader to wait some amount of time
				sleep(1);
			}
		}
	};
	
	GroupTask.initialize(WRITERS + 1);
	GroupTask.add(writer, WRITERS);
	GroupTask.add(reader);
	GroupTask.execute();
	
	System.out.println("Null polls: " + nullCounter.get());
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:51,代碼來源:TestBlockableQueue.java

示例9: testSingleWriterManyReads

import org.magnos.util.BlockableQueue; //導入方法依賴的package包/類
@Test
public void testSingleWriterManyReads()
{
	final int MESSAGES = 10000;
	final int READERS = 10;
	final int MESSAGES_PER_READER = MESSAGES / READERS;

	final AtomicInteger nullCounter = new AtomicInteger();
	final BlockableQueue<Integer> q = new BlockableQueue<Integer>();
	q.setBlocking(true);
	
	Runnable reader = new Runnable() {
		public void run() {
			int total = 0;
			double minWait = Double.MAX_VALUE;
			double maxWait = -Double.MAX_VALUE;
			Stopwatch watch = new Stopwatch();
			while (total < MESSAGES_PER_READER) {
				watch.start();
				Integer value = q.poll();
				watch.stop();
				if (value != null) {
					minWait = Math.min(minWait, watch.seconds());
					maxWait = Math.max(maxWait, watch.seconds());
					total++;	
				} else {
					nullCounter.incrementAndGet();
				}
			}
			System.out.format("Wait: min[%.6fs] max[%.6fs]\n", minWait, maxWait);
		}
	};
	
	Runnable writer = new Runnable() {
		public void run() {
			for (int i = 0; i < MESSAGES; i++) {
				assertTrue( q.offer(i) );
			}
		}
	};
	
	GroupTask.initialize(READERS + 1);
	GroupTask.add(reader, READERS);
	GroupTask.add(writer);
	GroupTask.execute();
	
	System.out.println("Null polls: " + nullCounter.get());
}
 
開發者ID:ClickerMonkey,項目名稱:Curity,代碼行數:49,代碼來源:TestBlockableQueue.java


注:本文中的org.magnos.util.BlockableQueue.setBlocking方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。