`
san_yun
  • 浏览: 2654869 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

RingBufferTester

    博客分类:
  • code
 
阅读更多
package mytest;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.SingleThreadedClaimStrategy;
import com.lmax.disruptor.SleepingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;

public class RingBufferTester {

	private static class SimpleEvent {

	}

	private static class SimpleEventFactory implements EventFactory<SimpleEvent> {
		public SimpleEvent newInstance() {
			return new SimpleEvent();
		}
	}

	public static class SimpleEventHandler implements EventHandler<SimpleEvent> {

		@Override
		public void onEvent(SimpleEvent event, long sequence, boolean endOfBatch) throws Exception {
			System.out.println(event);
			while (true) {
				Thread.sleep(10000);
			}
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat(
				"Action" + "-thread-%d").build();

		EventFactory<SimpleEvent> EVENT_FACTORY = new EventFactory<SimpleEvent>() {
			@Override
			public SimpleEvent newInstance() {
				return new SimpleEvent();
			}
		};

		ExecutorService es = Executors.newCachedThreadPool(namedThreadFactory);

		Disruptor<SimpleEvent> disruptor =
				new Disruptor<SimpleEvent>(EVENT_FACTORY, es,
						new SingleThreadedClaimStrategy(1024 ),
						new SleepingWaitStrategy());

		disruptor.handleEventsWith(new SimpleEventHandler());
		final RingBuffer<SimpleEvent> ringBuffer = disruptor.start();

		Runnable task = new Runnable() {

			@Override
			public void run() {

				while (true) {
					long seq = ringBuffer.next();
					System.out.println(seq);
					ringBuffer.publish(seq);
				}
			}
		};

		Thread t = new Thread(task);
		t.setName("test-1");
		t.start();

//		t = new Thread(task);
//		t.setName("test-2");
//		t.start();
		
//		final RingBuffer<SimpleEvent> ringBuffer = new RingBuffer<SimpleEvent>(SimpleEvent.EVENT_FACTORY,
//				new SingleThreadedClaimStrategy(64), new YieldingWaitStrategy());
//
//		final SimpleEventHandler eventHandler = new SimpleEventHandler();
//		final BatchEventProcessor<SimpleEvent> eventProcessor =
//				new BatchEventProcessor<SimpleEvent>(ringBuffer,
//						ringBuffer.newBarrier(),
//						eventHandler);
//		eventHandler.sesetSequence(eventProcessor.getSequence());
//		ringBuffer.setGatingSequences(eventProcessor.getSequence());

//		ringBuffer.publish(sequence)
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics