Three ways to implement Java multithreading

Source: Internet
Author: User

Java Multi-threaded implementation of the main three kinds: inherit the thread class, implement Runnable interface, use Executorservice, callable, the future implementation has the result of multithreading. There are no return values for the first two methods after the thread has finished executing, only the last one with the return value.

1. Inheriting the thread class for multithreading

Methods for inheriting the thread class although I am listed as a multithreaded implementation, the only way to start a thread is through the start () instance method of the thread class. The start () method is a native method that starts a new thread and executes the run () method. This way of implementing multithreading is simple, by extend thread directly through your own class, and by copying the run () method, you can start a new thread and execute your own defined run () method. Here's an example where two threads are using the start () method:

public class Test_thread{public static void Main (string[] args) {Thread T1 = new Runner1 (); Thread t2 = new Runner2 (); T1.start (); T2.start ();    } }class Runner1 extends thread{public void Run () {for (int i=0;i<10;i++) {System.out.println ("1th Thread: No" +i);}} Class Runner2 extends Thread{public void Run () {for (int i=10;i>0;i--) {System.out.println ("2nd Thread: No" +i);}}}

2. Implement runnable interface mode to realize multithreading

If your class already extends another class, you cannot directly extends the Thread, at which point you must implement a runnable interface, as follows: 

public class Test_runnable {public static void main (string[] args) {Runner1 run1=new Runner1 (); Runner2 run2=new Runner2 (); Thread Th1 = new Thread (RUN1); Thread Th2 = new Thread (run2); Th1.start (); Th2.start ();}} Class Runner1 implements Runnable{public void Run () {for (int i=10;i>=0;i--) {System.out.println ("First thread No:" +i);}}} Class Runner2 implements Runnable{public void Run () {for (int i=0;i<=10;i++) {System.out.println ("Second thread No:" +i);}}}

3. Use Executorservice, callable, and future to implement multi-threading with return results

Executorservice, callable, future This object is actually a function class in the executor framework. You want to learn more about the Executor framework's accessible http://www.javaeye.com/topic/366591.

The thread that returns the result is a new feature introduced in JDK1.5. A task with a return value must implement the callable interface, and similarly, a task with no return value must implement the Runnable interface.

After performing the callable task, you can get a future object, call get on the object to get to the callable task returned object, and combined with the thread pool interface Executorservice can realize the legend has the return result of multithreading.

Import Java.util.concurrent.callable;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class Test_callable implements callable {private int num;public test_callable (  int num) {super (); this.num=num;} public static void Main (string[] args) {executorservice exec =executors.newcachedthreadpool ();// Call Interface Implementation Thread Exec.submit (new test_callable (1)), Exec.submit (New Test_callable (2));//Call thread pool run Runable interface} public Object Call () throws exception{for (int i=0;i<10;i++) {System.out.println ("+this.num+" Thread: No "+i);} return null;}}

Prior to Java development version JDK1.5, there were only the first two implementations on the thread, but the JDK1.5 was later added with callable to implement multithreading with the return.

Three ways to implement Java multithreading

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.