Merging threads with main classes

Source: Internet
Author: User
Tags sleep thread thread class

In the above example, we see that thread classes (threads) are separated from the main class (main) of the program. This is very reasonable and easy to understand. However, there is another way that is often used. Although it is not clear, it is generally more concise (which explains why it is so popular). By turning the main program class into a thread, this form merges the main program class with the thread class. Because the main program class must inherit from a frame or applet for a GUI program, additional functionality must be added with an interface. This interface is called Runnable, which contains the basic methods that are consistent with thread. In fact, thread also implements Runnable, which only indicates that there is a run () method.
the merged program/thread says its usage is not very clear. When we start the program, we create a runnable (running) object, but we do not start the thread ourselves. The start of the thread must be done explicitly. The following program shows us this, which reproduces the functionality of the Counter2:
 

: Counter3.java//Using The Runnable interface to turn the//main class into a thread.
Import java.awt.*;
Import java.awt.event.*;

Import java.applet.*;
  public class Counter3 extends Applet implements Runnable {private int count = 0;
  Private Boolean runflag = true;
  Private Thread selfthread = null;
  Private button onoff = New button ("Toggle"), start = New button ("Start");
  Private TextField t = new TextField (10);
    public void Init () {Add (t);
    Start.addactionlistener (New Startl ());
    Add (start);
    Onoff.addactionlistener (New Onoffl ());
  Add (OnOff);
      public void Run () {while (true) {try {selfthread.sleep (100);
    catch (Interruptedexception e) {} if (Runflag) T.settext (integer.tostring (count++)); Class Startl implements ActionListener {public void actionperformed (ActionEvent e) {if (Selfthread = n
        ull) {selfthread = new Thread (counter3.this); Selfthread.start (); Class ONOFFL implements ActionListener {public void actionperformed (ActionEvent e) {runflag =!r
    Unflag;
    } public static void Main (string[] args) {Counter3 applet = new Counter3 ();
    Frame aframe = new Frame ("Counter3"); Aframe.addwindowlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {System.
        Exit (0);
    }
      });
    Aframe.add (applet, borderlayout.center);
    Aframe.setsize (300,200);
    Applet.init ();
    Applet.start ();
  Aframe.setvisible (TRUE); }
} ///:~

Now run () is in the class, but it is still in the "sleep" state after Init () ends. If you press the Start button, the thread will be created with some ambiguous expressions (if the thread does not already exist):
New Thread (Counter3.this);
If something has a runnable interface, it simply means that it has a run () method, but there is no special thing associated with it-it does not have any innate threading power, which is different from the class that inherits from Thread. So in order to generate a thread from a Runnable object, you must create a separate thread and pass the Runnable object for it, and you can use a special builder for it and use a runnable as your own argument. You can then call Start () for that thread, as follows:
Selfthread.start ();
Its role is to perform a general initialization operation and then invoke run ().
One of the great advantages of the Runnable interface is that everything is subordinate to the same class. If you need to access something, simply access it, without having to involve a separate object. But there's a price to pay for this convenience--you can only run a single thread for that particular object (although you can create multiple objects of that type, or create other objects in different classes).
Note that the runnable interface itself is not the culprit for this limitation. It is caused by the merging of runnable with our main class because each application can only be one object of the main class.

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.