欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

帶你快速搞定java并發(fā)庫

 更新時間:2021年07月15日 10:11:06   作者:香菜聊游戲  
本文主要介紹了java高并發(fā)寫入用戶信息到數據庫的幾種方法,具有很好的參考價值。下面跟著小編一起來看下吧,希望能給你帶來幫助

一、總覽

計算機程序 = 數據 + 算法。

并發(fā)編程的一切根本原因是為了保證數據的正確性,線程的效率性。

Java并發(fā)庫共分為四個大的部分,如下圖

Executor 和 future 是為了保證線程的效率性

Lock 和數據結構 是為了維持數據的一致性。

Java并發(fā)編程的時候,思考順序為,

對自己的數據要么加鎖。要么使用提供的數據結構,保證數據的安全性

調度線程的時候使用Executor更好的調度。

二、Executor總覽

Executor 提供一種將任務提交與每個任務將如何運行的機制(包括線程使用的細節(jié)、調度等)分離開來的方法。

相當于manager,老板讓manager去執(zhí)行一件任務,具體的是誰執(zhí)行,什么時候執(zhí)行,就不管了。

看上圖的繼承關系,介紹幾個

內置的線程池基本上都在這里

newScheduledThreadPool 定時執(zhí)行的線程池

newCachedThreadPool 緩存使用過的線程

newFixedThreadPool 固定數量的線程池

newWorkStealingPool 將大任務分解為小任務的線程池

三、繼承結構

構造函數

包含一個定時的service

public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
}
static class DelegatedScheduledExecutorService
        extends DelegatedExecutorService
        implements ScheduledExecutorService {
    private final ScheduledExecutorService e;
    DelegatedScheduledExecutorService(ScheduledExecutorService executor) {
        super(executor);
        e = executor;
    }

四、怎么保證只有一個線程

定時執(zhí)行的時候調用這個方法,調用過程如下,注意看其中的注釋,由上往下的調用順序

public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                 long initialDelay,
                                                 long delay,
                                                 TimeUnit unit) {
    if (command == null || unit == null)
        throw new NullPointerException();
    if (delay <= 0)
        throw new IllegalArgumentException();
    ScheduledFutureTask<Void> sft =
        new ScheduledFutureTask<Void>(command,
                                      null,
                                      triggerTime(initialDelay, unit),
                                      unit.toNanos(-delay));
    RunnableScheduledFuture<Void> t = decorateTask(command, sft);
    sft.outerTask = t;
    //  延遲執(zhí)行
    delayedExecute(t);
    return t;
}
private void delayedExecute(RunnableScheduledFuture<?> task) {
    if (isShutdown())
        reject(task);
    else {
        // 加入任務隊列
        super.getQueue().add(task);
        if (isShutdown() &&
            !canRunInCurrentRunState(task.isPeriodic()) &&
            remove(task))
            task.cancel(false);
        else
            // 確保執(zhí)行
            ensurePrestart();
    }
}
// 如果worker數量小于corePoolSize,創(chuàng)建新的線程,其他情況不處理
void ensurePrestart() {
    int wc = workerCountOf(ctl.get());
    if (wc < corePoolSize)
        addWorker(null, true);
    else if (wc == 0)
        addWorker(null, false);
}

五、怎么保證時間可以定時執(zhí)行

public ScheduledFuture<?> schedule(Runnable command,
                                   long delay,
                                   TimeUnit unit) {
    if (command == null || unit == null)
        throw new NullPointerException();
    RunnableScheduledFuture<?> t = decorateTask(command,
        new ScheduledFutureTask<Void>(command, null,
                                      triggerTime(delay, unit)));
    delayedExecute(t);
    return t;
}

在每次執(zhí)行的時候會把下一次執(zhí)行的時間放進任務中

private long triggerTime(long delay, TimeUnit unit) {
    return triggerTime(unit.toNanos((delay < 0) ? 0 : delay));
}
/**
 * Returns the trigger time of a delayed action.
 */
long triggerTime(long delay) {
    return now() +
        ((delay < (Long.MAX_VALUE >> 1)) ? delay : overflowFree(delay));
}

FutureTask 定時是通過LockSupport.parkNanos(this, nanos);LockSupport.park(this);

private int awaitDone(boolean timed, long nanos)
    throws InterruptedException {
    final long deadline = timed ? System.nanoTime() + nanos : 0L;
    WaitNode q = null;
    boolean queued = false;
    for (;;) {
        if (Thread.interrupted()) {
            removeWaiter(q);
            throw new InterruptedException();
        }
        int s = state;
        if (s > COMPLETING) {
            if (q != null)
                q.thread = null;
            return s;
        }
        else if (s == COMPLETING) // cannot time out yet
            Thread.yield();
        else if (q == null)
            q = new WaitNode();
        else if (!queued)
            queued = UNSAFE.compareAndSwapObject(this, waitersOffset,
                                                 q.next = waiters, q);
        else if (timed) {
            nanos = deadline - System.nanoTime();
            if (nanos <= 0L) {
                removeWaiter(q);
                return state;
            }
            //注意這里
            LockSupport.parkNanos(this, nanos);
        }
        else //注意這里
            LockSupport.park(this);
    }
}

總結:Executor是通過將任務放在隊列中,生成的futureTask。然后將生成的任務在隊列中排序,將時間最近的需要出發(fā)的任務做檢查。如果時間不到,就阻塞線程到下次出發(fā)時間。

注意:newSingleThreadScheduledExecutor只會有一個線程,不管你提交多少任務,這些任務會順序執(zhí)行,如果發(fā)生異常會取消下面的任務,線程池也不會關閉,注意捕捉異常

六、使用

ScheduledExecutorService single = Executors.newSingleThreadScheduledExecutor();
Runnable runnable1 = () -> {
    try {
        Thread.sleep(4000);
        System.out.println("11111111111111");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
};
Runnable runnable2 = () -> {
    try {
        Thread.sleep(4000);
        System.out.println("222");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
};
single.scheduleWithFixedDelay(runnable1,0,1, TimeUnit.SECONDS);
single.scheduleWithFixedDelay(runnable2,0,2, TimeUnit.SECONDS);

11111111111111 222 11111111111111 222 11111111111111

在項目中要注意關閉線程池

actionService = Executors.newSingleThreadScheduledExecutor();
        actionService.scheduleWithFixedDelay(() -> {
            try {
                Thread.currentThread().setName("robotActionService");
                Integer robotId = robotQueue.poll();
                if (robotId == null) {
                    //    關閉線程池
                    actionService.shutdown();
                } else {
                    int aiLv = robots.get(robotId);
                    if (actionQueueMap.containsKey(aiLv)) {
                        ActionQueue actionQueue = actionQueueMap.get(aiLv);
                        actionQueue.doAction(robotId);
                    }
                }
            } catch (Exception e) {
                //    捕捉異常
                LOG.error("",e);
            }
        }, 1, 1, TimeUnit.SECONDS);

總結

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!

相關文章

  • 一文詳解Java?etcd的應用場景及編碼實戰(zhàn)

    一文詳解Java?etcd的應用場景及編碼實戰(zhàn)

    etcd?是一個高度一致的分布式鍵值存儲系統。本文旨在幫助大家理解etcd,從宏觀角度俯瞰etcd全局,掌握etcd的基本操作技能,需要的可以參考一下
    2022-08-08
  • SpringMVC @ResponseBody 415錯誤處理方式

    SpringMVC @ResponseBody 415錯誤處理方式

    這篇文章主要介紹了SpringMVC @ResponseBody 415錯誤處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java并發(fā)編程_線程池的使用方法(詳解)

    java并發(fā)編程_線程池的使用方法(詳解)

    下面小編就為大家?guī)硪黄猨ava并發(fā)編程_線程池的使用方法(詳解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java怎么獲取當前時間、計算程序運行時間源碼詳解(超詳細!)

    Java怎么獲取當前時間、計算程序運行時間源碼詳解(超詳細!)

    有的時候,我們需要查看某一段代碼的性能如何,最為簡單的方式,可以通過計算該段代碼執(zhí)行的耗時,來進行簡單的判斷,這篇文章主要給大家介紹了關于Java怎么獲取當前時間、計算程序運行時間的相關資料,需要的朋友可以參考下
    2024-07-07
  • 詳談cxf和axis兩種框架下的webservice客戶端開發(fā)

    詳談cxf和axis兩種框架下的webservice客戶端開發(fā)

    這篇文章主要介紹了詳談cxf和axis兩種框架下的webservice客戶端開發(fā),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 在SpringBoot中使用@Value注解來設置默認值的方法

    在SpringBoot中使用@Value注解來設置默認值的方法

    Spring Boot提供了一種使用注解設置默認值的方式,即使用 @Value 注解,下面這篇文章主要給大家介紹了關于如何在SpringBoot中使用@Value注解來設置默認值的相關資料,需要的朋友可以參考下
    2023-10-10
  • Java實現多線程聊天室

    Java實現多線程聊天室

    這篇文章主要為大家詳細介紹了Java實現多線程聊天室,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • springboot項目數據庫密碼如何加密

    springboot項目數據庫密碼如何加密

    在我們日常開發(fā)中,我們可能很隨意把數據庫密碼直接明文暴露在配置文件中,今天就來聊聊在springboot項目中如何對數據庫密碼進行加密,感興趣的可以了解一下
    2021-07-07
  • SpringBoot定時任務動態(tài)擴展ScheduledTaskRegistrar詳解

    SpringBoot定時任務動態(tài)擴展ScheduledTaskRegistrar詳解

    這篇文章主要為大家介紹了SpringBoot定時任務動態(tài)擴展ScheduledTaskRegistrar類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Java錯誤問題:找不到或無法加載主類的解決

    Java錯誤問題:找不到或無法加載主類的解決

    這篇文章主要介紹了Java錯誤問題:找不到或無法加載主類的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論