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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java并發(fā)程序入門介紹

Java并發(fā)程序入門介紹

2019-12-14 14:49Microgoogle JAVA教程

這篇文章主要介紹了Java并發(fā)程序入門 ,需要的朋友可以參考下

今天看了看Java并發(fā)程序,寫一寫入門程序,并設(shè)置了線程的優(yōu)先級。

?
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
class Elem implements Runnable{
  public static int id = 0;
  private int cutDown = 5;
  private int priority;
  
  public void setPriority(int priority){
    this.priority = priority;
  }
  
  public int getPriority(){
    return this.priority;
  }
  public void run(){
    Thread.currentThread().setPriority(priority);
    int threadId = id++;
    while(cutDown-- > 0){
      double d = 1.2;
      while(d < 10000)
        d = d + (Math.E + Math.PI)/d;
      System.out.println("#" + threadId + "(" + cutDown + ")");
    }
  }
}
public class Basic {
  public static void main(String args[]){
    for(int i = 0; i < 10; i++){
      Elem e = new Elem();
      if(i == 0 )
        e.setPriority(Thread.MAX_PRIORITY);
      else
        e.setPriority(Thread.MIN_PRIORITY);
      Thread t = new Thread(e);
      t.start();
    }
  }
}

由于機器很強悍,所以先開始并沒看到并發(fā)的效果,感覺是按順序執(zhí)行的,所以在中間加了浮點數(shù)的運算來延遲時間。

當(dāng)然,main函數(shù)里面可以用Executors來管理線程。

?
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
import java.util.concurrent.*;
class Elem implements Runnable{
  public static int id = 0;
  private int cutDown = 5;
  private int priority;
  
  public void setPriority(int priority){
    this.priority = priority;
  }
  
  public int getPriority(){
    return this.priority;
  }
  public void run(){
    Thread.currentThread().setPriority(priority);
    int threadId = id++;
    while(cutDown-- > 0){
      double d = 1.2;
      while(d < 10000)
        d = d + (Math.E + Math.PI)/d;
      System.out.println("#" + threadId + "(" + cutDown + ")");
    }
  }
}
public class Basic {
  public static void main(String args[]){
//    for(int i = 0; i < 10; i++){
//      Elem e = new Elem();
//      if(i == 0 )
//        e.setPriority(Thread.MAX_PRIORITY);
//      else
//        e.setPriority(Thread.MIN_PRIORITY);
//      Thread t = new Thread(e);
//      t.start();
//    }
    ExecutorService exec = Executors.newCachedThreadPool();
    for(int i = 0; i < 10; i++){
      Elem e = new Elem();
      if(i == 0 )
        e.setPriority(Thread.MAX_PRIORITY);
      else
        e.setPriority(Thread.MIN_PRIORITY);
      exec.execute(e);
    }
    exec.shutdown();
  }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本xnxnxnxnxn护士 | 午夜国产精品福利在线观看 | 91久久精品国产一区二区 | 色综合97天天综合网 | 麻豆网站在线观看 | 久久久久免费视频 | 韩国久播影院理论片不卡影院 | 99热精品在线免费观看 | 亚洲男gay同性同志 亚洲免费在线看 | 九九精品国产兔费观看久久 | 236zz宅宅最新伦理 | 骚虎最新网址 | 99精彩视频在线观看 | 护士让我吃奶我扒她奶 | 男女xxoo做爰猛烈动态一 | 好湿好紧太硬了我太爽了h 好湿好滑好硬好爽好深视频 | 日韩视频一区 | 日韩免费观看成第15集 | 国内自拍成人网在线视频 | 欧美1| meyd–456佐山爱在线播放 | 亚洲午夜精品久久久久久成年 | 日韩不卡一区二区 | 日韩亚洲国产欧美精品 | 国产老太婆hd老头 | 蜜臀91 | 草莓视频旧版本 | 午夜久久精品 | 福利社在线免费观看 | 日韩不卡一区二区三区 | 美女视频91 | 国产成人精品午夜在线播放 | 日韩在线毛片 | 啊啊啊好爽在线观看 | 精品国产无限资源免费观看 | 欧美一级欧美一级高清 | 欧美高清无砖专区欧美精品 | 亚洲欧美激情日韩在线 | 我们中文在线观看免费完整版 | 国产男人天堂 | 日韩欧美亚洲天堂 |