Reference URL: http://blog.csdn.net/xyw591238/article/details/51900325
The directory structure is as follows:
Callablemethod class
/** * */package test;import java.util.concurrent.callable;/*** @author lei_chunhui* @date February 1, 2018 PM 8:42:05*//** * @author Leichunhui * * February 1, 2018 */public class Callablemethod implements Callable<string> ; {@Overridepublic String call () throws Exception {System.out.println ("callable Way to create thread"); return null;}}
Runnablemethod class:
/** * */package test;/*** @author lei_chunhui* @date February 1, 2018 afternoon 8:27:12*//** * @author Leichunhui * * February 1, 2018 */public class Runnablemethod implements Runnable {@Overridepublic void run () {System.out.println (" Runnable Way to create Threads ");}}
Threadmethod class
/** * */package test;/*** @author lei_chunhui* @date February 1, 2018 afternoon 8:54:03*//** * @author Leichunhui * *< c8/>2018 February 1 */public class Threadmethod extends thread {public void run () {System.out.println ("Inherit thread method Creation threads");}}
Test class
/** * */package test;import java.util.concurrent.callable;import java.util.concurrent.futuretask;/*** @author lei_ chunhui* @date February 1, 2018 PM 8:29:56*//** * @author Leichunhui * * February 1, 2018 */public class Test {/** * * Description: TODO * Created by: Leichunhui * Date Created: February 1, 2018 * Modified by: Leichunhui * Modified: February 1, 2018 * Modified Note: * Method Name: main* return type: void * @param args * @ver sion * */public static void Main (string[] args) {//Overrides the Runnable interface's Run method implementation System.out.println ("Runnable mode creation thread Start"); Runnablemethod runnablemethod1=new Runnablemethod (); Runnablemethod runnablemethod2=new Runnablemethod (); Thread thread1=new thread (RUNNABLEMETHOD1); Thread thread2=new thread (RUNNABLEMETHOD2); Thread1.start (); Thread2.start (); System.out.println ("Thread1 's thread name is:" +thread1.getname () + "thread ID:" +thread1.getid ()); System.out.println ("Thread2 's thread name is:" +thread2.getname () + "thread ID:" +thread2.getid ()); System.out.println ("runnable way to create thread end"); SYSTEM.OUT.PRINTLN ("Callable mode creation thread Start"); Callablemethod callablemethod3=new Callablemethod (); CallablemeThod callablemethod4=new Callablemethod (); Futuretask<string> futuretask3=new futuretask<> (CALLABLEMETHOD3); Futuretask<string> futuretask4=new futuretask<> (CALLABLEMETHOD4); Thread thread3=new thread (FUTURETASK3); Thread thread4=new thread (FUTURETASK4); Thread3.start (); Thread4.start (); System.out.println ("Thread3 's thread name is:" +thread3.getname () + "thread ID:" +thread3.getid ()); System.out.println ("Thread4 's thread name is:" +thread4.getname () + "thread ID:" +thread4.getid ()); System.out.println ("Callable way to create thread end"); SYSTEM.OUT.PRINTLN ("Thread inheritance Method creation threads start"); Threadmethod thread5=new Threadmethod (); Threadmethod thread6=new Threadmethod (); Thread5.start (); Thread6.start (); System.out.println ("Thread5 's thread name is:" +thread5.getname () + "thread ID:" +thread5.getid ()); System.out.println ("Thread6 's thread name is:" +thread6.getname () + "thread ID:" +thread6.getid ()); SYSTEM.OUT.PRINTLN ("Thread inheritance Method creation threads End");}}
Results:
Runnable way to create thread start
Runnable way to create a thread
The thread name for THREAD1 is: Thread-0 thread ID: 10
The thread name for thread2 is: Thread-1 thread ID: 11
Runnable way to create thread end
Callable way to create thread start
Runnable way to create a thread
The thread name for THREAD3 is: Thread-2 thread ID: 12
The thread name for THREAD4 is: Thread-3 thread ID: 13
Callable way to create a thread
Callable way to create a thread
Callable way to create thread end
Thread inheritance mode Create threads start
The thread name for THREAD5 is: Thread-4 thread ID: 14
Inherit thread mode to create threads
Inherit thread mode to create threads
The thread name for THREAD6 is: Thread-5 thread ID: 15
Thread inheritance way to create threads end
Callable,runnable and thread three ways of creating threads