一、继承Thread类,重写run方法
public class MyThread{
public static void main(String[] args) {
Thread threadDome = new ThreadDome();
threadDome.start();
}
}
class ThreadDome extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("i = " + i);
}
}
}
二、实现Runnable接口,重写run方法
public class MyThread{
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
new Thread(myRunnable).run();
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("i = " + i);
}
}
}
三、实现Callable接口,重写call方法
public class MyThread{
public static void main(String[] args) throws ExecutionException, InterruptedException
{
MyCallable myCallable = new MyCallable();
FutureTask futureTask = new FutureTask(myCallable);
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
}
class MyCallable implements Callable<Integer>{
@Override
public Integer call() throws Exception {
return 10;
}
}
callable相较于runnable接口,可以拥有返回值,并且runnable中程序出现异常只能通过try...catch进行捕获然后处理,callable可以将异常抛出。
四、使用线程池
1、线程池的实现:
1、FixedThreadPool(定长线程池):只有核心线程,线程数量固定,执行完立即回收,任务队列为链表结果的有界队列,控制线程的最大并发数
@Test
public void test() throws ExecutionException, InterruptedException {
//创建线程池
ExecutorService pool = Executors.newFixedThreadPool(5);
//使用线程池
for (int i = 0; i < 5; i++) {
Future<Integer> submit = pool.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 10;
}
});
System.out.println(submit.get());
}
//关闭线程池
pool.shutdown();
}
2、ScheduledThreadPool(定时线程池):核心线程数量固定,非核心线程数量无限,执行完闲置10s后回收,任务队列为延时阻塞队列,执行定时或周期性任务
@Test
public void test02() throws ExecutionException, InterruptedException {
//创建线程池
ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
//使用线程池
for (int i = 0; i < 5; i++) {
ScheduledFuture<Integer> schedule = pool.schedule(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 10;
}
}, 1, TimeUnit.SECONDS);
System.out.println(schedule.get());
}
//销毁线程池
pool.shutdown();
}
3、CachedThreadPool(可缓存线程池):无核心线程,非核心线程数量无限,执行完闲置60s回收,任务队列为不存储元素的阻塞队列,执行大量,耗时少的任务
@Test
public void test03() throws ExecutionException, InterruptedException {
//创建线程池
ExecutorService pool = Executors.newCachedThreadPool();
//使用线程池
for (int i = 0; i < 5; i++) {
Future<Integer> submit = pool.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 10;
}
});
System.out.println(submit.get());
}
//关闭线程池
pool.shutdown();
}
4、SingleThreadExecutor(单线程化线程池):只有一个核心线程,无非核心线程,执行完立即回收,任务队列为链表结果的有界队列,不适合做并发但可能引起io阻塞及影响ui线程响应的操作,如数据库操作、文件操作等
@Test
public void test04() throws ExecutionException, InterruptedException {
//创建线程池
ExecutorService pool = Executors.newSingleThreadExecutor();
//使用线程池
for (int i = 0; i < 5; i++) {
Future<Integer> submit = pool.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 10;
}
});
System.out.println(submit.get());
}
//关闭线程池
pool.shutdown();
}
2、excute和submit的区别
excute:只能提交Runable类型任务,会直接抛出异常,可以用try、catch来捕获,和普通线程处理方式一致,excute没有返回值
submit:能提交Runable类型任务也能提交Callable类型任务,会吃掉异常,可通过future的get方法将任务执行时的异常重新抛出,submit有返回值,需要返回值的时候必须使用submit
submit最后的任务也是抛给excute方法,提交任务不需要一个结果的话,直接用excute()提高性能