Java multithreaded programming implementation of the actual thread

Source: Internet
Author: User
Tags current time thread 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.

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.