Multithreading-through callable implementation (30)

Source: Internet
Author: User
Tags thread class ticket

Video: https://edu.aliyun.com/course/36/learn?spm=5176.8764728.0.0.fVZ5cb#lesson/433

1. Starting with JDK1.5, a new development package has been added: Java.util.concurrent, the development package is primarily for high-performance programming, which provides classes that will be used in high concurrency operations in this development package. Within this package there is a new interface defined:

 Public Interface Callable<v> {    publicthrows  Exception;}

Although the run () method in Runnable is also the main method of the thread, it does not return a value because its design follows the principle of the main method, that is, the thread starts and does not look back. However, there are a number of times when a return value is required, such as when certain threads are executed, which can result in some return, which can only be done through callable. (Description in JDK: Thecallable interface is similar in Runnable that both are designed for classes whose instances might be executed by another thread.) However, Runnable does not return a result and cannot throw a checked exception. )

Example: Defining a thread principal class using callable

class Implements Callable<string>{    @Override    publicthrows  Exception {          for (int x=0;x<20;x++) {                System.out.println ("Sell ticket, X" +x);        }         return "The ticket is sold out";    }}   // this is the principal class of a thread.

public class MyThread222 {
public static void Main (string[] args) {

}
}

In either case, if you want to start multithreading only the Thread class's start () method,

Multithreading-through callable implementation (in)

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.