美文网首页
Java线程的正确打开方式

Java线程的正确打开方式

作者: e飞聪天 | 来源:发表于2020-03-07 23:26 被阅读0次

    记得读大学时,老师教我们创建一个Java线程有两种方式,这里我们先来简单温习一下在Java中如何创建一个线程,方便帮助小白扫盲:

    • 通过继承Thread类,然后通过重写父类中的run方法,实现我们的线程业务逻缉,示例代码如下:
    public class MyThread extends Thread{
    
        @Override
        public void run() {
            System.out.println("hello,I'm a thread,my thread id is "+Thread.currentThread().getId());
        }
    }
    
    • 通过实现Runable接口,然后实现Runable接口中的run方法,实现我们的线程业务逻缉,示例代码如下:
    public class MyThread2 implements Runnable{
    
        @Override
        public void run() {
            System.out.println("hello,I'm a thread,my thread id is "+Thread.currentThread().getId());
        }
    }
    

    我们之前使用线程的时候都是使用new Thread的方式来进行线程的实例化创建,但是这样会有一些问题。如:

    1. 每次new Thread新建对象性能差。
    2. 线程缺乏统一管理,可能无限制新建线程,相互之间竞争,及可能占用过多系统资源导致死机或oom。
    3. 缺乏更多功能,如定时执行、定期执行、线程中断。

    那么,有没有更好的方式来创建线程,答案当然有。Java为我们提供了四种线程池来帮助我们规范且更好的使用线程,相比new Thread,线程池的好处有:

    1. 重用存在的线程,减少对象创建、消亡的开销,性能佳。
    2. 可有效控制最大并发线程数,提高系统资源的使用率,同时避免过多资源竞争,避免堵塞。
    3. 提供定时执行、定期执行、单线程、并发数控制等功能。

    下面就让我们来了解一下Java提供的四种线程池,在Java中线程池是通过ExecutorService这个顶级接口来定义,Executors分别提供了四种线程池的创建方法,而四种线程池分别为:

    1. CachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
    2. FixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。
    3. ScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
    4. SingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

    CachedThreadPool示例代码

    ExecutorService executorService = Executors.newCachedThreadPool();
    for(int i=0; i<10; i++){
      final int index = i;
      try {
          Thread.sleep(index * 1000);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
      executorService.execute(()->{
          System.out.println(String.format("my thread id is %d ", Thread.currentThread().getId()));
      });
    }
    

    运行结果如下,可以看到线程池为无限大,当执行第二个任务时第一个任务已经完成,会复用执行第一个任务的线程,而不用每次新建线程。

    my thread id is 13 
    my thread id is 13 
    my thread id is 13 
    ...
    

    FixedThreadPool示例代码

    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    for (int i = 0; i < 10; i++) {
        final int index = i;
        fixedThreadPool.execute(() -> {
            try {
              System.out.println(String.format("my thread id is %d",Thread.currentThread().getId()));
              Thread.sleep(1000);
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
        });
    }
    

    运行结果如下,因为线程池大小为3,每个任务输出线程ID后sleep 1秒,所以每1秒打印3个线程ID。
    定长线程池的大小最好根据系统资源进行设置。如Runtime.getRuntime().availableProcessors()。可参考PreloadDataCache。

    my thread id is 13
    my thread id is 14
    my thread id is 15
    my thread id is 15
    my thread id is 13
    my thread id is 14
    ...
    

    ScheduledThreadPool示例代码
    延迟3秒执行一次

    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
    scheduledThreadPool.schedule(()->{
        System.out.println("thread delay 3 seconds");
    },3, TimeUnit.SECONDS);
    

    延迟1秒后每3秒执行一次

    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
    scheduledThreadPool.scheduleAtFixedRate(()->{
        System.out.println("delay 1 seconds, and excute every 3 seconds");
    }, 1, 3, TimeUnit.SECONDS);
    

    SingleThreadExecutor示例代码

    ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    for (int i = 0; i < 10; i++) {
        final int index = i;
        singleThreadExecutor.execute(()->{
            try {
                System.out.println(String.format("my thread id is %d",Thread.currentThread().getId()));
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
          });
    }
    

    运行结果如下,可以看到每执行一次线程,sleep1秒,只有一个线程来完成任务的执行。

    my thread id is 13
    my thread id is 13
    my thread id is 13
    ...
    

    相关文章

      网友评论

          本文标题:Java线程的正确打开方式

          本文链接:https://www.haomeiwen.com/subject/xrukdhtx.html