org.jctools.queues.MpscArrayQueue Java Examples

The following examples show how to use org.jctools.queues.MpscArrayQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestQueueGroup2.java    From util4j with Apache License 2.0 6 votes vote down vote up
protected QueueGroupExecutor buildByMpMc(int minThread,int maxThread,int maxQueue,int maxPendingTask)
{
	//多生产多消费者队列(线程竞争队列)
	Queue<Runnable> bossQueue=new MpscArrayQueue<>(maxQueue);
	QueueFactory qf=new QueueFactory() {
		@Override
		public RunnableQueue buildQueue() {
			//多生产单消费者队列(PS:bossQueue决定了一个队列只能同时被一个线程处理)
			Queue<Runnable> queue=MpscLinkedQueue.newMpscLinkedQueue();
			return new RunnableQueueWrapper(queue);
		}
	};
	QueueGroupManager kqm=new DefaultQueueManager(qf);
	NioQueueGroupExecutor.Builder b=new NioQueueGroupExecutor.Builder();
	b.setAssistExecutor(Executors.newSingleThreadExecutor());
	return b.setMaxPoolSize(minThread).setCorePoolSize(maxThread).setBossQueue(bossQueue).setQueueGroupManagerr(kqm).build();
}
 
Example #2
Source File: XActorThread.java    From actor4j-core with Apache License 2.0 6 votes vote down vote up
public XActorThread(ThreadGroup group, String name, ActorSystemImpl system) {
	super(group, name, system);
	
	directiveQueue = new MpscArrayQueue<>(system.getQueueSize());
	priorityQueue  = new PriorityBlockingQueue<>(system.getQueueSize());
	serverQueueL2  = new MpscArrayQueue<>(system.getQueueSize());
	serverQueueL1  = new ArrayDeque<>(system.getBufferQueueSize());
	outerQueueL2B  = new MpscLinkedQueue8<>(); 
	outerQueueL2A  = new MpscArrayQueue<>(system.getQueueSize());
	outerQueueL1   = new ArrayDeque<>(system.getBufferQueueSize());
	innerQueueL2   = new LinkedList<>();
	innerQueueL1   = new CircularFifoQueue<>(system.getQueueSize());
	
	innerQueueAntiFloodingTimer = ((XActorSystemImpl)system).factoryAntiFloodingTimer.get();
	outerQueueAntiFloodingTimer = ((XActorSystemImpl)system).factoryAntiFloodingTimer.get();
	
	newMessage = new AtomicBoolean(true);
}
 
Example #3
Source File: TCPControllerTest.java    From kieker with Apache License 2.0 6 votes vote down vote up
/**
 * initialize configuration for TCP controller.
 * 
 * @throws IOException on io errors
 */
@BeforeClass
public static void init() throws IOException {
	// setup for other depending parts
	configuration.setProperty(ConfigurationKeys.ADAPTIVE_MONITORING_ENABLED, true);
	configuration.setProperty(ConfigurationKeys.WRITER_CLASSNAME, DumpWriter.class.getName());
	configuration.setProperty(WriterController.PREFIX + WriterController.RECORD_QUEUE_FQN,
			MpscArrayQueue.class.getName());
	configuration.setProperty(WriterController.PREFIX + WriterController.RECORD_QUEUE_SIZE, "1");
	configuration.setProperty(WriterController.PREFIX + WriterController.RECORD_QUEUE_INSERT_BEHAVIOR, "1");
	// setup for TCP test
	configuration.setProperty(ConfigurationKeys.ACTIVATE_TCP_DOMAIN, "kieker.tcp");
	configuration.setProperty(ConfigurationKeys.ACTIVATE_TCP_REMOTE_PORT, port);
	configuration.setProperty(ConfigurationKeys.ACTIVATE_TCP, true);
	configuration.setProperty(SingleSocketTcpWriter.CONFIG_PORT, port);
	configuration.setProperty(SingleSocketTcpWriter.CONFIG_BUFFERSIZE, 65535);
	configuration.setProperty(SingleSocketTcpWriter.CONFIG_FLUSH, true);
	configuration.setProperty(ConfigurationKeys.ACTIVATE_TCP, true);
	configuration.setProperty(ConfigurationKeys.TIMER_CLASSNAME, SystemNanoTimer.class.getName());
	configuration.setProperty(SingleSocketTcpWriter.CONFIG_CONN_TIMEOUT_IN_MS, timeoutInMs);
	@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
	final String address = "127.0.0.1";
	configuration.setProperty(SingleSocketTcpWriter.CONFIG_HOSTNAME, address);

}
 
Example #4
Source File: ThreadedSimpleNBody.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public ThreadedSimpleNBody() {
	int cores = Runtime.getRuntime().availableProcessors();
	maxCuboids = cores * 2;
	maxRunners = cores * 12;
	sharedQueue = new SpmcArrayQueue<NBodyCuboid.CuboidCoordinates>(maxCuboids);
	resultsQueue = new MpscArrayQueue<CuboidRunnerResults>(maxCuboids);
	runnerExecutor = Executors.newFixedThreadPool(maxRunners);
}
 
Example #5
Source File: CuboidRunner.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public CuboidRunner(double dmp, double timeDelta,
	SpmcArrayQueue<CuboidCoordinates> cuboidQueue,
	MpscArrayQueue<CuboidRunnerResults> resultsQueue,
	Collection<NBody3DBody> universe) {
	this.id = UUID.randomUUID();
	this.dmp = dmp;
	this.timeDelta = timeDelta;
	this.cuboidQueue = cuboidQueue;
	this.universe = universe;
	this.resultsQueue = resultsQueue;
}
 
Example #6
Source File: PseudoActorCell.java    From actor4j-core with Apache License 2.0 5 votes vote down vote up
public PseudoActorCell(ActorSystem wrapper, Actor actor, boolean blocking) {
	super(wrapper.system, actor);
	
	if (blocking)
		outerQueueL2 = new LinkedBlockingQueue<>();
	else
		outerQueueL2 = new MpscArrayQueue<>(system.getQueueSize());
	
	outerQueueL1 = new LinkedList<>();
	rxOuterQueueL1 = ActorMessageFlowable.getMessages(outerQueueL1);
}
 
Example #7
Source File: DefaultActorThread.java    From actor4j-core with Apache License 2.0 5 votes vote down vote up
public DefaultActorThread(ThreadGroup group, String name, ActorSystemImpl system) {
	super(group, name, system);
	
	directiveQueue = new MpscArrayQueue<>(system.getQueueSize());
	priorityQueue  = new PriorityBlockingQueue<>(system.getQueueSize());
	serverQueueL2  = new MpscArrayQueue<>(system.getQueueSize());
	serverQueueL1  = new ArrayDeque<>(system.getBufferQueueSize());
	outerQueueL2   = new MpscArrayQueue<>(system.getQueueSize());
	outerQueueL1   = new ArrayDeque<>(system.getBufferQueueSize());
	innerQueue     = new CircularFifoQueue<>(system.getQueueSize());
	
	newMessage = new AtomicBoolean(true);
}
 
Example #8
Source File: WriterControllerTest.java    From kieker with Apache License 2.0 5 votes vote down vote up
/**
 * Check whether blocking of the queue on insert works when queue is full.
 * 
 * @throws Exception on all kind of thread issues
 */
@Test
@SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
// @SuppressFBWarnings("SIC")
public void testBlockOnQueueIsFullInsertBehavior() throws Exception {
	final Configuration configuration = new Configuration();
	configuration.setProperty(ConfigurationKeys.WRITER_CLASSNAME, DumpWriter.class.getName());
	configuration.setProperty(WriterController.PREFIX + WriterController.RECORD_QUEUE_FQN,
			MpscArrayQueue.class.getName());
	configuration.setProperty(WriterController.PREFIX + WriterController.RECORD_QUEUE_SIZE, "1");
	configuration.setProperty(WriterController.PREFIX + WriterController.RECORD_QUEUE_INSERT_BEHAVIOR, "1");

	final WriterController writerController = new WriterController(configuration);

	final Thread thread = new Thread(new Runnable() {
		@Override
		public void run() {
			// the first element fits into the queue
			writerController.newMonitoringRecord(new EmptyRecord());
			// the second element exceeds the queue's capacity and triggers
			// the blocking wait
			writerController.newMonitoringRecord(new EmptyRecord());
		}
	});
	thread.start();

	Await.awaitThreadState(thread, State.WAITING, THREAD_STATE_CHANGE_TIMEOUT_IN_MS);

	writerController.init(); // starts the queue consumer

	Await.awaitThreadState(thread, State.TERMINATED, THREAD_STATE_CHANGE_TIMEOUT_IN_MS);

	writerController.cleanup(); // triggers the termination of the queue
								// consumer

	writerController.waitForTermination(CONTROLLER_TIMEOUT_IN_MS);

	Assert.assertThat(writerController.getStateOfMonitoringWriterThread(), CoreMatchers.is(State.TERMINATED));
}
 
Example #9
Source File: Simulation.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
private StepResults runWithTreahds(StepData stepData) throws InvalidNumberOfCubesException {
		if (maxCuboids == -1) {
			maxCuboids = Runtime.getRuntime().availableProcessors();
			maxRunners = maxCuboids * 2;
			sharedQueue = new SpmcArrayQueue<NBodyCuboid.CuboidCoordinates>(maxCuboids);
			resultsQueue = new MpscArrayQueue<CuboidRunnerResults>(maxCuboids);
			runnerExecutor = Executors.newFixedThreadPool(maxRunners);
		}
		prprDrtn = Duration.ZERO;
		calcAccelDrtn = Duration.ZERO;
		calcVelDrtn = Duration.ZERO;
		calcPosDrtn = Duration.ZERO;
		memSize = 0;
		// x in b=2^x, where b is the next power of 2 greater than a
		int numCuboids = (int) Math.pow(2, 32 - Integer.numberOfLeadingZeros(maxCuboids - 1));
		Collection<CuboidCoordinates> stepCuboids = new StockCuboids(stepData.getCoordiantes()).setupCuboids(numCuboids);
		List<NBody3DBody> newUniverse = new ArrayList<>(stepData.getUniverse().size());
		while (!stepCuboids.isEmpty()) {
			sharedQueue.addAll(stepCuboids);
			List<CuboidRunner> runners = new ArrayList<CuboidRunner>();
			for (int r = 0; r < numCuboids; r++) {
				CuboidRunner runner = new CuboidRunner(0.995, 0.001, sharedQueue, resultsQueue, stepData.getUniverse());
				runners.add(runner);
			}

			List<Future<UUID>> futures = null;
			try {
				futures = runnerExecutor.invokeAll(runners);
			} catch (InterruptedException e1) {
				System.err.println("ex " + e1.getMessage());
			} finally {
				if (futures != null) {
					resultsQueue.drain(r -> {
//						boolean found = checkIds(futures, r);
//						if (!found) {
//							throw new IllegalStateException("Finished thread did not queue results");
//						}
						try {
							prprDrtn = prprDrtn.plus(r.durations().prepareDrtn());
							calcAccelDrtn = calcAccelDrtn.plus(r.durations().calcAccelDrtn());
							calcVelDrtn = calcVelDrtn.plus(r.durations().calcVelDrtn());
							calcPosDrtn = calcPosDrtn.plus(r.durations().calcPosDrtn());
							// Only count if durations are OK
							stepCuboids.remove(r.coordiantes());
							newUniverse.addAll(r.bodies());
							memSize += r.memUsed();
						} catch (RequestedDurationNotFound e) {
							// Retry
							System.err.println("ex " + e.getMessage());
						}
					});
				}
			}
		}
		System.out.println("Simultaion multi-thread finished " + LocalDateTime.now());
		return new StepResults(
				stepData.getCoordiantes(),
				new StockCuboidSimulationDurations(
						prprDrtn.toNanos(),
						calcAccelDrtn.toNanos(),
						calcVelDrtn.toNanos(),
						calcPosDrtn.toNanos()),
				newUniverse,
				memSize);
	}
 
Example #10
Source File: MpscArrayBuffer.java    From caffeine with Apache License 2.0 4 votes vote down vote up
MpscArrayBuffer() {
  queue = new MpscArrayQueue<>(BUFFER_SIZE);
}
 
Example #11
Source File: PlatformDependent.java    From netty-4.1.22 with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new {@link Queue} which is safe to use for multiple producers (different threads) and a single
 * consumer (one thread!) with the given fixes {@code capacity}.
 */
public static <T> Queue<T> newFixedMpscQueue(int capacity) {
    return hasUnsafe() ? new MpscArrayQueue<T>(capacity) : new MpscAtomicArrayQueue<T>(capacity);
}