目录
一:继承Thread
二:重新Runnable接口
三:Callable
四:lambda
一:继承Thread
public static void main(String[] args)
{Thread1 t1=new Thread1();t1.start();
}
class Thread1 extends Thread
{@Overridepublic void run() {System.out.println("1");}
}
二:重新Runnable接口
public static void main(String[] args){
Thread t3=new Thread(new Runnable1());t3.start();
}
class Runnable1 implements Runnable{@Overridepublic void run() {System.out.println("3");}
}
三:Callable
public static void main(String[] args){mycallable call=new mycallable();FutureTask<Integer> ft=new FutureTask<>(call);Thread t4=new Thread(ft);t4.start();System.out.println(ft.get());
}class mycallable implements Callable<Integer> {@Overridepublic Integer call() throws Exception {return 4;}
}
这里可能会有异常记得抛出
四:lambda
Thread t2=new Thread(()->{System.out.println("2");});t2.start();