Example of how java captures kill signals to process Program Information

Source: Internet
Author: User

In c, the corresponding signal information should be controlled, and simple signal capturing can also be implemented in java;
It can be used as a reference for clearing data when the program is closed,
The procedure is as follows:

Package com. i5a6. signal;
Import sun. misc. Signal;
Import sun. misc. SignalHandler;
Public class TestSignal implements SignalHandler {
 
Public static void main (String [] args) throws InterruptedException {
TestSignal tsh = new TestSignal ();
 
Signal. handle (new Signal ("TERM"), tsh); // kill-15 common kill
Signal. handle (new Signal ("INT"), tsh); // Ctrl + c
// Signal. handle (new Signal ("KILL"), tsh); // kill-9 no Support
// Signal. handle (new Signal ("USR1"), tsh); // kill-10
// Signal. handle (new Signal ("USR2"), tsh); // kill-12
For (;;)
  {
Thread. sleep (3000 );
System. out. println ("running ......");
  }
 }
 
Private void signalCallBack (Signal sn)
 {
System. out. println (sn. getName () + "is recevied .");
 }
 
@ Override
Public void handle (Signal sn ){
SignalCallBack (sn );
 }
}
Compile it in eclipse. After adjusting the access and warning of compiled, compiled the jar package that can be run;
Run: java-jar TS. jar
Then, send the kill command to view the successfully captured signal.
None of the annotated listeners can be registered. Record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.