Program | tips
The Java platform is designed to be a multithreaded environment from the outset. When your main program executes, other tasks such as fragmentation collection and event handling are done in the background.
Essentially, you can think of these jobs as threads. They happen to be system-managed threads, but they are threads anyway. Threads allow you to define separate jobs that do not interfere with each other. The system will swap these jobs into or out of the CPU so that (from the outside) they appear to be running at the same time.
You can also use multiple processes when you need to work with multiple jobs in your program. These processes can be created by yourself, and you can manipulate system threads. You do these multiple job processing, using several different classes or interfaces:
Java.util.timer class
Javax.swing.timer class
Thread class
Runnable interface
For a simple job, which usually needs to be repeated, you can use the Java.util.timer class to tell it to "do it every half second." Note: Most system routines are used in milliseconds. Half a second is 500 milliseconds.
The task you want the timer to implement is defined in the Java.util.timertask instance, where the method that is run contains the task to be performed. These are shown in the Hi class where the string "HI" is repeatedly displayed on the screen until you press ENTER.
Import java.util.*; public class Hi {public static void main (string args); Timer.cancel (); } }
|
The way the Java Runtime Environment Works is if a thread is running, the program does not quit. This way, when the cancellation is invoked and no other thread is running, the program exits. There are some system threads running, such as the fragmentation collector. These system threads are also referred to as background threads. The presence of background threads does not affect the shutdown of the running environment, and only non-background threads guarantee that the running environment is not closed.
The Javax.swing.timer class works the same way as the Java.util.timer class, but there are some differences to note. First, the running job is defined by the implementation of the ActionListener interface. Second, the execution of the job is done inside the event-handling thread, not as the Java.util.timer class is outside it. This is important because it relates to how the swing component set is designed.
If you are unfamiliar with swing, it is a set of graphical components that can be used by Java programs. Swing is called single-threaded by the design process. This means that access to the inner content of the swing class must be done in a single thread. This particular thread is an event-handling thread.
So, for example, if you want to change the text of a label component, you can't just call the JLabel SetText method. Instead, you must make sure that the SetText call takes place in the event-handling thread, which is where the Javax.swing.time class comes in handy.
To illustrate this second scenario, the following program displays the value of an incremented counter. The value of the US half second counter increases and the new values are displayed:
Import javax.swing.*; Import java.awt.*; Import java.awt.event.*; public class Count {public static void main (string args) {final string urlstring = args; Final string message = args; Thread thread1 = new Thread () {public void run () {try {URL url = new URL (urlstring); URLConnection connection = Url.openconnection (); InputStreamReader ISR = new InputStreamReader (Connection.getinputstream ()); BufferedReader reader = new BufferedReader (ISR); int count = 0; while (Reader.read ()!=-1) {count++; } System.out.println ("size is:" + count); Reader.close (); catch (Malformedurlexception e) {system.err.println ("Bad URL:" + urlstring); catch (IOException e) {system.err.println ("I/O problems"); } } }; Thread1.start (); runnable runnable = new runnable () {public void run () {while (true) {System.out.println (message); try {thread.sleep (500); catch (Interruptedexception e) {}}}; Thread thread2 = new Thread (runnable); Thread2.start (); try {System.out.println ("press ENTER to stop"); System.in.read (new byte); catch (IOException e) {System.out.println ("I/O problems"); } system.exit (0); } }
|
Because there are many ways to handle threads, which technology you choose depends on the conditions that you and you face. To be a valid Java programmer, although you typically do not have to learn all the content and core libraries of the Java programming language, threads are an exception. The sooner you learn how threads work and how to use threads, the sooner you learn how Java programs work and interact.