一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程

詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程

2020-09-02 10:12關(guān)瑋琳linSir Java教程

這篇文章主要介紹了Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程,ExecutorService可以維護我們的大量線程在操作臨界資源時的穩(wěn)定性。

自從java1.5以后,官網(wǎng)就推出了executor這樣一個類,這個類,可以維護我們的大量線程在操作臨界資源時的穩(wěn)定性。
先上一段代碼吧:

testrunnable.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class testrunnable implements runnable {
  private string name;
 
  public testrunnable(string name) {
    this.name = name;
  }
 
  @override
  public void run() {
    while (true) {
      if (main.surplus < 0)
        return;
      main.surplus--;
      system.out.println(name + " " + main.surplus);
    }
  }
}

main入口

?
1
2
3
4
5
6
7
8
9
10
11
12
public static void main(string[] args) {
 
     testrunnable runnable = new testrunnable("runnable1");
     testrunnable runnable2 = new testrunnable("runnable2");
 
     thread t1 = new thread(runnable);
     thread t2 = new thread(runnable2);
 
     t1.start();
     t2.start();
 
  }

詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程

這樣,我們就看到了,數(shù)據(jù)肯定是亂了的,當(dāng)然這個時候我們可以加上一個synchronized的關(guān)鍵字,但是這樣也會出現(xiàn)點小問題的

詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程

下面我打算采用一種java內(nèi)置的線程管理的機制,來解決這個問題,解決這個問題的思路大概就是,我們維護了一個線程池,當(dāng)有請求操作的時候統(tǒng)統(tǒng)進入線程池,并且我們只開了一個線程,可以讓請求順序執(zhí)行,順序調(diào)用臨界資源,就很安全了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.concurrent.callable;
import java.util.concurrent.executionexception;
import java.util.concurrent.executorservice;
import java.util.concurrent.executors;
import java.util.concurrent.future;
 
public class main {
  public static int surplus = 10;
 
  private executorservice executor = executors.newsinglethreadexecutor();
 
  void addtask(runnable runnable) {
    executor.execute(runnable);
  }
 
  <v> v addtask(callable<v> callable) {
    future<v> submit = executor.submit(callable);
    try {
      return submit.get();
    } catch (interruptedexception e) {
      system.out.println("interruptedexception" + e.tostring());
    } catch (executionexception e) {
      system.out.println("executionexception" + e.tostring());
    }
    return null;
  }
 
  public void testaddtask(string name) {
    addtask(new runnable() {
      @override
      public void run() {
        for (int i = 0; i < 3; i++) {
          if (main.surplus <= 0)
            return;
          main.surplus--;
          system.out.println(name + " " + main.surplus);
        }
 
      }
    });
  }
 
  public void testaddtask2(string name) {
    int count = addtask(new callable<integer>() {
      @override
      public integer call() throws exception {
        for (int i = 0; i < 3; i++) {
          if (main.surplus <= 0)
            return 0;
          main.surplus--;
          system.out.println(name + " " + main.surplus);
        }
        return main.surplus;
      }
    });
 
  }
 
  public void close() {
    executor.shutdown();
  }
 
  public static void main(string[] args) {
    main main = new main();
    main.testaddtask("task1");
    main.testaddtask2("task2");
    main.testaddtask("task3");
    main.testaddtask2("task4");
    main.close();
  }
}

在這里,我們定義了兩種方法,分別是addtask,具有泛型的addtask,這兩種方法實現(xiàn)原理都是一樣的,其中一個是有回調(diào)的,一個是沒有回調(diào)的,就看項目需求了吧。

詳解Java利用ExecutorService實現(xiàn)同步執(zhí)行大量線程

然后分別調(diào)用這兩個方法咯,就可以看到結(jié)果是非常有序,且不會混亂的。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.jianshu.com/p/ccdb616723ab

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色婷婷狠狠 | 日本精品欧洲www | 日本在线观看视频 | 91香蕉视频在线 | 狠狠色伊人亚洲综合网站色 | 国内精品久久久久香蕉 | 韩国美女豪爽一级毛片 | 精品在线播放 | 日本成熟老妇xxxx | 高h辣文小说网 烧书阁 | 亚洲一区 在线播放 | 免费看片aⅴ免费大片 | 欧美在线国产 | 国产综合久久久久久 | 亚洲图片一区二区三区 | 国产裸舞在线一区二区 | 国产成人精品日本亚洲网站 | 222aaa精品影院 | 99久久精品免费看国产一区 | 性xxx免费视频 | 欧美一级视频在线观看 | 5g影院天天| 91夜夜操 | 欧美贵妇videos办公室360 | 青青草99 | 51国产午夜精品免费视频 | 女班长的放荡日记高h | 鞋奴的视频VK | 亚洲男gay同性同志 亚洲免费在线看 | ffyybb免费福利视频 | 免费观看成年肉动漫网站 | 日韩理论在线观看 | 欧美国产日韩1区俺去了 | 日韩在线一区二区三区 | 日韩欧美一区二区三区免费看 | 国产成人免费高清激情明星 | 欧美黑人性| 日本人与黑人做爰视频网站 | 亚洲男人的天堂视频 | 亚洲精品视频久久 | hd性欧美俱乐部中文 |