Most GUI examples show System.exit(0) as the window closing eventhandler. However, System.exit(0) is a bad way to exit applications.The specification says that the VM will exit when all the non-daemonthreads have finished. How can this be implemented so as to avoid thehard exit of System.exit(0)?

The reason most GUI examples use System.exit(0) to terminate theapplication is twofold. First, they are examples, and it is the mostexpedient way to guarantee that the example will exit properly on allJVMs. The second reason is historical. Simply put, System.exit() wasthe only way you could exit a GUI application. Consider thefollowing. If the JVM must exit when all non-daemon threads haveterminated and the AWT thread is made a daemon thread, then yourapplication would exit immediately after main() returned. AWTprograms will never exit if you don’t use System.exit() because theAWT thread is not a daemon thread. This has been a bug in JDKs 1.0.xthrough 1.3. Sun has considered various solutions for fixing it, buthas never adopted one because it would break too many existingapplications. The latest word is that they will try to remedy thesituation in the JDK after 1.3 (Kestrel), code-named Merlin.

I should add that System.exit() is not an entirely unacceptable means ofterminating a program. In JDK 1.1, you could guarantee finalizerswould run before exiting by using System.runFinalizersOnExit(). Thiswas deprecated in JDK 1.2 because of race conditions. JDK 1.3 hasadded Runtime.addShutdownHook(), which allows you to specify cleanupcode that should be run before exiting. The problem, of course, isthat there is no way to clean up that works in JDKs 1.1 through 1.3.I believe this is further evidence that Java needs to be specifiedthrough a formal standards body, which might avoid the pitfalls Sunkeeps on creating for developers.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as