The cause of the bug
When sending messages, this interface needs to be rewritten. If the rewritten method throws an exception and does not capture it, the program will be stuck and will not execute the
Destroy source code
package com.lmax.disruptor;
public interface EventTranslatorVararg<T> {
void translateTo(T var1, long var2, Object... var4);
}
Problem code
public class Main {
public static void main(String[] args) {
Disruptor<Message> disruptor = new Disruptor<>(
Message::new,
1024,
(ThreadFactory) Thread::new);
disruptor.handleEventsWith((EventHandler<Message>) (message, l, b) -> {
System.out.println("Handling messages " + message);
});
disruptor.start();
RingBuffer<Message> ringBuffer = disruptor.getRingBuffer();
for(int i = 0; i<10; i++){
Message message = new Message(String.valueOf(i));
ringBuffer.publishEvent((m, l) ->
throw new RuntimeException();
);// Throwing exceptions without catching and finding that the program cannot be terminated
}
System.out.println("hi"); //hi Will not output
disruptor.shutdown();//shutdown the disruptor, the method will block until all events have been handled.
}
}
result:
The main thread exits, but the program continues to run without stopping
solve:
Handle exceptions on call
public class Main {
public static void main(String[] args) {
Disruptor<Message> disruptor = new Disruptor<>(
Message::new,
1024,
(ThreadFactory) Thread::new);
disruptor.handleEventsWith((EventHandler<Message>) (message, l, b) -> {
System.out.println("Handling messages " + message);
});
disruptor.start();
RingBuffer<Message> ringBuffer = disruptor.getRingBuffer();
for(int i = 0; i<10; i++){
Message message = new Message(String.valueOf(i));
ringBuffer.publishEvent((m, l, m2) -> {
try {
throw new RuntimeException();
}catch (Exception e){
e.printStackTrace();
}
});// Handle exceptions, find program print exceptions and can end
}
System.out.println("hi");
disruptor.shutdown();//Close the disruptor and the method will block until all events have been processed.
}
}
Message.java
public class Message {
String id;
public Message(String id) {
this.id = id;
}
public Message() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Message{" +
"id='" + id + '\'' +
'}';
}
}
Read More:
- BUG:soft lockup – CPU#0 stuck for 67s! [migration/0:5]
- close failed in file object destructor: IOError: [Error 10] No child processes
- Someone is doing something abnormal
- ERROR 0210: Stuck Key 36
- Solution to abnormal errors during startup of Python’s Anaconda
- Doris decommission be node stuck [How to Solve]
- appear org.springframework.beans . factory.BeanCreationException Causes and solutions of abnormal
- Socket Error 104 bug
- Eclipse gets stuck saving copy and paste
- Abnormal reading after stm32f4 RTC time setting
- Stuck solution when executing router doctor
- Abnormal display of page object moved to here
- Oracle Net Configuration Assistant failed abnormal solution
- A first chance exception of type ‘ System.NullReferenceException ‘when occurred, you did encounter a bug
- [Linux] [kernel] bug: scheduling while atomic problem analysis
- MySQL driver compiling method of QT under windows and solutions to abnormal errors
- Fatal error in GC getthreadcontext failed bug exception.
- Android is Stuck in Download maven-metadata.xml [How to Solve]
- A little bug of CSDN blog
- Http Error 12057 (Bug Fix Note)