更多:Java进阶核心知识集
包含:JVM,JAVA集合,网络,JAVA多线程并发,JAVA基础,Spring原理,微服务,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存等等
高效学习视频
public void setName(String name) {
this.name = name;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "TradeEvent{" +
"userId=" + userId +
", id='" + id + '\'' +
", name='" + name + '\'' +
", status=" + status +
", produceId=" + produceId +
'}';
}
}
* **生产者类**
用来生产事件的,可以创建多个实例来进行模拟多生产者的场景
import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer;
public class Producer {
public static void translate(TradeEvent event, long sequence, ByteBuffer buffer) {
event.setUserId(buffer.getInt(0));
event.setProduceId(buffer.getInt(4));
}
private RingBuffer ringBuffer;
private Integer produceId;
public Producer(RingBuffer<TradeEvent> ringBuffer, Integer produceId) {
this.ringBuffer = ringBuffer;
this.produceId = produceId;
}
public void process(){
ByteBuffer bb = ByteBuffer.allocate(8);
for (int l = 1; true; l++) {
bb.putInt(0, l);
bb.putInt(4,produceId);
ringBuffer.publishEvent(Producer::translate, bb);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
* **启动类**
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class LongEventMain4 {
public static void handleEvent(TradeEvent event, long sequence, boolean endOfBatch) {
System.out.println(event);
}
public static void main(String[] args) throws Exception {
// Specify the size of the ring buffer, must be power of 2.
int bufferSize = 1024;
// Construct the Disruptor
Disruptor<TradeEvent> disruptor = new Disruptor<>(TradeEvent::new,
bufferSize,
DaemonThreadFactory.INSTANCE,
ProducerType.MULTI,
new YieldingWaitStrategy());
// Connect the handler
disruptor.handleEventsWith(LongEventMain::handleEvent);
// Start the Disruptor, starts all threads running
disruptor.start();
// Get the ring buffer from the Disruptor to be used for publishing.
RingBuffer<TradeEvent> ringBuffer = disruptor.getRingBuffer();
//模拟多个生产者
最后
针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。
上述的面试题答案都整理成文档笔记。 也还整理了一些面试资料&最新2021收集的一些大厂的面试真题(都整理成文档,小部分截图)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
n.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**