Java中创建线程的四种方式

1. 继承Thread类

通过继承 Thread 类来创建线程的一般步骤如下:

  1. 定义一个 Thread 类的子类,重写 run() 方法,将相关逻辑实现,run() 方法就是线程要执行的业务逻辑方法;
  2. 创建自定义的线程子类对象;
  3. 调用子类实例的 start() 方法来启动线程。

代码示例:

public class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
public class MyThreadTest {
    public static void main(String[] args) {
        // 创建线程
        MyThread thread = new MyThread();
        // 启动线程
        thread.start();
    }
}

2. 实现 Runnable 接口

通过实现 Runnable 接口创建线程一般步骤如下:

  1. 定义 Runnable 接口实现类 MyRunnable,并重写 run() 方法;
  2. 创建 MyRunnable 实例 runnable,以 runnable 作为 target 创建 Thead 对象,该 Thread 对象才是真正的线程对象;
  3. 调用线程对象的 start() 方法。

代码示例:

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
public class MyRunnableTest {
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        // 创建线程
        Thread thread = new Thread(myRunnable);
        // 启动线程
        thread.start();
    }
}

3. 使用 Callable 和 Future 创建线程

       与 Runnable 接口不一样,Callable 接口提供了一个 call() 方法作为线程执行体,call() 方法比 run() 方法功能要强大,比如:call() 方法可以有返回值、call() 方法可以声明抛出异常。

       Java5 提供了 Future 接口来代表 Callable 接口里 call() 方法的返回值,并且为 Future 接口提供了一个实现类 FutureTask,这个实现类既实现了 Future 接口,还实现了 Runnable 接口,因此可以作为 Thread 类的 target。在 Future 接口里定义了几个公共方法来控制它关联的 Callable 任务。

使用 Callable 和 Future 创建线程的一般步骤如下:

  1. 创建实现 Callable 接口的类 myCallable;
  2. 以 myCallable 为参数创建 FutureTask 对象;
  3. 将 FutureTask 作为参数创建 Thread 对象;
  4. 调用线程对象的 start() 方法。

代码示例:

import java.util.concurrent.Callable;

public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName());
        return 99;
    }
}
public class MyCallableTest {
    public static void main(String[] args) {
        FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
        // 创建线程
        Thread thread = new Thread(futureTask);
        // 启动线程
        thread.start();
        // 结果返回
        try {
            Thread.sleep(1000);
            System.out.println("返回的结果是:" + futureTask.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4.  使用线程池创建线程

Executors 提供了一系列工厂方法用于创先线程池,返回的线程池都实现了ExecutorService 接口。

主要有四种:

  1. newFixedThreadPool
  2. newCachedThreadPool
  3. newSingleThreadExecutor
  4. newScheduledThreadPool

代码示例:

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
public class SingleThreadExecutorTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        MyRunnable myRunnable = new MyRunnable();
        for(int i = 0; i < 10; i++){
            executorService.execute(myRunnable);
        }
        System.out.println("=======任务开始=======");
        executorService.shutdown();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值