当前位置: 移动技术网 > IT编程>开发语言>Java > Java编程中线程池的基本概念和使用

Java编程中线程池的基本概念和使用

2019年07月22日  | 移动技术网IT编程  | 我要评论

1 引入线程池的原因
  由于线程的生命周期中包括创建、就绪、运行、阻塞、销毁阶段,当我们待处理的任务数目较小时,我们可以自己创建几个线程来处理相应的任务,但当有大量的任务时,由于创建、销毁线程需要很大的开销,运用线程池这些问题就大大的缓解了。

2 线程池的使用
  我们只需要运用executors类给我们提供的静态方法,就可以创建相应的线程池:

  public static executorsevice newsinglethreadexecutor()

  public static executorsevice newfixedthreadpool()

  public static executorsevice newcachedthreadpool()

  newsinglethreadexecutor返回以个包含单线程的executor,将多个任务交给此exector时,这个线程处理完一个任务后接着处理下一个任务,若该线程出现异常,将会有一个新的线程来替代。

  newfixedthreadpool返回一个包含指定数目线程的线程池,如果任务数量多于线程数目,那么没有没有执行的任务必须等待,直到有任务完成为止。

  newcachedthreadpool根据用户的任务数创建相应的线程来处理,该线程池不会对线程数目加以限制,完全依赖于jvm能创建线程的数量,可能引起内存不足。

  我们只需要将待执行的任务放入run方法中即可,将runnable接口的实现类交给线程池的execute方法,作为它的一个参数,如下所示:

executor executor = executors.newsinglethreadexecutor();
executor.execute(new runnable(){
  public void run(){
    //执行的任务  
 }
}

  如果需要给任务传递参数,可以通过创建一个runnable接口的实现类来完成。

3.实例
(1):newsinglethreadexecutor
mythread.java

publicclassmythread extends thread {
  @override
  publicvoid run() {
    system.out.println(thread.currentthread().getname() + "正在执行。。。");
  }
}
testsinglethreadexecutor.java
publicclasstestsinglethreadexecutor {
  publicstaticvoid main(string[] args) {
    //创建一个可重用固定线程数的线程池
    executorservice pool = executors. newsinglethreadexecutor();
    //创建实现了runnable接口对象,thread对象当然也实现了runnable接口
    thread t1 = new mythread();
    thread t2 = new mythread();
    thread t3 = new mythread();
    thread t4 = new mythread();
    thread t5 = new mythread();
    //将线程放入池中进行执行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //关闭线程池
    pool.shutdown();
  }
}

输出结果

pool-1-thread-1正在执行。。。
pool-1-thread-1正在执行。。。
pool-1-thread-1正在执行。。。
pool-1-thread-1正在执行。。。
pool-1-thread-1正在执行。。。

(2):newfixedthreadpool
testfixedthreadpool.java

publicclass testfixedthreadpool {
  publicstaticvoid main(string[] args) {
    //创建一个可重用固定线程数的线程池
    executorservice pool = executors.newfixedthreadpool(2);
    //创建实现了runnable接口对象,thread对象当然也实现了runnable接口
    thread t1 = new mythread();
    thread t2 = new mythread();
    thread t3 = new mythread();
    thread t4 = new mythread();
    thread t5 = new mythread();
    //将线程放入池中进行执行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //关闭线程池
    pool.shutdown();
  }
}

输出结果

pool-1-thread-1正在执行。。。
pool-1-thread-2正在执行。。。
pool-1-thread-1正在执行。。。
pool-1-thread-2正在执行。。。
pool-1-thread-1正在执行。。。

(3): newcachedthreadpool
testcachedthreadpool.java

publicclass testcachedthreadpool {
  publicstaticvoid main(string[] args) {
    //创建一个可重用固定线程数的线程池
    executorservice pool = executors.newcachedthreadpool();
    //创建实现了runnable接口对象,thread对象当然也实现了runnable接口
    thread t1 = new mythread();
    thread t2 = new mythread();
    thread t3 = new mythread();
    thread t4 = new mythread();
    thread t5 = new mythread();
    //将线程放入池中进行执行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //关闭线程池
    pool.shutdown();
  }
}

输出结果:

pool-1-thread-2正在执行。。。
pool-1-thread-4正在执行。。。
pool-1-thread-3正在执行。。。
pool-1-thread-1正在执行。。。
pool-1-thread-5正在执行。。。

(4):newscheduledthreadpool
testscheduledthreadpoolexecutor.java

publicclass testscheduledthreadpoolexecutor {
  publicstaticvoid main(string[] args) {
    scheduledthreadpoolexecutor exec = new scheduledthreadpoolexecutor(1);
    exec.scheduleatfixedrate(new runnable() {//每隔一段时间就触发异常
           @override
           publicvoid run() {
              //throw new runtimeexception();
              system.out.println("================");
           }
         }, 1000, 5000, timeunit.milliseconds);
    exec.scheduleatfixedrate(new runnable() {//每隔一段时间打印系统时间,证明两者是互不影响的
           @override
           publicvoid run() {
              system.out.println(system.nanotime());
           }
         }, 1000, 2000, timeunit.milliseconds);
  }
}

输出结果

================
8384644549516
8386643829034
8388643830710
================
8390643851383
8392643879319
8400643939383

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网