Turn: https: //www.cnblogs.com/selene/p/5972882.html
Use thread exception handler improve system reliability
We want to write a Socket application, listening on the specified port, receive and transmit data packets logic, which is often used to exchange data between the earlier systems, such interfaces typically need to consider two issues: one is to avoid thread blocking, to ensure received data processing as quickly as possible; the second is: the interface of stability and reliability of the data package is very complex, system interface services are many, once the waiting thread can lead to abnormal Socket stop, which is very dangerous, and that we have any way to avoid it?
Java1.5 version was added later setUncaughtExceptionHandler in Thread class method to achieve a thread exception of the capture and processing. May you have a question: If the Socket application whether an abnormality has occurred can automatically restart the unpredictability of it? In fact, using a thread exception handler is easy to solve, we look at an exception handler application example, the following code:
class TcpServer implements Runnable { // 创建后即运行 public TcpServer() { Thread t = new Thread(this); t.setUncaughtExceptionHandler(new TcpServerExceptionHandler()); t.start(); } @Override public void run() { for (int i = 0; i < 3; i++) { try { Thread.sleep(1000); System.out.println("系统正常运行:" + i); }the catch (InterruptedException E) { e.printStackTrace (); } } // thrown the throw new new a RuntimeException (); } // exception handler Private static class TcpServerExceptionHandler the implements the Thread.UncaughtExceptionHandler { @Override public void the uncaughtException (the Thread T, the Throwable E) { // record thread exception information System.out.println ( "thread" + t.getName () + "abnormal, restarts itself, analyze the reasons." ); e.printStackTrace (); // restart the thread, service interruption new new TcpServer (); } } }
This code logic is relatively simple, i.e., when a thread starts TcpServer class is created, the TCP service provided, for example, receive and send documents, specific logic implemented in the run method. At the same time, we set up when the thread appears runtime exception (that is, Uncaught Exception), by the TcpServerExceptionHandler exception handler to handle the exception. So TcpServerExceptionHandler doing it? Two things:
- Record exception information to find the problem
- Re-start a new thread, to provide uninterrupted service
With these two points, TcpServer can be stable operation, even if the abnormal can automatically restart, the client code is relatively simple and requires only new TcpServer () to run the following results:
As can be seen from the results of the operation, when an exception occurs Thread-0, the system automatically restart Thread-1 threads, continue to provide services, greatly improving the performance of the system.
This program is just a sample program, to apply in the real world, you need to pay attention to the following three aspects:
- Shared resource lock: If the reasons for an exception is thread resources are locked, restart the application automatically inform the increased burden on the system, can not provide uninterrupted service. For example, an instant communication service (XMPP Server) appears where the information can not be written, and even then how to start the service, but also can not solve the problem. In this case, the best way is to stop all the threads, the release of resources.
- Dirty data caused by system logic confusion: abnormal interrupt the business logic being executed, especially if you are dealing with a atomic operation (like instant messaging server for user authentication and sign these two events should be handled in a single operation, are not allowed verification is successful, but the situation unsuccessful sign), but if you throw a runtime exception this time is likely to disrupt normal business logic, such as user authentication occurs, but the sign is not successful, in this context restart the application server, although you can provide a service, but it has had on the part of the user logic exception.
- Memory Overflow: thread exception, but the objects created by the thread and will not immediately recover, if we re pro start a new thread, and then create a group of objects, in particular, joined the scene to take over, it is very dangerous, such as instant messaging service re-start a new thread must guarantee the transparency of the original online user, that user will not notice the service to restart, in which case, you need to load a large number of objects in the process initializes the state to ensure that the user's information, but if a thread after a restart , it is likely to cause OutOfMemory memory leaks.