通过实现Runnable接口来实现

  1. public class T01_CreateThread {
  2. // 创建线程的三种方式
  3. public static void main(String[] args) {
  4. // 通过实现Runnable接口来创建Thread线程
  5. // 1、写一个类实现Runnable 接口
  6. // 2、创建一个对象
  7. RunnableMe runnableMe = new RunnableMe();
  8. // 3、由runnable 创建一个thread 对象
  9. // 4、通过调用start 方法来启动一个线程
  10. new Thread(runnableMe, " i am runnable thread").start();
  11. // 注意坑,此只是调用了run方法并不是启动了一个线程,通过查看 线程name ,发现打印的是 主线程的名字 main
  12. runnableMe.run();
  13. System.out.println("main: " + Thread.currentThread().getName());
  14. }
  15. // 实现 runnable 接口
  16. private static class RunnableMe implements Runnable {
  17. @Override
  18. public void run() {
  19. System.out.println("i am thread runnable " + Thread.currentThread().getName());
  20. }
  21. }
  22. }
  23. 输出结果:
  24. i am thread runnable main
  25. i am thread runnable i am runnable thread
  26. main: main

通过实现Callable接口来实现

  1. public class T01_CreateThread {
  2. // 创建线程的三种方式
  3. public static void main(String[] args) {
  4. //
  5. // 通过实现Callable 接口来实现
  6. // 由Callable<Integer>创建一个FutureTask<Integer>对象:
  7. FutureTask<Integer> oneTask = new FutureTask<Integer>(new CallableMe());
  8. // FutureTask<Integer>是一个包装器,它通过接受Callable<Integer>来创建,它同时实现了Future和Runnable接口。
  9. // 由FutureTask<Integer>创建一个Thread对象:
  10. new Thread(oneTask, "i am FutureTask").start();
  11. System.out.println("main: " + Thread.currentThread().getName());
  12. }
  13. private static class CallableMe implements Callable {
  14. @Override
  15. public Object call() throws Exception {
  16. System.out.println("i am thread CallableMe " + Thread.currentThread().getName());
  17. return 0;
  18. }
  19. }
  20. }
  21. 输出结果:
  22. main: main
  23. i am thread CallableMe i am FutureTask

继承Thread 类实现创建线程

  1. public class T01_CreateThread {
  2. // 创建线程的三种方式
  3. public static void main(String[] args) {
  4. // 1 通过继承 thread 来实现
  5. ThreadMe threadMe = new ThreadMe(" i am extends thread");
  6. // 启动线程
  7. threadMe.start(); //
  8. }
  9. // 继承 thread
  10. private static class ThreadMe extends Thread {
  11. public ThreadMe(String name) {
  12. super(name);
  13. }
  14. @Override
  15. public void run() {
  16. System.out.println("i am thread extends thread " + Thread.currentThread().getName());
  17. }
  18. }
  19. }
  20. 输出结果:
  21. i am thread extends thread i am extends thread

通过Executors 框架来进行创建( 阿里规约明确规定不允许使用此框架,要求手动创建线程池,目的是明确每个 参数的作用)

  1. ExecutorService executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE,
  2. 60L, TimeUnit.SECONDS,
  3. new SynchronousQueue<Runnable>(),Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
  4. 参数说明
  5. // 1、corePoolSize, 指定了线程池里的线程数量,核心线程池大小
  6. // 2、maximumPoolSize, 指定了线程池里的最大线程数量
  7. // 3、keepAliveTime, 当线程池线程数量大于corePoolSize时候,多出来的空闲线程,多长时间会被销毁。
  8. // 4、unit 时间单位。TimeUnit
  9. // 5、workQueue 任务队列,用于存放提交但是尚未被执行的任务。我们可以选择如下几种:
  10. // ArrayBlockingQueue:基于数组结构的有界阻塞队列,FIFO。
  11. // LinkedBlockingQueue:基于链表结构的有界阻塞队列,FIFO。
  12. // SynchronousQueue:不存储元素的阻塞队列,每个插入操作都必须等待一个移出操作,反之亦然。
  13. // PriorityBlockingQueue:具有优先级别的阻塞队列。
  14. // 6、threadFactory 线程工厂,用于创建线程,一般可以用默认的
  15. // 7、handler 拒绝策略,所谓拒绝策略,是指将任务添加到线程池中时,线程池拒绝该任务所采取的相应策略。
  16. // 当向线程池中提交任务时,如果此时线程池中的线程已经饱和了,而且阻塞队列也已经满了,则线程池会选择一种拒绝策略来处理该任务,该任务会交给RejectedExecutionHandler 处理。
  17. // 线程池提供了四种拒绝策略:
  18. // AbortPolicy:直接抛出异常,默认策略;
  19. // CallerRunsPolicy:用调用者所在的线程来执行任务;
  20. // DiscardOldestPolicy:丢弃阻塞队列中靠最前的任务,并执行当前任务;
  21. // DiscardPolicy:直接丢弃任务;