1、Thread类
1.1、自定义类继承Thread类,重写run()方法
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println(Thread.currentThread().getName() + "---" + i);
}
}
}
1.2、实例化自定义类,调用start()方法
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName() + "---" + i);
}
}
2、Runnable接口
2.1、自定义类实现Runnable接口,重写run()方法
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 30; i++) {
System.out.println(Thread.currentThread().getName() + "---" + i);
}
}
}
2.2、实例化自定义类,并交给一个thread实例
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable, "myRunnable");
2.3、调用thread的start()方法
thread.start();
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName() + "---" + i);
}
3、线程池
3.1、四种线程池
3.1.1、固定大小线程池FixedThreadPool——池中线程数量固定
3.1.2、缓存线程池CachedThreadPool——池中线程数量由提交的任务数决定
3.1.3、单线程线程池SingleThreadExecutor——只包含一个线程
3.1.4、调度线程池ScheduledThreadPool——定时任务线程池
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
3.2、线程池的使用
3.2.1、实例化线程池对象
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
3.2.2、提交任务到线程池
Runnable runnable = new Runnable() {
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}
};
fixedThreadPool.submit(runnable);
3.2.3、关闭线程池
fixedThreadPool.shutdown();
4、Callable接口
4.1、单独使用
4.1.1、自定义类继承Callable接口,重写call()方法
public class MyCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 101; i++) {
sum += i;
Thread.sleep(100);
}
return sum;
}
}
4.1.2、实例化自定义类并将其转为Task对象
MyCallable myCallable = new MyCallable();
FutureTask<Integer> task = new FutureTask(myCallable);
4.1.3、将task对象交给thread对象
4.1.4、调用thread对象的run()方法
Thread thread = new Thread(task);
thread.start();
try {
Integer sum = task.get();
System.out.println(sum);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
4.2、配合线程池使用
4.2.1、自定义类继承Callable接口,重写call()方法
public class MyCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 101; i++) {
sum += i;
Thread.sleep(100);
}
return sum;
}
}
4.1.2、创建线程池对象
//创建一个线程数为3的固定大小线程池
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
4.1.3、实例化自定义类并将其交给线程池对象
MyCallable myCallable = new MyCallable();
Future<Integer> future = fixedThreadPool.submit(myCallable);
Future<Integer> future2 = fixedThreadPool.submit(myCallable);
4.1.4、获取线程执行的返回结果,关闭线程池
try {
int sum = future.get();
int sum2 = future2.get();
System.out.println(sum + sum2);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
fixedThreadPool.shutdown();