:sparkles:JDK8

This commit is contained in:
小柒2012
2020-04-14 21:53:38 +08:00
parent c04cb62f71
commit 50496458f8
5 changed files with 12 additions and 20 deletions

View File

@@ -12,16 +12,12 @@ import com.lmax.disruptor.dsl.Disruptor;
*/
public class DisruptorUtil {
static Disruptor<SeckillEvent> disruptor = null;
static Disruptor<SeckillEvent> disruptor;
static{
SeckillEventFactory factory = new SeckillEventFactory();
int ringBufferSize = 1024;
ThreadFactory threadFactory = new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return new Thread(runnable);
}
};
disruptor = new Disruptor<SeckillEvent>(factory, ringBufferSize, threadFactory);
ThreadFactory threadFactory = runnable -> new Thread(runnable);
disruptor = new Disruptor<>(factory, ringBufferSize, threadFactory);
disruptor.handleEventsWith(new SeckillEventConsumer());
disruptor.start();
}

View File

@@ -12,7 +12,8 @@ public class SeckillEventConsumer implements EventHandler<SeckillEvent> {
private ISeckillService seckillService = (ISeckillService) SpringUtil.getBean("seckillService");
public void onEvent(SeckillEvent seckillEvent, long seq, boolean bool) throws Exception {
@Override
public void onEvent(SeckillEvent seckillEvent, long seq, boolean bool) {
seckillService.startSeckil(seckillEvent.getSeckillId(), seckillEvent.getUserId());
}
}

View File

@@ -8,7 +8,8 @@ import com.lmax.disruptor.EventFactory;
*/
public class SeckillEventFactory implements EventFactory<SeckillEvent> {
public SeckillEvent newInstance() {
@Override
public SeckillEvent newInstance() {
return new SeckillEvent();
}
}

View File

@@ -16,11 +16,7 @@ public class SeckillEventMain {
public static void producerWithTranslator(){
SeckillEventFactory factory = new SeckillEventFactory();
int ringBufferSize = 1024;
ThreadFactory threadFactory = new ThreadFactory() {
public Thread newThread(Runnable runnable) {
return new Thread(runnable);
}
};
ThreadFactory threadFactory = runnable -> new Thread(runnable);
//创建disruptor
Disruptor<SeckillEvent> disruptor = new Disruptor<SeckillEvent>(factory, ringBufferSize, threadFactory);
//连接消费事件方法

View File

@@ -9,12 +9,10 @@ import com.lmax.disruptor.RingBuffer;
*/
public class SeckillEventProducer {
private final static EventTranslatorVararg<SeckillEvent> translator = new EventTranslatorVararg<SeckillEvent>() {
public void translateTo(SeckillEvent seckillEvent, long seq, Object... objs) {
seckillEvent.setSeckillId((Long) objs[0]);
seckillEvent.setUserId((Long) objs[1]);
}
};
private final static EventTranslatorVararg<SeckillEvent> translator = (seckillEvent, seq, objs) -> {
seckillEvent.setSeckillId((Long) objs[0]);
seckillEvent.setUserId((Long) objs[1]);
};
private final RingBuffer<SeckillEvent> ringBuffer;