java util concurrent_java.util.concurrent简介

java.util.concurrent简介

java.util.concurrent包提供了很多有用的类,方便我们进行并发程序的开发。本文将会做一个总体的简单介绍。

主要的组件

java.util.concurrent包含了很多内容, 本文将会挑选其中常用的一些类来进行大概的说明:

Executor

ExecutorService

ScheduledExecutorService

Future

CountDownLatch

CyclicBarrier

Semaphore

ThreadFactory

Executor

Executor是一个接口,它定义了一个execute方法,这个方法接收一个Runnable,并在其中调用Runnable的run方法。

我们看一个Executor的实现:

public class Invoker implements Executor {

@Override

public void execute(Runnable r) {

r.run();

}

}

现在我们可以直接调用该类中的方法:

public void execute() {

Executor executor = new Invoker();

executor.execute( () -> {

log.info("{}", Thread.currentThread().toString());

});

}

注意,Executor并不一定要求执行的任务是异步的。

ExecutorService

如果我们真正的需要使用多线程的话,那么就需要用到ExecutorService了。

ExecutorService管理了一个内存的队列,并定时提交可用的线程。

我们首先定义一个Runnable类:

public class Task implements Runnable {

@Override

public void run() {

// task details

}

}

我们可以通过Executors来方便的创建ExecutorService:

ExecutorService executor = Executors.newFixedThreadPool(10);

上面创建了一个ThreadPool, 我们也可以创建单线程的ExecutorService:

ExecutorService executor =Executors.newSingleThreadExecutor();

我们这样提交task:

public void execute() {

executor.submit(new Task());

}

因为ExecutorService维持了一个队列,所以它不会自动关闭, 我们需要调用executor.shutdown() 或者executor.shutdownNow()来关闭它。

如果想要判断ExecutorService中的线程在收到shutdown请求后是否全部执行完毕,可以调用如下的方法:

try {

executor.awaitTermination( 5l, TimeUnit.SECONDS );

} catch (InterruptedException e) {

e.printStackTrace();

}

ScheduledExecutorService

ScheduledExecutorService和ExecutorService很类似,但是它可以周期性的执行任务。

我们这样创建ScheduledExecutorService:

ScheduledExecutorService executorService

= Executors.newSingleThreadScheduledExecutor();

executorService的schedule方法,可以传入Runnable也可以传入Callable:

Future future = executorService.schedule(() -> {

// ...

return "Hello world";

}, 1, TimeUnit.SECONDS);

ScheduledFuture> scheduledFuture = executorService.schedule(() -> {

// ...

}, 1, TimeUnit.SECONDS);

还有两个比较相近的方法:

scheduleAtFixedRate( Runnable command, long initialDelay, long period, TimeUnit unit )

scheduleWithFixedDelay( Runnable command, long initialDelay, long delay, TimeUnit unit )

两者的区别是前者的period是以任务开始时间来计算的,后者是以任务结束时间来计算。

Future

Future用来获取异步执行的结果。可以调用cancel(boolean mayInterruptIfRunning) 方法来取消线程的执行。

我们看下怎么得到一个Future对象:

public void invoke() {

ExecutorService executorService = Executors.newFixedThreadPool(10);

Future future = executorService.submit(() -> {

// ...

Thread.sleep(10000l);

return "Hello world";

});

}

我们看下怎么获取Future的结果:

if (future.isDone() && !future.isCancelled()) {

try {

str = future.get();

} catch (InterruptedException | ExecutionException e) {

e.printStackTrace();

}

}

future还可以接受一个时间参数,超过指定的时间,将会报TimeoutException。

try {

future.get(10, TimeUnit.SECONDS);

} catch (InterruptedException | ExecutionException | TimeoutException e) {

e.printStackTrace();

}

CountDownLatch

CountDownLatch是一个并发中很有用的类,CountDownLatch会初始化一个counter,通过这个counter变量,来控制资源的访问。我们会在后面的文章详细介绍。

CyclicBarrier

CyclicBarrier和CountDownLatch很类似。CyclicBarrier主要用于多个线程互相等待的情况,可以通过调用await() 方法等待,知道达到要等的数量。

public class Task implements Runnable {

private CyclicBarrier barrier;

public Task(CyclicBarrier barrier) {

this.barrier = barrier;

}

@Override

public void run() {

try {

LOG.info(Thread.currentThread().getName() +

" is waiting");

barrier.await();

LOG.info(Thread.currentThread().getName() +

" is released");

} catch (InterruptedException | BrokenBarrierException e) {

e.printStackTrace();

}

}

}

public void start() {

CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {

// ...

LOG.info("All previous tasks are completed");

});

Thread t1 = new Thread(new Task(cyclicBarrier), "T1");

Thread t2 = new Thread(new Task(cyclicBarrier), "T2");

Thread t3 = new Thread(new Task(cyclicBarrier), "T3");

if (!cyclicBarrier.isBroken()) {

t1.start();

t2.start();

t3.start();

}

}

Semaphore

Semaphore包含了一定数量的许可证,通过获取许可证,从而获得对资源的访问权限。通过 tryAcquire()来获取许可,如果获取成功,许可证的数量将会减少。

一旦线程release()许可,许可的数量将会增加。

我们看下怎么使用:

static Semaphore semaphore = new Semaphore(10);

public void execute() throws InterruptedException {

LOG.info("Available permit : " + semaphore.availablePermits());

LOG.info("Number of threads waiting to acquire: " +

semaphore.getQueueLength());

if (semaphore.tryAcquire()) {

try {

// ...

}

finally {

semaphore.release();

}

}

}

ThreadFactory

ThreadFactory可以很方便的用来创建线程:

public class ThreadFactoryUsage implements ThreadFactory {

private int threadId;

private String name;

public ThreadFactoryUsage(String name) {

threadId = 1;

this.name = name;

}

@Override

public Thread newThread(Runnable r) {

Thread t = new Thread(r, name + "-Thread_" + threadId);

log.info("created new thread with id : " + threadId +

" and name : " + t.getName());

threadId++;

return t;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值