Practical Java language Multithread Programming essentials of Implementation thread

Source: Internet
Author: User
Tags current time sleep thread class

The Java programming language makes multithreading so simple and effective that some programmers say it's actually natural. Although the use of line turndown in Java is much easier in other languages, there are still some concepts that need to be mastered. One important thing to keep in mind is that the main () function is also a thread and can be used to do useful work. Programmers need to create new threads only if they need multiple threads.

Thread class

The thread class is a concrete class, that is, not an abstract class that encapsulates the behavior of a thread. To create a thread, the programmer must create a new class that is exported from the thread class. The programmer must overwrite the Thread's run () function to do useful work. Instead of calling this function directly, you must call the Thread's start () function, which calls run () again. The following code illustrates its use:

Create two new threads

import java.util.*;
class TimePrinter extends Thread {
  int pauseTime;
  String name;
  public TimePrinter(int x, String n) {
   pauseTime = x;
   name = n;
  }
  public void run() {
   while(true) {
    try {
     System.out.println(name + ":" + new Date(System.currentTimeMillis()));
     Thread.sleep(pauseTime);
    } catch(Exception e) {
     System.out.println(e);
    }
   }
  }
  static public void main(String args[]) {
   TimePrinter tp1 = new TimePrinter(1000, "Fast Guy");
   tp1.start();
   TimePrinter tp2 = new TimePrinter(3000, "Slow Guy");
   tp2.start();
  }
}

In this example, we can see a simple program that displays the current time on the screen at two different intervals (1 seconds and 3 seconds). This is done by creating two new threads, including a total of three threads in main (). However, because classes that are sometimes run as threads may already be part of a class hierarchy, you cannot create threads by this mechanism. Although you can implement any number of interfaces in the same class, the Java programming language allows only one class to have a parent class. At the same time, some programmers avoid exporting from the Thread class because it imposes a class hierarchy. In this case, you need to runnable the interface.

Runnable interface

This interface has only one function, run (), and this function must be implemented by the class that implements this interface. However, in the case of running this class, the semantics are slightly different from the previous example. We can overwrite the previous example with the Runnable interface. (different parts are shown in bold.) )

Create two new threads without imposing class hierarchies

import java.util.*;
class TimePrinter implements Runnable {
  int pauseTime;
  String name;
  public TimePrinter(int x, String n) {
   pauseTime = x;
   name = n;
  }
  public void run() {
   while(true) {
    try {
     System.out.println(name + ":" + new Date(System.currentTimeMillis()));
     Thread.sleep(pauseTime);
    } catch(Exception e) {
     System.out.println(e);
    }
   }
  }
  static public void main(String args[]) {
   Thread t1 = new Thread(new TimePrinter(1000, "Fast Guy"));
   t1.start();
   Thread t2 = new Thread(new TimePrinter(3000, "Slow Guy"));
   t2.start();
  }
}

Note that when you use the Runnable interface, you cannot create the object of the desired class directly and run it, and you must run it from within an instance of the Thread class. Many programmers prefer the runnable interface because inheritance from the Thread class imposes class hierarchies.

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.