package JavaBasic; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.FutureTask; /** * @Classname ThreadTest * @Description 多线程的三种实现方式 * @Date 19-7-22 上午9:30 * @Created by mao */ class MyThread1 extends Thread{ @Override public void run(){ System.out.println(Thread.currentThread()+"thread by extends Thread"); } } class MyThread2 implements Runnable{ @Override public void run(){ System.out.println(Thread.currentThread()+"thread by implements Runnable"); } } class MyThread3 implements Callable{ @Override public String call() throws Exception{ return Thread.currentThread()+"thread by implements Callable"; } } public class ThreadTest { public static void main(String[] args){ System.out.println(Thread.currentThread()); MyThread1 myThread1=new MyThread1(); myThread1.start(); MyThread2 myThread2=new MyThread2(); new Thread(myThread2).start(); FutureTask futureTask=new FutureTask<>(new MyThread3()); new Thread(futureTask).start(); try { String res=futureTask.get(); System.out.println(res); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } new Thread(()->{ System.out.println(Thread.currentThread()+"thread by lambda"); }).start(); new Thread() { public void run() { System.out.println(Thread.currentThread()+"thread by 1"); } }.start(); Runnable r = new Runnable() {//创建方式2 public void run() { System.out.println(Thread.currentThread()+"thread by 2"); } }; new Thread(r).start(); } }