executor java_Java:使用Executors创建和管理线程

1.类Executors

此类中提供的一些方法有:

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

packagecom.zj.concurrency.executors;

publicclassMyThreadimplementsRunnable {

privateintcount= 1,number;

publicMyThread(intnum) {

number= num;

System.out.println("Create Thread-"+number);

}

publicvoidrun() {

while(true) {

System.out.println("Thread-"+number+" run "+count+" time(s)");

if(++count== 3)

return;

}

}

}

这个线程会打印出相应的创建和执行信息。

4.2使用CachedThreadPool启动线程

CachedThreadPool.java

packagecom.zj.concurrency.executors;

importjava.util.concurrent.ExecutorService;

importjava.util.concurrent.Executors;

publicclassCachedThreadPool {

publicstaticvoidmain(String[] args) {

ExecutorService exec = Executors.newCachedThreadPool();

for(inti = 0; i < 5; i++)

exec.execute(newMyThread(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

packagecom.zj.concurrency.executors;

importjava.util.concurrent.ExecutorService;

importjava.util.concurrent.Executors;

publicclassFixedThreadPool {

publicstaticvoidmain(String[] args) {

ExecutorService exec = Executors.newFixedThreadPool(2);

for(inti = 0; i < 5; i++)

exec.execute(newMyThread(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

packagecom.zj.concurrency.executors;

importjava.util.concurrent.ExecutorService;

importjava.util.concurrent.Executors;

publicclassSingleThreadExecutor {

publicstaticvoidmain(String[] args) {

ExecutorService exec = Executors.newSingleThreadExecutor();

for(inti = 0; i < 5; i++)

exec.execute(newMyThread(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

packagecom.zj.concurrency.executors.factory;

importjava.util.concurrent.ThreadFactory;

publicclassDaemonThreadFactoryimplementsThreadFactory {

publicThread newThread(Runnable r)

{

Thread t =newThread(r);

t.setDaemon(true);

returnt;

}

}

5.2设置优先级属性

最高优先级MaxPriorityThreadFactory.java

packagecom.zj.concurrency.executors.factory;

importjava.util.concurrent.ThreadFactory;

publicclassMaxPriorityThreadFactoryimplementsThreadFactory {

publicThread newThread(Runnable r)

{

Thread t =newThread(r);

t.setPriority(Thread.MAX_PRIORITY);

returnt;

}

}

最低优先级MinPriorityThreadFactory.java

packagecom.zj.concurrency.executors.factory;

importjava.util.concurrent.ThreadFactory;

publicclassMinPriorityThreadFactoryimplementsThreadFactory {

publicThread newThread(Runnable r)

{

Thread t =newThread(r);

t.setPriority(Thread.MIN_PRIORITY);

returnt;

}

}

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

ExecFromFactory.java

packagecom.zj.concurrency.executors;

importjava.util.concurrent.ExecutorService;

importjava.util.concurrent.Executors;

importcom.zj.concurrency.executors.factory.DaemonThreadFactory;

importcom.zj.concurrency.executors.factory.MaxPriorityThreadFactory;

importcom.zj.concurrency.executors.factory.MinPriorityThreadFactory;

publicclassExecFromFactory {

publicstaticvoidmain(String[] args)throwsException

{

ExecutorService defaultExec =

Executors.newCachedThreadPool();

ExecutorService daemonExec = Executors

.newCachedThreadPool(newDaemonThreadFactory());

ExecutorService maxPriorityExec =

Executors

.newCachedThreadPool(newMaxPriorityThreadFactory());

ExecutorService minPriorityExec =

Executors

.newCachedThreadPool(newMinPriorityThreadFactory());

for(inti = 0; i < 10; i++)

daemonExec.execute(newMyThread(i));

for(inti = 10; i < 20; i++)

if(i == 10)

maxPriorityExec.execute(newMyThread(i));

elseif(i == 11)

minPriorityExec.execute(newMyThread(i));

else

defaultExec.execute(newMyThread(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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值