开启线程四种方式
public class ThreadTest {
public static ExecutorService executorService = Executors.newFixedThreadPool(10);
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
MyCallable myCallable = new MyCallable();
FutureTask<Integer> futureTask = new FutureTask<>(myCallable);
Thread thread1 = new Thread(futureTask);
thread1.start();
executorService.submit(new MyRunnable());
}
public static class MyThread extends Thread{
@Override
public void run() {
System.out.println("开始执行线程:"+ Thread.currentThread().getName());
System.out.println("继承thread");
System.out.println("线程执行结束");
}
}
public static class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("开始执行线程:"+ Thread.currentThread().getName());
System.out.println("实现runnable");
System.out.println("结束线程");
}
}
public static class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
System.out.println("开始执行线程:" + Thread.currentThread().getName());
System.out.println("实现callable");
int a = 10/5;
return a;
}
}
}
四种方式的特点