Java多线程创建的四种方法

创建线程有四种方式:

1、继承 Thread 类
2、实现 Runnable 接口
3、实现 Callable 接口
4、使用 Executors 工具类创建线程池

一、继承 Thread 类

步骤
1. 定义一个Thread类的子类,重写run方法,将相关逻辑实现,run()方法就是线程要执行的业务逻辑方法
2. 创建自定义的线程子类对象
3. 调用子类实例的star()方法来启动线程
public class MyThread extends Thread {
 @Override
 public void run() {
 System.out.println(Thread.currentThread().getName() + " run()方法正在执...");
 }
 }
public class TheadTest {
public static void main(String[] args) {
 MyThread myThread = newMyThread();
 myThread.start();
 System.out.println(Thread.currentThread().getName() + " main()方法执行结束");
 }
 }

二、实现 Runnable 接口

步骤
1. 定义Runnable接口实现类MyRunnable,并重写run()方法
2. 创建MyRunnable实例myRunnable,以myRunnable作为target创建Thead对象,该Thread对象才是真正的线程对象
3. 调用线程对象的start()方法
 public class MyRunnable implements Runnable {
 @Override
 public void run() {
    System.out.println(Thread.currentThread().getName() + " run()方法执行中...");
 }
 }
public class RunnableTest {
 public static void main(String[] args) {
     MyRunnable myRunnable = new MyRunnable();
     Thread thread = new Thread(myRunnable);
     thread.start();
     System.out.println(Thread.currentThread().getName() + " main()方法执行完成");
 }
 }

三、实现 Callable 接口

步骤
1. 创建实现Callable接口的类myCallable
2. 以myCallable为参数创建FutureTask对象
3. 将FutureTask作为参数创建Thread对象
4. 调用线程对象的start()方法
 public class MyCallable implements Callable<Integer> {
 @Override
     public Integer call() {
        System.out.println(Thread.currentThread().getName() + " call()方法执行中...");
     return 1;
     }
 }
public class CallableTest {
 public static void main(String[] args) {
     FutureTask<Integer> futureTask = new FutureTask<Integer>(newMyCallable());
     Thread thread = new Thread(futureTask);
     thread.start();
 try {
     Thread.sleep(1000);
     System.out.println("返回结果 " + futureTask.get());
     } catch (InterruptedException e) {
     e.printStackTrace();
     } catch (ExecutionException e) {
     e.printStackTrace();
     }
     System.out.println(Thread.currentThread().getName() + " main()方法执行完成");
     }
 }
四、使用 Executors 工具类创建线程池
        Executors提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService接口。
        主要有newFixedThreadPool、newCachedThreadPool、newSingleThreadExecutor,newScheduledThreadPool。
public class MyRunnable implements Runnable {
  @Override
      public void run() {
          System.out.println(Thread.currentThread().getName() + " run()方法执行中...");
      }
  }
 public class SingleThreadExecutorTest {
      public static void main(String[] args) {
            ExecutorService executorService = Executors.newSingleThreadExecutor();
            MyRunnable runnableTest = new MyRunnable();
            for (int i = 0; i < 5; i++) {
                  executorService.execute(runnableTest);
              }
             System.out.println("线程任务开始执行");
             executorService.shutdown();
         }
 }

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值