最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年03月26日 21:11:51   作者:Microgoogle  
這篇文章主要介紹了Java并發(fā)程序入門 ,需要的朋友可以參考下

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

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();
    }
  }
}

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

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

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();
  }
}

相關(guān)文章

  • SpringBoot2.3集成ELK7.1.0的示例代碼

    SpringBoot2.3集成ELK7.1.0的示例代碼

    這篇文章主要介紹了SpringBoot2.3集成ELK7.1.0的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 5分鐘搞定java單例模式

    5分鐘搞定java單例模式

    單例模式(Singleton?Pattern)是?Java?中最簡單的設(shè)計(jì)模式之一。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式,本文給大家介紹下java單例模式的相關(guān)知識,感興趣的朋友一起看看吧
    2022-03-03
  • 基于SpringBoot和Vue3的博客平臺發(fā)布、編輯、刪除文章功能實(shí)現(xiàn)

    基于SpringBoot和Vue3的博客平臺發(fā)布、編輯、刪除文章功能實(shí)現(xiàn)

    在上一個教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring?Boot和Vue3的用戶注冊與登錄功能。本教程將繼續(xù)引導(dǎo)您實(shí)現(xiàn)博客平臺的發(fā)布、編輯、刪除文章功能,需要的朋友參考一下
    2023-04-04
  • SpringBoot使用POI進(jìn)行Excel下載

    SpringBoot使用POI進(jìn)行Excel下載

    這篇文章主要為大家詳細(xì)介紹了SpringBoot使用POI進(jìn)行Excel下載,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • SpringBoot獲取yml和properties配置文件的內(nèi)容

    SpringBoot獲取yml和properties配置文件的內(nèi)容

    這篇文章主要為大家詳細(xì)介紹了SpringBoot獲取yml和properties配置文件的內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • SpringBoot中的WebSocketSession原理詳解

    SpringBoot中的WebSocketSession原理詳解

    這篇文章主要介紹了SpringBoot中的WebSocketSession原理詳解,傳統(tǒng)的?HTTP?協(xié)議是無法支持實(shí)時通信的,因?yàn)樗且环N無狀態(tài)協(xié)議,每次請求都是獨(dú)立的,無法保持連接。為了解決這個問題,WebSocket?協(xié)議被引入,需要的朋友可以參考下
    2023-07-07
  • mybatisplus下劃線駝峰轉(zhuǎn)換的問題解決

    mybatisplus下劃線駝峰轉(zhuǎn)換的問題解決

    在mybatis-plus中,下劃線-駝峰自動轉(zhuǎn)換可能導(dǎo)致帶下劃線的字段查詢結(jié)果為null,本文就來介紹一下mybatisplus下劃線駝峰轉(zhuǎn)換的問題解決,感興趣的可以了解一下
    2024-10-10
  • Java處理科學(xué)計(jì)數(shù)法數(shù)字方式

    Java處理科學(xué)計(jì)數(shù)法數(shù)字方式

    這篇文章主要介紹了Java處理科學(xué)計(jì)數(shù)法數(shù)字方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 對SpringBoot項(xiàng)目Jar包進(jìn)行加密防止反編譯

    對SpringBoot項(xiàng)目Jar包進(jìn)行加密防止反編譯

    最近項(xiàng)目要求部署到其他公司的服務(wù)器上,但是又不想將源碼泄露出去,要求對正式環(huán)境的啟動包進(jìn)行安全性處理,防止客戶直接通過反編譯工具將代碼反編譯出來,本文介紹了如何對SpringBoot項(xiàng)目Jar包進(jìn)行加密防止反編譯,需要的朋友可以參考下
    2023-10-10
  • Java從源碼角度解析SpringMVC執(zhí)行流程

    Java從源碼角度解析SpringMVC執(zhí)行流程

    這篇文章主要介紹了Java從源碼角度解析SpringMVC執(zhí)行流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評論

盘山县| 承德市| 郓城县| 澎湖县| 新田县| 库尔勒市| 彩票| 葫芦岛市| 成都市| 双柏县| 砚山县| 称多县| 贡觉县| 蒙自县| 蒙山县| 郓城县| 台安县| 托里县| 万山特区| 化州市| 宁国市| 涞水县| 科技| 铜川市| 宾阳县| 双鸭山市| 新河县| 丘北县| 济宁市| 当涂县| 门源| 揭西县| 灵丘县| 盐亭县| 平陆县| 平南县| 藁城市| 林州市| 洱源县| 乌拉特中旗| 宣恩县|