总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
// 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();
ByteBuffer bb = ByteBuffer.allocate(4);
for (int l = 0; true; l++)
{
bb.putInt(0, l);
ringBuffer.publishEvent(LongEventMain::translate, bb);
Thread.sleep(1000);
}
}
}
### 多个消费者
* **串行**
消费者之间必须等待上一个执行完成才能继续
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190306104156471.png)
需求:C1、添加订单号;C2、记录订单(入库);C3、打印订单详情
代码实现:
public class LongEventMain1 {
public static void handleEvent1(TradeEvent event, long sequence, boolean endOfBatch) {
event.setId(UUID.randomUUID().toString());
}
public static void handleEvent2(TradeEvent event, long sequence, boolean endOfBatch) {
System.out.println(String.format("订单号:%s,入库成功!!", event.getId()));
}
public static void handleEvent(TradeEvent event, long sequence, boolean endOfBatch) {
System.out.println(event);
}
public static void translate(TradeEvent event, long sequence, ByteBuffer buffer) {
event.setUserId(buffer.getInt(0));
}
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);
// Connect the handler
disruptor.handleEventsWith(LongEventMain1::handleEvent1)
.handleEventsWith(LongEventMain1::handleEvent2)
.handleEventsWith(LongEventMain1::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();
ByteBuffer bb = ByteBuffer.allocate(8);
for (int l = 0; true; l++) {
bb.putInt(0, l);
ringBuffer.publishEvent(LongEventMain1::translate, bb);
Thread.sleep(1000);
}
}
}
* **并行**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190306112812897.png)
需求:
C1、添加订单状态
C2、添加订单名称
C3、打印订单详情
代码实现:
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.UUID;
public class LongEventMain2 {
private static Random random = new Random();
public static void handleEvent1(TradeEvent event, long sequence, boolean endOfBatch) {
event.setId(UUID.randomUUID().toString());
try {
int i = random.nextInt(10);
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("====ID====");
}
public static void handleEvent2(TradeEvent event, long sequence, boolean endOfBatch) {
event.setName("order:"+event.getUserId());
try {
int i = random.nextInt(10);
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("====Name====");
}
public static void handleEvent(TradeEvent event, long sequence, boolean endOfBatch) {
System.out.println(event);
}
public static void translate(TradeEvent event, long sequence, ByteBuffer buffer) {
event.setUserId(buffer.getInt(0));
}
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);
// Connect the handler
disruptor.handleEventsWith(LongEventMain2::handleEvent1,LongEventMain2::handleEvent2)
.then(LongEventMain2::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();
ByteBuffer bb = ByteBuffer.allocate(8);
for (int l = 0; true; l++) {
bb.putInt(0, l);
ringBuffer.publishEvent(LongEventMain2::translate, bb);
Thread.sleep(1000);
}
}
}
* **并行加串行**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190306113830531.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzkxMDQ1Mw==,size_16,color_FFFFFF,t_70)
需求:
C1、生成订单号
C2、生成订单名称
C3、记录订单号的状态
C4、打印订单详情
代码实现:
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.UUID;
public class LongEventMain3 {
private static Random random = new Random();
public static void handleEvent1(TradeEvent event, long sequence, boolean endOfBatch) {
event.setId(UUID.randomUUID().toString());
try {
int i = random.nextInt(10);
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("====ID====");
}
public static void handleEvent3(TradeEvent event, long sequence, boolean endOfBatch) {
event.setStatus(event.getId().hashCode()%2);
System.out.println("====Status====");
}
public static void handleEvent2(TradeEvent event, long sequence, boolean endOfBatch) {
event.setName("order:"+event.getUserId());
try {
int i = random.nextInt(10);
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("====Name====");
}
public static void handleEvent(TradeEvent event, long sequence, boolean endOfBatch) {
System.out.println(event);
}
public static void translate(TradeEvent event, long sequence, ByteBuffer buffer) {
event.setUserId(buffer.getInt(0));
}
面试结束复盘查漏补缺
每次面试都是检验自己知识与技术实力的一次机会,面试结束后建议大家及时总结复盘,查漏补缺,然后有针对性地进行学习,既能提高下一场面试的成功概率,还能增加自己的技术知识栈储备,可谓是一举两得。
以下最新总结的阿里P6资深Java必考题范围和答案,包含最全MySQL、Redis、Java并发编程等等面试题和答案,用于参考~
重要的事说三遍,关注+关注+关注!
更多笔记分享
.(img-6aF2C961-1715297861456)]