Java多线程 - 线程的八种创建方式

1. 继承 Thread 类

        重写run(),然后调用 start 方法。

class SampleThread extends Thread {
    //重写run方法,线程运行后,跑的就是run方法 
    public void run(){
       //System.out.println("");
    }
 
    public static void main(String[] args){
       Thread t1 = new SampleThread();
       Thread t2 = new SampleThread();
       t1.start();  //线程运行,调用的 run()方法.
       t2.start(); //线程运行,调用的 run()方法..  
    }
}

2. 实现 Runnable 接口

        实现的 run 方法, 然后再用 Thread 类包裹后,调用 start 方法。

class A implements Runnable{
 
    @Override
    public void run() {
        // implement run method here 
    }
 
    public static void main() {
        final A obj = new A(); 
        Thread t1 = new Thread(new A()); 
        t1.start();
    } 
}

3. 匿名内部类

// Runnable匿名类,实现其run()方法
new Thread(new Runnable() {
    @Override
    public void run() {
         System.out.println(Thread.currentThread().getName() + " is running");
    }
}).start();
// 同上,使用lambda表达式函数式编程
new Thread(()->{
    System.out.println(Thread.currentThread().getName() + " is running");
}).start();

4. 实现Callabe接口

实现 Callable 接口的 call 方法,用 FutureTask 类包裹 Callable 对象。然后再用 Thread 类包裹 FutureTask 类,并调用 start 方法。call() 方法可以有返回值。

class MyCallable implements Callable {
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        return sum;
    }
 
    public static void main(String[] args) throws Exception {
        MyCallable mc = new MyCallable(); //实例化 callable
 
        FutureTask oneTask = new FutureTask(mc); //用FutureTask包裹
        Thread oneThread = new Thread(oneTask); //用Thread包裹
        oneThread.start();
        System.out.print(oneTask.get()); //获取返回值
    }
}

Callable 方法在 Java 8 后,支持拉姆达表达式的写法,可以创建一个 FutureTask 类,语句上不是太罗嗦。 Callable 方式有以下几个优点:

  • 可以捕获线程上的异常。
  • 可以通过 get 方法得到返回值。
  • get 方法阻塞当前线程,直到调用的线程运行结束。
  • 可以取消线程的运行。

下面代码演示了使用 FutureTask 类运行线程,捕获异常的例子:

FutureTask<Integer> task=new FutureTask<Integer>(()->{
	throw new Exception("自定义异常");
});
 
new Thread(task).start();
 
try {
	System.out.println(task.get());
} catch (Exception e) {
	System.out.println(e.getMessage());
}

5. 定时器(java.util.Timer)

使用定时器java.util.Timer可以快速地实现定时任务,TimerTask实际上实现了Runnable接口。

public class CreatingThread05 {
    public static void main(String[] args) {
        Timer timer = new Timer();
        // 每隔1秒执行一次
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " is running");
            }
        }, 0 , 1000);
    }
}

6. 线程池

使用线程池的方式,可以复用线程,节约系统资源。通过创建线程池来创建线程,使用 ExecutorService 的 execute 方法:

public class CreatingThread06 {
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 100; i++) {
            threadPool.execute(()-> System.out.println(Thread.currentThread().getName() + " is running"));
        }
    }
}

7. lambda表达式

使用并行计算的方式,可以提高程序运行的效率,多线程并行执行。

public static void main(String[] args) {
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
    // 串行,打印结果为12345
    list.stream().forEach(System.out::print);
    System.out.println();
    // 并行,打印结果随机,比如35214
    list.parallelStream().forEach(System.out::print);
}

8. Spring异步方法

首先,springboot启动类加上@EnableAsync注解(@EnableAsync是spring支持的,这里方便举例使用springboot)。

@SpringBootApplication
@EnableAsync
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

其次,方法加上@Async注解。

@Service
public class CreatingThread08Service {
    @Async
    public void call() {
        System.out.println(Thread.currentThread().getName() + " is running");
    }
}

然后,测试用例直接跟使用一般的Service方法一模一样。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class CreatingThread08Test {

    @Autowired
    private CreatingThread08Service creatingThread08Service;

    @Test
    public void test() {
        creatingThread08Service.call();
        creatingThread08Service.call();
        creatingThread08Service.call();
        creatingThread08Service.call();
    }
}

总结:

本质上就两种,一种是继承Thread类并重写其run()方法,一种是实现Runnable接口的run()方法

请看下面的例子,同时继承Thread并实现Runnable接口,应该输出什么呢?

public class CreatingThread09 {
    public static void main(String[] args) {
        new Thread(()-> {
            System.out.println("Runnable: " + Thread.currentThread().getName());
        }) {
            @Override
            public void run() {
                System.out.println("Thread: " + getName());
            }
        }.start();
    }
}

说到这里,我们有必要看一下Thread类的源码:

public class Thread implements Runnable {
    // Thread维护了一个Runnable的实例
    private Runnable target;
    public Thread() {
        init(null, null, "Thread-" + nextThreadNum(), 0);
    }
    
    public Thread(Runnable target) {
        init(null, target, "Thread-" + nextThreadNum(), 0);
    }
    
    private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals) {
        // ...
        // 构造方法传进来的Runnable会赋值给target
        this.target = target;
        // ...
    }
    
    @Override
    public void run() {
        // Thread默认的run()方法,如果target不为空,会执行target的run()方法
        if (target != null) {
            target.run();
        }
    }
}

看到这里是不是豁然开朗呢?既然上面的例子同时继承Thread并实现了Runnable接口,根据源码,实际上相当于重写了Thread的run()方法,在Thread的run()方法时实际上跟target都没有关系了。

所以,上面的例子输出结果为Thread: Thread-0,只输出重写Thread的run()方法中的内容。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值