亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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)悍,所以先開(kāi)始并沒(méi)看到并發(fā)的效果,感覺(jué)是按順序執(zhí)行的,所以在中間加了浮點(diǎn)數(shù)的運(yùn)算來(lái)延遲時(shí)間。

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

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)文章

  • Spring Boot使用profile如何配置不同環(huán)境的配置文件

    Spring Boot使用profile如何配置不同環(huán)境的配置文件

    ,springboot支持通過(guò)不同的profile來(lái)配置不同環(huán)境的配置,下面就大致介紹一下yml配置文件跟properties配置文件怎么使用profile配置不同環(huán)境的配置文件
    2018-01-01
  • 使用spring整合Quartz實(shí)現(xiàn)—定時(shí)器功能

    使用spring整合Quartz實(shí)現(xiàn)—定時(shí)器功能

    這篇文章主要介紹了使用spring整合Quartz實(shí)現(xiàn)—定時(shí)器功能,不基于特定的基類的方法,需要的朋友可以參考下
    2018-04-04
  • 簡(jiǎn)單了解Java多線程實(shí)現(xiàn)的四種方式

    簡(jiǎn)單了解Java多線程實(shí)現(xiàn)的四種方式

    這篇文章主要介紹了簡(jiǎn)單了解Java多線程實(shí)現(xiàn)的四種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Spring Boot 實(shí)現(xiàn)配置文件加解密原理

    Spring Boot 實(shí)現(xiàn)配置文件加解密原理

    這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)配置文件加解密原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • SpringBoot SSE服務(wù)端主動(dòng)推送事件的實(shí)現(xiàn)

    SpringBoot SSE服務(wù)端主動(dòng)推送事件的實(shí)現(xiàn)

    本文主要介紹了SpringBoot SSE服務(wù)端主動(dòng)推送事件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java獲取當(dāng)前系統(tǒng)事件System.currentTimeMillis()方法

    Java獲取當(dāng)前系統(tǒng)事件System.currentTimeMillis()方法

    下面小編就為大家?guī)?lái)一篇Java獲取當(dāng)前系統(tǒng)事件System.currentTimeMillis()方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • Java 內(nèi)部類的定義與范例

    Java 內(nèi)部類的定義與范例

    說(shuō)起內(nèi)部類這個(gè)詞,想必很多人都不陌生,但是又會(huì)覺(jué)得不熟悉。原因是平時(shí)編寫代碼時(shí)可能用到的場(chǎng)景不多,用得最多的是在有事件監(jiān)聽(tīng)的情況下,并且即使用到也很少去總結(jié)內(nèi)部類的用法。今天我們就來(lái)一探究竟
    2021-11-11
  • 如何優(yōu)雅的拋出Spring Boot注解的異常詳解

    如何優(yōu)雅的拋出Spring Boot注解的異常詳解

    這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • MyBatis實(shí)現(xiàn)批量插入方法實(shí)例

    MyBatis實(shí)現(xiàn)批量插入方法實(shí)例

    最近在公司項(xiàng)目開(kāi)發(fā)中遇到批量數(shù)據(jù)插入或者更新,下面這篇文章主要給大家介紹了關(guān)于MyBatis實(shí)現(xiàn)批量插入的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Java多線程并發(fā)開(kāi)發(fā)之DelayQueue使用示例

    Java多線程并發(fā)開(kāi)發(fā)之DelayQueue使用示例

    這篇文章主要為大家詳細(xì)介紹了Java多線程并發(fā)開(kāi)發(fā)之DelayQueue使用示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09

最新評(píng)論