当前位置: 移动技术网 > IT编程>开发语言>Java > Java并发编程中使用Executors类创建和管理线程的用法

Java并发编程中使用Executors类创建和管理线程的用法

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

1. 类 executors
executors类可以看做一个“工具类”。援引jdk1.6 api中的介绍:
  此包中所定义的 executor、executorservice、scheduledexecutorservice、threadfactory 和 callable 类的工厂和实用方法。此类支持以下各种方法:
(1)创建并返回设置有常用配置字符串的 executorservice 的方法。
(2)创建并返回设置有常用配置字符串的 scheduledexecutorservice 的方法。
(3)创建并返回“包装的”executorservice 方法,它通过使特定于实现的方法不可访问来禁用重新配置。
(4)创建并返回 threadfactory 的方法,它可将新创建的线程设置为已知的状态。
(5)创建并返回非闭包形式的 callable 的方法,这样可将其用于需要 callable 的执行方法中。
    通过这个类能够获得多种线程池的实例,例如可以调用newsinglethreadexecutor()获得单线程的executorservice,调 用newfixedthreadpool()获得固定大小线程池的executorservice,等等。拿到executorservice可以做的事情就比 较多了,最简单的是用它来执行runnable对象,也可以执行一些实现了callable<t>的对象。用thread的start()方 法没有返回值,如果该线程执行的方法有返回值那用executorservice就再好不过了,可以选择submit()、invokeall()或者 invokeany(),根据具体情况选择合适的方法即可。
此类中提供的一些方法有:
1.1 public static executorservice newcachedthreadpool()
创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们。对于执行很多短期异步任务的程序而言,这些线程池通常可提高程序性能。
 
1.2 public static executorservice newfixedthreadpool(int nthreads)
创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程。
 
1.3 public static executorservice newsinglethreadexecutor()
创建一个使用单个 worker 线程的 executor,以无界队列方式来运行该线程。
 
这三个方法都可以配合接口threadfactory的实例一起使用。并且返回一个executorservice接口的实例。
2. 接口 threadfactory
根据需要创建新线程的对象。使用线程工厂就无需再手工编写对 new thread 的调用了,从而允许应用程序使用特殊的线程子类、属性等等。
此接口最简单的实现就是:

class simplethreadfactory implements threadfactory {
  public thread newthread(runnable r) {
   return new thread(r);
  }
 }

3. 接口executorservice
该接口提供了管理终止的方法。
4.创建标准线程池启动线程
4.1 提供一个简单的实现runnable接口的线程
mythread.java

package com.zj.concurrency.executors;
 
public class mythread implements runnable {
  private int count = 1, number;
 
  public mythread(int num) {
    number = num;
    system.out.println("create thread-" + number);
  }
 
  public void run() {
    while (true) {
      system.out.println("thread-" + number + " run " + count+" time(s)");
      if (++count == 3)
       return;
    }
  }
}

这个线程会打印出相应的创建和执行信息。
 
4.2使用cachedthreadpool启动线程
cachedthreadpool.java

package com.zj.concurrency.executors;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
 
public class cachedthreadpool {
  public static void main(string[] args) {
    executorservice exec = executors.newcachedthreadpool();
    for (int i = 0; i < 5; i++)
      exec.execute(new mythread(i));
    exec.shutdown();
  }
}

结果:

create thread-0
create thread-1
create thread-2
create thread-3
thread-0 run 1 time(s)
thread-0 run 2 time(s)
thread-1 run 1 time(s)
thread-1 run 2 time(s)
thread-2 run 1 time(s)
thread-2 run 2 time(s)
create thread-4
thread-4 run 1 time(s)
thread-4 run 2 time(s)
thread-3 run 1 time(s)
thread-3 run 2 time(s)

 
4.3 使用fixedthreadpool启动线程

fixedthreadpool.java
package com.zj.concurrency.executors;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
 
public class fixedthreadpool {
  public static void main(string[] args) {
    executorservice exec = executors.newfixedthreadpool(2);
    for (int i = 0; i < 5; i++)
      exec.execute(new mythread(i));
    exec.shutdown();
  }
}

结果:

create thread-0
create thread-1
create thread-2
create thread-3
create thread-4
thread-0 run 1 time(s)
thread-0 run 2 time(s)
thread-2 run 1 time(s)
thread-2 run 2 time(s)
thread-3 run 1 time(s)
thread-3 run 2 time(s)
thread-4 run 1 time(s)
thread-4 run 2 time(s)
thread-1 run 1 time(s)
thread-1 run 2 time(s)

 
4.4 使用singlethreadexecutor启动线程
singlethreadexecutor.java

package com.zj.concurrency.executors;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
 
public class singlethreadexecutor {
  public static void main(string[] args) {
    executorservice exec = executors.newsinglethreadexecutor();
    for (int i = 0; i < 5; i++)
      exec.execute(new mythread(i));
    exec.shutdown();
  }
}

结果:

create thread-0
create thread-1
create thread-2
create thread-3
create thread-4
thread-0 run 1 time(s)
thread-0 run 2 time(s)
thread-1 run 1 time(s)
thread-1 run 2 time(s)
thread-2 run 1 time(s)
thread-2 run 2 time(s)
thread-3 run 1 time(s)
thread-3 run 2 time(s)
thread-4 run 1 time(s)
thread-4 run 2 time(s)

5.配合threadfactory接口的使用
我们试图给线程加入daemon和priority的属性设置。
5.1设置后台线程属性
daemonthreadfactory.java

package com.zj.concurrency.executors.factory;
import java.util.concurrent.threadfactory;
 
public class daemonthreadfactory implements threadfactory {
  public thread newthread(runnable r) {
    thread t = new thread(r);
    t.setdaemon(true);
    return t;
  }
}

 
5.2 设置优先级属性
最高优先级maxprioritythreadfactory.java

package com.zj.concurrency.executors.factory;
import java.util.concurrent.threadfactory;
 
public class maxprioritythreadfactory implements threadfactory {
  public thread newthread(runnable r) {
    thread t = new thread(r);
    t.setpriority(thread.max_priority);
    return t;
  }
}

最低优先级minprioritythreadfactory.java

package com.zj.concurrency.executors.factory;
import java.util.concurrent.threadfactory;
 
public class minprioritythreadfactory implements threadfactory {
  public thread newthread(runnable r) {
    thread t = new thread(r);
    t.setpriority(thread.min_priority);
    return t;
  }
}

 
5.3启动带有属性设置的线程
execfromfactory.java

package com.zj.concurrency.executors;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
import com.zj.concurrency.executors.factory.daemonthreadfactory;
import com.zj.concurrency.executors.factory.maxprioritythreadfactory;
import com.zj.concurrency.executors.factory.minprioritythreadfactory;
 
public class execfromfactory {
  public static void main(string[] args) throws exception {
    executorservice defaultexec = executors.newcachedthreadpool();
    executorservice daemonexec = executors
       .newcachedthreadpool(new daemonthreadfactory());
    executorservice maxpriorityexec = executors
       .newcachedthreadpool(new maxprioritythreadfactory());
    executorservice minpriorityexec = executors
       .newcachedthreadpool(new minprioritythreadfactory());
    for (int i = 0; i < 10; i++)
      daemonexec.execute(new mythread(i));
    for (int i = 10; i < 20; i++)
      if (i == 10)
       maxpriorityexec.execute(new mythread(i));
      else if (i == 11)
       minpriorityexec.execute(new mythread(i));
      else
       defaultexec.execute(new mythread(i));
  }
}

结果:

create thread-0
create thread-1
create thread-2
create thread-3
thread-0 run 1 time(s)
thread-0 run 2 time(s)
thread-1 run 1 time(s)
thread-1 run 2 time(s)
thread-2 run 1 time(s)
thread-2 run 2 time(s)
create thread-4
thread-4 run 1 time(s)
thread-4 run 2 time(s)
create thread-5
thread-5 run 1 time(s)
thread-5 run 2 time(s)
create thread-6
create thread-7
thread-7 run 1 time(s)
thread-7 run 2 time(s)
create thread-8
thread-8 run 1 time(s)
thread-8 run 2 time(s)
create thread-9
create thread-10
thread-10 run 1 time(s)
thread-10 run 2 time(s)
create thread-11
thread-9 run 1 time(s)
thread-9 run 2 time(s)
thread-6 run 1 time(s)
thread-6 run 2 time(s)
thread-3 run 1 time(s)
thread-3 run 2 time(s)
create thread-12
create thread-13
create thread-14
thread-12 run 1 time(s)
thread-12 run 2 time(s)
thread-13 run 1 time(s)
thread-13 run 2 time(s)
create thread-15
thread-15 run 1 time(s)
thread-15 run 2 time(s)
create thread-16
thread-16 run 1 time(s)
thread-16 run 2 time(s)
create thread-17
create thread-18
create thread-19
thread-14 run 1 time(s)
thread-14 run 2 time(s)
thread-17 run 1 time(s)
thread-17 run 2 time(s)
thread-18 run 1 time(s)
thread-18 run 2 time(s)
thread-19 run 1 time(s)
thread-19 run 2 time(s)
thread-11 run 1 time(s)
thread-11 run 2 time(s)

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

相关文章:

验证码:
移动技术网