Three ways to create Java Concurrent Programming learning notes------threads

Source: Internet
Author: User

There are several ways to create threads:

1.通过继承Thread类来创建一个线程:

/** * Step 1: Define a subclass that inherits the thread class * Step 2: Construct an object of subclass * Step 3: Start Thread: * */public class Threadtest{public static void Main (string[] args) {//Construct subclass object Subthread subthread = new Subthread ();//Start thread Subthread.start ();}} Define the subclass of the inheritance Thread class Subthread extends thread{public void run () {System.out.println ("Thread test ...");}}

2. Create a thread by implementing the Runnable interface (static proxy mode)

/** * Steps to static proxy mode * Step one: Create a real role * Step two: Create a proxy role, hold a reference to a real role * step three: Implement the same interface **/public class Runnabletest {public static void Ma In (string[] args) {//Create a Class object runnable subrunnable = new subrunnable ();//Create a thread object from runnable thread  subthread = new Thread (subrunnable);//Start thread Subthread.start ();}} Create classes that implement the Runnable interface class Subrunnable implements runnable{@Overridepublic void Run () {System.out.println ("Runnable Test ... ");}

3. Create a thread by implementing the callable interface (can return values, declare exceptions, etc.)

/* Step One: Create an implementation class for the callable interface and override the call () method * Step Two: Get the Future object by executing the dispatch service Executorservice * Step three: Get () method get value * Step four: Shutdownnow () Method Stop Service */public class Callabletest {public static void main (string[] args) {// Get Future object Executorservice Ser = Executors.newfixedthreadpool (2) with the execution scheduling service Executorservice; Race test1 = new Race ("Lilei"); Race test2 = new Race ("Hanmeimei"); future<string> res1 = Ser.submit (test1); future<string> res2 = Ser.submit (test2); the try {//get () method gets the value System.out.println (Res1.get ()); System.out.println (Res2.get ());} catch (Interruptedexception e) {e.printstacktrace ();} catch (Executionexception e) {e.printstacktrace ();} Shutdownnow () Method stop service Ser.shutdownnow ();}} Create the callable interface implementation Class class Race implements Callable<string> {private string name;public Race (String name) {super (); THIS.name = name;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overridepublic String Call () throws Exception {return "Hello" + name + "...";}}

Summarize:

1, the recommended use of interfaces, for the following reasons:

1) Avoid the limitations of single inheritance;

2) easy to share resources

2, two ways to implement the interface (Runnable and callable)区别:

1) callable Start method is call (), runnable Start method is run ()

2) The callable task can return a value after execution, while the runnable task has no return value

3) Callable Call method can throw an exception, Runnable's Run method can not

4) The callable task can get the future object, which provides a way to check whether the calculation is complete, waiting for the completion of the calculation, and retrieving the results of the calculation.

The future object enables you to understand task execution, cancel the execution of a task, and get execution results.

   

Three ways to create Java Concurrent Programming learning notes------threads

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.