Inherit from Thread (Java)

Source: Internet
Author: User
Tags garbage collection sleep thread thread class

The easiest way to create a thread is to inherit from the thread class. This class contains everything you need to create and run a thread. The most important method of thread is run (). However, in order to use Run (), it must be overloaded or overwritten so that it can act in full accordance with its own instructions. Therefore, run () belongs to code that will execute "concurrent" or "simultaneous" with other threads in the program.
The following example creates any number of threads and tracks different threads by assigning a unique number (generated by a static variable) to each thread. The thread's run () method is overridden here, and each time a loop is passed, the count completes the loop with a minus 1--count of 0 (when you return to run (), the Threads abort).

: Simplethread.java
//Very Simple Threading Example Public

class Simplethread extends Thread {
  private int Countdown = 5;
  private int threadnumber;
  private static int threadcount = 0;
  Public Simplethread () {
    threadnumber = ++threadcount;
    System.out.println ("making" + Threadnumber);
  }
  public void Run () {while
    (true) {
      System.out.println ("Thread + 
        Threadnumber +" ("+ Countdown +")); 
   if (--countdown = = 0) return
    ;
  }
  public static void Main (string[] args) {for
    (int i = 0; i < 5; i++)
      New Simplethread (). Start ();
    System.out.println ("All Threads Started");
  }
///:~


The run () method almost certainly contains some form of looping-they continue until the thread is no longer needed. Therefore, we must specify conditions to break and exit the loop (or, in the example above, simply return from Run ()). Run () usually takes the form of an infinite loop. That is, it will run forever (until the program completes) by blocking the external issue of a stop () or destroy () call to the thread.
In main (), you can see the creation and running of a large number of threads. Thread contains a special method called Start (), which is a special initialization of the thread, and then calls Run (). So the whole step consists of calling the builder to build the object, then configuring the thread with Start (), and then calling run (). If you do not call Start ()--if appropriate, you can do it in the builder--the thread will never start.
The following is the output of the program running one time (note that each run will be different):

Making 1
making 2
making 3
making 4
making 5
thread 1 (5)
thread 1 (4)
thread 1 (3)
Thread 1 (2)
thread 2 (5)
thread 2 (4)
thread 2 (3)
thread 2 (2) thread 2 (1)
thread 1 (1) All
Threads started
Thread 3 (5)
thread 4 (5)
thread 4 (4)
thread 4 (3)
thread 4 (2) thread 4 (1)
thread 5 (5)
Thread 5 (4)
Thread 5 (3)
thread 5 (2)
thread 5 (1)
thread 3 (4)
thread 3 (3) thread 3 (2)
thread 3 (1)

Note that sleep () is called everywhere in this example, but the output indicates that each thread obtains its own portion of CPU execution time. As you can see, although sleep () relies on the presence of one thread, it is independent of allowing or prohibiting threads. It's just another different way.
You can also see that the threads are not running in the order they were created. In fact, the order of the CPU to process an existing set of threads is indeterminate-unless we intervene in person and adjust their priority with the thread's SetPriority () method.
When main () creates a thread object, it does not capture the handle of any object. Ordinary objects are a "fair game" for garbage collection, but threads are not. Each thread will "register" itself, so there is actually a reference to it somewhere. As a result, the garbage collector would have to "eye-popping" it.

Related Article

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.