Java thread Implementation Method

Source: Internet
Author: User

Multithreading has always been a very difficult feeling for everyone. In fact, after understanding it, you will find that it is not as profound as you think. Next, I will share with you my experience in multithreading, next, we will briefly introduce three ways to implement threads: 1. inherit Thread (1) defines the subclass of Thread class and override the run method of this class, the method body represents the task that the thread needs to complete. The run method can be called a thread execution body. (2) create an instance of the Thread subclass, that is, create a Thread object. (3) Call the start method of the thread object to start the thread. Package com. xiaomo. thread; public class ExtendThread extends Thread {private int I; public void run () {// when the Thread inherits the thread class, use this directly to get the getName () of the current Thread // Thread object and return the name of the current Thread // Therefore, you can directly call the getName () method to return the name of the current Thread (; I <100; I ++) {System. out. println (this. getName () + "" + I) ;}} public static void main (String [] args) {for (int I = 0; I <100; I ++) {// call the currentThread () method of Thread to obtain the current Thread System. out. println (Thread. curr EntThread (). getName () + "" + I); if (I = 20) {// create and start the first thread new ExtendThread (). start (); // create and start the second thread new ExtendThread (). result: main 0 main 1.. Main 19 main 20Thread-0 0Thread-0 1.. Thread-0 21Thread-0 22.. Thread-1 98Thread-1 99 inherits the implementation class of the Thread class and does not share the instance attributes. 2. Implement Runnable (1) to define the implementation class of the Runnable interface, and rewrite the run method of this interface. The method body of this method is the thread execution body of this thread. (2) create a Runnable implementation class instance and use this instance as the target of the Thread to create a Thread object. This Thread object is the real Thread object. (3) Call the start method of the thread object to start the thread. Package com. xiaomo. thread; public class ImplRunnable implements Runnable {private int I; public static void main (String [] args) {for (int I = 0; I <100; I ++) {System. out. println (Thread. currentThread (). getName () + "" + I); if (I = 20) {ImplRunnable ir = new ImplRunnable (); new Thread (ir, "Thread 1 "). start (); new Thread (ir, "Thread 2 "). start () ;}}@ Overridepublic void run () {for (; I <100; I ++) {// when the thread implements the Runnable interface // If To obtain the current Thread, you can only use the Thread. currentThread () method System. out. println (Thread. currentThread (). getName () + "" + I) ;}} result: main 0 main 1 main 2... Main 19 main 20 Thread 0 thread 1... Thread 70 thread 2 72 main 46 thread 2 74 thread 73 thread 2 75... Thread 1 79 main 98 main 99 implements Runnable class implementation class sharing instance attributes. 3. Use Callable and Future to create a thread (1) Create the implementation class of the Callable interface and implement the cal method. The call method will be used as the thread execution body, and the call method has a return value. (2) create an instance of the Callable implementation class and use the FutureTask class to wrap the Callable object. The FutureTask object encapsulates the return value of the call method of the Callable object. (3) Use the FutureTask object as the target of the Thread object to create and start a new Thread. (4) Call the get method of the FutureTask object to obtain the returned value after the sub-thread finishes execution. Package com. xiaomo. thread; import java. util. concurrent. callable; import java. util. concurrent. futureTask; public class ImplCallable implements Callable <Integer> {public static void main (String [] args) {// create the Callable object ImplCallable ic = new ImplCallable (); // use FutureTask <Integer> to wrap the Callable object FutureTask <Integer> task = new FutureTask <> (ic); for (int I = 0; I <100; I ++) {System. out. println (Thread. currentT Hread (). getName () + "" + I); if (I = 20) {// you can create and start a new Thread (task, "Threads with returned values "). start () ;}try {// get the thread return value System. out. println ("subthread return value:" + task. get ();} catch (Exception ex) {ex. printStackTrace () ;}// call () method, used as the thread execution body @ Overridepublic Integer call () throws Exception {int I = 0; for (; I <100; I ++) {System. out. println (Thread. currentThread (). getName () + "" + I);} // The call method can return the return I;} result: main 0 m Ain 1.. Main 33 has a returned value Thread 0 has a returned value thread 1 has a returned value thread 2.. Thread 98 with return values 99 main 34 with return values.. Return Value of main 99 sub-thread: 100 this method can obtain the return value.

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.