Java creates three methods to start Multithreading

Source: Internet
Author: User

Thread creation and startup java uses Thread to represent the Thread. All objects are Thread classes or other instances. The basic steps for starting multithreading using the inherited Tread class are as follows: 1. create a subclass of the Thread class and override the run () method. This method represents the task completed by the Thread. The run method is the thread execution body. 2. Create an instance of the Thread class subclass, that is, the Thread object is created. 3. Call the start () method of the thread to start the thread. [Java] package com. java. xiong. tread16; public class ss extends Thread {// re-run method @ Override public void run () {for (int I = 0; I <100; I ++) {// The current thread System. out. println ("thread name" + I + ":" + this. getName () ;}}// start the main thread main public static void main (String [] arg) {for (int I = 0; I <100; I ++) {// The current thread System. out. println (Thread. currentThread (). getName (); if (I = 10) {// start the first thread new ss (). start (); // start the second thread new ss (). start () ;}}}use the Runnable interface to create and start a thread step 1. define the implementation class of the Runnable interface, and rewrite the run () method of the interface; the run () method is the execution body of the thread 2. create an instance of the Runnable interface implementation Class 3. instantiate the Thread class. The parameter is the object of the Runnable interface implementation class (the object of the Thread class is the ongoing Thread object) to start the Thread [java] package com. java. xiong. tread16; // create the thread object public class RunnableTread implements Runnable {@ Override public void run () {for (int I = 0; I <100; I ++) {// The current thread System. out. println ("Thread name" + I + ":" + Thread. currentThread (). getName () ;}} [java] package com. java. xiong. tread16; public class RunnableTest {public static void main () {RunnableTread runs = new RunnableTread (); // get the Thread object Thread tr = new Thread (runs ); for (int I = 0; I <100; I ++) {// The current thread System. out. println (Thread. currentThread (). getName (); if (I = 10) {// start the first thread tr. start (); // start the second thread tr. start () ;}}}use Callable and Future to create the startup thread 1. create the Callable implementation class and write the call () method. This method is the thread execution body, and this method has a return value. 2. create a Callable instance and use the FutuerTask class to wrap the Callable object. The FutuerTask encapsulates the return value of the call () method of the Callable object. 3. instantiate the FutuerTask class. The parameter is the object of the FutuerTask interface implementation class to start the thread. 4. the get () method of the object of the FutuerTask class is used to obtain the return value after the end of the thread [java] package com. java. xiong. tread16; import java. util. concurrent. callable; import java. util. concurrent. futureTask; public class CallableTest implements Callable <Integer> {@ Override public Integer call () throws Exception {// TODO Auto-generated method stub int I = 0; (; I <100; I ++) {// The current thread System. out. println ("Thread name" + I + ":" + Thread. currentThread ();} return I;} public static void main (String [] args) {CallableTest call = new CallableTest (); futureTask <Integer> fu = new FutureTask <Integer> (call); Thread th = new Thread (fu, "Threads with returned values"); for (int I = 0; I <100; I ++) {// The current thread System. out. println (Thread. currentThread (). getName (); if (I = 10) {// start the first thread th. start () ;}try {System. out. println ("Return Value:" + fu. get ();} catch (Exception e) {e. printStackTrace ();}}}

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.