A summary of Java implementation Multithreading method _java

Source: Internet
Author: User
Tags thread class

Three ways to implement multithreading in Java

In Java, there are three ways to implement multithreading. The first method: Inherit the thread class and override the run function. The second method: Implement the Runnable interface and rewrite the run function. The third method is to implement the callable interface and rewrite the call function. This article will explain how to implement multithreading in these three methods through an example. may refer to the need.

(1) Inherit the thread class and rewrite the run function.

Class XX extends thread{public
     void Run () {
      thread.sleep (1000)  /thread sleeps 1000 milliseconds, sleep causes the thread to enter the block state, and frees the resource
    }}

To open a thread:

Object. Start ()//boot thread, run function

(2) Implement Runnable interface, the code is as follows

Class Mythread implements Runnable {
  
  private String name;
  
  Public Mythread (String name) {
    super ();
    this.name = name;
  
  @Override public
  Void Run () {for
    (int i = 0; i < i++) {
      System.out.println ("Thread" +name+ "--->" +i) ;
    }  
  }
}

public class Threaddemo {public

  static void Main (string[] args) {
    mythread a = new Mythread ("a");
    Mythread B = new Mythread ("B");
    Mythread C = new Mythread ("C");
    New Thread (a). Start ();
    New Thread (b). Start ();
    New Thread (c). Start ();
  }
}

(3) Implement callable interface, rewrite call function

Callable are interfaces similar to runnable, classes that implement callable interfaces, and classes that implement runnable are tasks that can be performed by other threads.

There are several differences between callable and runnable:

    1. Callable the method specified is call (), and runnable the method is run ().
    2. A callable task can return a value after execution, whereas a runnable task cannot return a value
    3. The call () method throws an exception, and the run () method cannot throw an exception.
    4. Run the callable task to get a future object, future represents the result of an asynchronous computation. It provides a way to check whether the calculation is complete, to wait for the calculation to complete, and to retrieve the results of the calculation. Through the future object, you can understand the execution of the task, cancel the execution of the task, and get the results of the task execution.

Java Callable code Example:

Class Taskwithresult implements callable<string> {
 private int id;

 public taskwithresult (int id) {
 this.id = ID;
 }

 @Override public
 String called () throws Exception {return ' result of
 Taskwithresult ' + id;
 }
}

public class Callabletest {public
 static void Main (string[] args) throws Interruptedexception,
  executionexception {
 Executorservice exec = Executors.newcachedthreadpool ();
 arraylist<future<string>> results = new arraylist<future<string>> (); Future is equivalent to a container for executor execution results
 (int i = 0; i < i++) {
  results.add exec.submit (New Taskwithresul T (i)));
 for (future<string> fs:results) {
  if (Fs.isdone ()) {
  System.out.println (Fs.get ());
  } else {
  System.out.println ("Future result are not yet complete");
  }
 Exec.shutdown ();
 }

 

Thank you for reading, I hope to help you, thank you for your support for this site!

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.