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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新評論