1 Callable
package com.xu.thread;
import java.util.concurrent.Callable;
public class MyCallable implements Callable {
@Override
public Object call() throws Exception {
System.out.println("Callable");
return "Hello";
}
}
public static void main(String[] args) throws Exception {
ThreadPoolExecutor executor = new ThreadPoolExecutor(15,
20,
10,
TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(5),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
Future<Object> s1 = executor.submit(new MyCallable());
System.out.println(s1.get());
executor.shutdown();
}
Callable
2 Runnable
package com.xu.thread;
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("Runnable");
}
}
public static void main(String[] args) throws Exception {
new Thread(new MyRunnable()).start();
}
Runnable
3 Thread
public static void main(String[] args) throws Exception {
new Thread(() -> {
System.out.println("Thread");
}).start();
}
Thread