最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Concurrent并發(fā)包詳解

2020-08-06 22:24 作者:CodeSnake  | 我要投稿

提前話:

進行myCat摸索時到處可見的并發(fā)包的原子性數(shù)據(jù)操作,與其摸瞎子過河,不如先把瞎子摸一遍!??!?? 于是便有了下面這片的技術(shù)博客(關(guān)于concurrent概述)??

Overview of the java.util.concurrent(概述)

原文網(wǎng)址

Main Components(主要構(gòu)成)

The java.util.concurrent contains way too many features to discuss in a single write-up. In this article, we will mainly focus on some of the most useful utilities from this package like:(在這片文章中如果一次撰寫或討論concurrent的功能、方式會太多,所以我們將大部分的精力集中于最常使用的實用程序)

  • Executor(執(zhí)行者)

  • ExecutorService(執(zhí)行器服務(wù))

  • ScheduledExecutorService(執(zhí)行器服務(wù)時間表)

  • Future

  • CountDownLatch

  • CyclicBarrier

  • Semaphore

  • ThreadFactory

  • BlockingQueue

  • DelayQueue

  • Locks

  • Phaser

You can also find many dedicated articles to individual classes here.

Executor

*Executor* is an interface that represents an object that executes provided tasks.

(Executor 是一個代表執(zhí)行提供任務(wù)的一個接口對象)

It depends on the particular implementation (from where the invocation is initiated) if the task should be

它依賴于一個特定的實現(xiàn)(從啟動調(diào)用的位置)如果這個任務(wù)需要運行在一個新線程或者當前線程

run on a new or current thread. Hence, using this interface, we can decouple the task execution flow from

因此,使用這個接口 我們可以將任務(wù)執(zhí)行流與實際任務(wù)執(zhí)行機制進行解耦(decouple)。

the actual task execution mechanism.

One point to note here is that Executor does not strictly require the task execution to be asynchronous.

這里需要注意的一點是,執(zhí)行器并不嚴格要求任務(wù)執(zhí)行是異步的。

In the simplest case, an executor can invoke the submitted task instantly in the invoking thread.

在這個簡單的例子上, 執(zhí)行者可以在調(diào)用線程中立即(instantly 即刻)調(diào)用提交的任務(wù)。

We need to create an invoker to create the executor instance:

我們需要生成一個調(diào)用者去生成一個執(zhí)行者實例


看到上述的案例,想必你想問,那我直接print(“Hello Executor”)不行嗎???(其實可以作為executor的生成案例) 我也正疑惑呢 ?so ? 得找個好案例

Executor


ExecutorService

ExecutorService is a complete solution for asynchronous processing.(執(zhí)行器服務(wù)是異步進程的一個完整解決方案) It manages an in-memory queue and schedules submitted tasks based on thread availability.

它管理著內(nèi)存中的隊列(什么隊列????? ??)并根據(jù)線程可用性安排已提交的任務(wù)。

To use ExecutorService, we need to create one Runnable class.



Now we can create the ExecutorService instance and assign this task. At the time of creation,

下面我們開始生成一個ExecutorService 實例并且分配一些任務(wù),在生成的時候,我們需要指定線程池的大小

we need to specify the thread-pool size.

ExecutorService executor = Executors.newFixedThreadPool(10);

If we want to create a single-threaded ExecutorService instance, we can use

如果我們想創(chuàng)建一個單線程的執(zhí)行器服務(wù)實例,我們可以使用()去生成一個實例

*newSingleThreadExecutor(ThreadFactory threadFactory)* to create the instance.

Once the executor is created, we can use it to submit the task.

一旦這個執(zhí)行者被生成,我們就可以使用它去提交任務(wù)


public void execute() {
? ?executor.submit(new Task());
}

We can also create the Runnable instance while submitting the task.

我們還可以在提交任務(wù)時創(chuàng)建Runnable實例。

executor.submit(() -> {
? ?new Task();
});

It also comes with two out-of-the-box execution termination methods.

它也有兩種現(xiàn)成(out of the box)的執(zhí)行終止方法

The first one is shutdown(); it waits until all the submitted tasks finish executing.

等待所有的提交任務(wù)執(zhí)行完畢

The other method is shutdownNow() which immediately terminates all the pending/executing tasks.

該方法立即終止(terminates)所有待處理/正在執(zhí)行的任務(wù)。

There is also another method awaitTermination(long timeout, TimeUnit unit)

which forcefully blocks until all tasks have completed execution after a shutdown event triggered

or execution-timeout occurred,

在觸發(fā)關(guān)閉事件或發(fā)生執(zhí)行超時后或者這個執(zhí)行線程本身中斷,它會強制阻塞直到所有任務(wù)都已完成執(zhí)行,

or the execution thread itself is interrupted(中斷),

try {
? ?executor.awaitTermination( 20l, TimeUnit.NANOSECONDS );
} catch (InterruptedException e) {
? ?e.printStackTrace();
}

這個executorService還是有點懵逼??


ScheduledExecutorService

ScheduledExecutorService is a similar interface to ExecutorService, but it can perform tasks periodically.

xxx是類似于執(zhí)行器服務(wù)的一個接口,但是它可定期執(zhí)行任務(wù),So you know that

*Executor and ExecutorService*‘s methods are scheduled on the spot without introducing any artificial delay.

XXX的方法是立即運行 沒有引入任何人為的延遲

Zero or any negative value signifies that the request needs to be executed instantly.

零和任何負值都表示這個請求需要立即執(zhí)行

We can use both Runnable and Callable interface to define the task.

public void execute() {
? ?ScheduledExecutorService executorService
? ? ?= Executors.newSingleThreadScheduledExecutor();

? ?Future<String> future = executorService.schedule(() -> {
? ? ? ?// ...
? ? ? ?return "Hello world";
? ?}, 1, TimeUnit.SECONDS);

? ?ScheduledFuture<?> scheduledFuture = executorService.schedule(() -> {
? ? ? ?// ...
? ?}, 1, TimeUnit.SECONDS);

? ?executorService.shutdown();
}

ScheduledExecutorService can also schedule the task after some given fixed delay:

executorService.scheduleAtFixedRate(() -> {
? ?// ...
}, 1, 10, TimeUnit.SECONDS);

executorService.scheduleWithFixedDelay(() -> {
? ?// ...
}, 1, 10, TimeUnit.SECONDS);

Here, the scheduleAtFixedRate( Runnable command, long initialDelay, long period, TimeUnit unit ) method creates and executes a periodic action that is invoked firstly after the provided initial delay, and subsequently with the given period until the service instance shutdowns.

The scheduleWithFixedDelay( Runnable command, long initialDelay, long delay, TimeUnit unit ) method creates and executes a periodic action that is invoked firstly after the provided initial delay, and repeatedly with the given delay between the termination of the executing one and the invocation of the next one.

?

未完————測試效果


Concurrent并發(fā)包詳解的評論 (共 條)

分享到微博請遵守國家法律
阳城县| 加查县| 朝阳市| 和田县| 洮南市| 海宁市| 甘肃省| 霍林郭勒市| 义乌市| 彭山县| 特克斯县| 平果县| 武邑县| 福清市| 澄江县| 凤翔县| 揭东县| 广东省| 凭祥市| 黑水县| 湘乡市| 渑池县| 信丰县| 通化县| 曲周县| 阜城县| 天全县| 茌平县| 淅川县| 玉门市| 綦江县| 呈贡县| 漯河市| 屏南县| 贞丰县| 汪清县| 阳朔县| 平利县| 太谷县| 永修县| 大渡口区|