创建线程的三种方法
1、继承Thread类
2、实现Runable接口,没有返回值,不抛异常
3、实现Callable接口,有返回值,会抛异常
public class CallableDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> futureTask = new FutureTask<>(new MyThread());
new Thread(futureTask, "AA").start();
int result01 = 100;
// while (!futureTask.isDone()){
//
// }
int result02 = futureTask.get();//会导致阻塞
System.out.println("result=" + (result01 + result02));
}
}
class MyThread implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("callable come in ...");
return 1024;
}
}
Executors.newCachedThreadPool
private static void cachedThreadPool() {
//不定量线程
ExecutorService threadPool = Executors.newCachedThreadPool();
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName()+"\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
Executors.newSingleThreadExecutor
private static void singleThreadPool() {
//一池1个线程
ExecutorService threadPool = Executors.newSingleThreadExecutor();
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
Executors.newFixedThreadPool
//一池5个线程
ExecutorService threadPool = Executors.newFixedThreadPool(5);
//一般常用try-catch-finally
//模拟10个用户来办理业务,每个用户就是一个线程
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
All
package com.tyred.demo.thread;
import java.time.LocalDate;
import java.util.concurrent.*;
/**
* 第四种使用Java多线程的方式,线程池
*/
public class MyThreadPoolDemo {
public static void main(String[] args) {
System.out.println("Fixed Thread Pool");
fixedThreadPool();
// System.out.println("Single Thread Pool");
// singleThreadPool();
// System.out.println("Cached Thread Pool");
// cachedThreadPool();
// System.out.println("Custom Thread Pool");
// customThreadPool();
// 获取今年的天数
// int daysOfThisYear = LocalDate.now().lengthOfYear();
// System.out.println(daysOfThisYear);
获取指定某年的天数
// System.out.println(LocalDate.of(2012, 1, 1).lengthOfYear());
// System.out.println(LocalDate.MAX);
}
private static void customThreadPool() {
ExecutorService threadPool =
new ThreadPoolExecutor(2,
5,
1L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
private static void cachedThreadPool() {
//不定量线程
ExecutorService threadPool = Executors.newCachedThreadPool();
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
private static void singleThreadPool() {
//一池1个线程
ExecutorService threadPool = Executors.newSingleThreadExecutor();
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
threadPool.shutdown();
}
}
private static void fixedThreadPool() {
//一池5个线程
ExecutorService threadPool = Executors.newFixedThreadPool(5);
//一般常用try-catch-finally
//模拟10个用户来办理业务,每个用户就是一个线程
try {
for (int i = 0; i < 9; i++) {
threadPool.execute(() -> {
System.out.println(Thread.currentThread().getName() + "\t办理业务");
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// threadPool.shutdown();
}
}
}