Applications life cycle in Java

Drawing Quick Response Code in Java Applications life cycle
112 Applications life cycle
QR Code 2d Barcode Generator In Java
Using Barcode drawer for Java Control to generate, create QR image in Java applications.
You saw in 10 how the Java system creates a life cycle for applets At key points in the life of an applet, certain methods are guaranteed to be called by the system These key points allow the developer to design an applet which will cooperate with other applets and the Java host system, as well as providing a general model for designing an applet Applications are not required to cooperate with other applications in the same manner This means that if you implement a method that would be called automatically in an applet, you must arrange to have this method called manually in your application Some of the applet methods may not be directly related to the operation of your application but others are more useful, especially the Runnable interface The main() method has already been introduced as the core of an application, and it is from here that methods must be called (although, of course, methods can be called by other methods which were, in turn, called from main) If you wish to define a life cycle for your application, main is the method you should start from Why would you require this kind of execution model After all, when your application is closed by the user the thread of execution is ended Well, while it is true that most applications will not require the kind of rigorous model that the Java system demands of an applet, there is a need to ensure that any external resources are handled cleanly when the application exits For example, if an application connects to a remote server to perform some kind of search, then it is far better, and preferable, to ensure that the server knows that the client has been closed by the user as soon as possible in order to avoid consuming valuable resources by performing a search for a client that is no longer able to take the results Equally, if an application shares information with other applications, or provides some kind of service, then the code should take precautions to ensure that when the thread of execution is terminated the dependent resources are released cleanly The advantage of the model imposed by applets is that developers are required to implement at least the basics of the life cycle in order to have code executed Our advice is to follow a similar model when writing applications 1121 Using threads in an application When using threads in an applet the Java system provides a certain amount of support for starting threads if the applet subclass implements the Runnable interface For example, when your applet has been sent the call to the init() method, a call will be made to the methods that make up the Runnable interface, which will allow you to ensure that your threads are running and stopping when they are supposed to The reason that a Java browser does this is to ensure that threads are killed or suspended when a page of HTML (including applets) is no longer on screen or is reloaded None of this is required with an application after all, once the user has called the application from the user interface or the command line interface the code will be executed until all the intended functions have been completed or the user terminates the execution Therefore, applications wishing to implement threads must make sure that they are executed from the main() method The most effective way of handling threads in an application is to provide separate classes which extend the Thread class and which are created and managed from the main application class A simple example is shown below:
Barcode Drawer In Java
Using Barcode encoder for Java Control to generate, create barcode image in Java applications.
class appThread { public static void main (String args[]) { new myThread("One")start(); new myThread("Two")start(); } } class myThread extends Thread { public myThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { Systemoutprintln("Thread " + getName() + " :" + i); try { sleep(100); } catch (InterruptedException e) { } } } } In this code, the top-level class appThread creates and starts two instances of the myThread class myThread extends the Thread class and so we only have to call the super method to instantiate it and then implement run() in order to provide the code to be executed in the thread By passing a string to myThread (and so to the Thread, by the use of super), we call a constructor which defines a name for the thread Then, in the run() method we determine which thread we are by calling getName() This is one approach; another approach would be to pass a String to myThread, which is then made available to other methods in the class We prefer the method in the example because it maintains a level of consistency with the constructor for the subclass and the superclass If you compile and execute this example, you will see each of the threads counting up from zero to nine, with the output being interleaved as the sleep() statements free up the logical execution stream for the other thread This example illustrates how to start threads As we have already mentioned, you should make some effort to ensure that when threads die or an application exits, any resources such as network connections and files are closed cleanly You can see from the example that threads can be started in the normal manner, and equally threads can be stopped by calling the stop() method If this method has not been overridden, then the default behaviour is to stop the execution of the thread 1122 Dual-purpose code One other reason for implementing methods which define a life cycle for an application is that it is possible to write code that can be executed both as an applet and as a standalone application At the time of writing this book the most compelling reason for doing this is the lack of a stable browser to display applets Other reasons might be to extend cross-platform support to platforms which may have a browser but not a run-time system, or which have a run-time system but no browser However, while the idea of writing code which can be run both as an application and as an applet may be appealing, there are some important differences between applets and applications which should be seriously considered Before discussing these issues it is worth introducing a simple example which can run both as an applet and as an application: import javaappletApplet;
Recognizing Barcode In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
import javaawt*; public class dualCode extends Applet { public static void main (String args[]) { Frame f = new Frame("Dual Applet/Application"); fsetLayout(new FlowLayout()); dualCode dc = new dualCode(); dcinit(); fadd("Center",dc); fresize(100,100); fshow(); } public void init() { setLayout(new FlowLayout()); add(new Button("Button")); } When this code is run as an applet the first method that is called is init() The init method simply defines the layout model for the applet and creates a button However, when the code is executed as an application the first method that is called is main(), as we have described at the start of this chapter In this example, main creates a new top-level Frame, called f The main method sets the layout model and creates a new instance of the example class this is done because main() is static Then, the new instance of the example class, dc, is added to the Frame f The Frame is then resized and displayed So, once the Frame is displayed the code operates in the same manner regardless of whether it is being executed within the hosting environment of a browser or as a standalone application However, while this looks like a perfect solution and makes you wonder why Java even bothers to differentiate between applets and applications, there are some severe constraints on using this approach The problems arise when differences in functionality between applets and applications make the use of the same Java code impossible As an example, the security model imposed on applets by the browser means that opening network connections is very constrained, while an application can open and manage connections freely Therefore, in order to write code which can function as both an applet and an application, it is necessary to write to the lowest common feature set In the case of network connections this means imposing the constraints of the browser security model on applications as well as applets and in the process severely limiting the functionality of the resulting code The next section discusses some of the differences between applications and applets Each can be regarded as an obstacle in writing dual-purpose code However, if you find that the requirements of your project are served by the constrained feature set that allows the generation of dual-purpose code, then using this technique can be an elegant and simple solution to reaching a larger number of users on a wider range of platforms, as well as facilitating development of applet code in the absence of browsers One final word on using dual-purpose code: make sure that you test it both as an applet and as an application before shipping the final product to your users It is easy to fall into the trap of assuming that calls which work for one type of model will work for the other, even though at the back of your mind you are retaining the fact that there are some features and library calls that are not dual-functional
QR Code Encoder In C#.NET
Using Barcode maker for .NET Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
Make QR In VS .NET
Using Barcode printer for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
Quick Response Code Encoder In Visual Basic .NET
Using Barcode maker for Visual Studio .NET Control to generate, create Quick Response Code image in Visual Studio .NET applications.
Code 39 Extended Creator In Java
Using Barcode maker for Java Control to generate, create Code 39 Extended image in Java applications.
Draw Bar Code In Java
Using Barcode creation for Java Control to generate, create barcode image in Java applications.
Drawing USS ITF 2/5 In Java
Using Barcode creator for Java Control to generate, create ITF image in Java applications.
Draw Data Matrix In C#
Using Barcode encoder for .NET framework Control to generate, create DataMatrix image in Visual Studio .NET applications.
Generating Code39 In .NET Framework
Using Barcode creation for Visual Studio .NET Control to generate, create Code 39 image in .NET applications.
Generating Code 39 Full ASCII In Visual C#
Using Barcode maker for .NET Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications.