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)境的配置文件
,springboot支持通過不同的profile來配置不同環(huán)境的配置,下面就大致介紹一下yml配置文件跟properties配置文件怎么使用profile配置不同環(huán)境的配置文件2018-01-01使用spring整合Quartz實(shí)現(xiàn)—定時器功能
這篇文章主要介紹了使用spring整合Quartz實(shí)現(xiàn)—定時器功能,不基于特定的基類的方法,需要的朋友可以參考下2018-04-04Spring Boot 實(shí)現(xiàn)配置文件加解密原理
這篇文章主要介紹了Spring Boot 實(shí)現(xiàn)配置文件加解密原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06SpringBoot SSE服務(wù)端主動推送事件的實(shí)現(xiàn)
本文主要介紹了SpringBoot SSE服務(wù)端主動推送事件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java獲取當(dāng)前系統(tǒng)事件System.currentTimeMillis()方法
下面小編就為大家?guī)硪黄狫ava獲取當(dāng)前系統(tǒng)事件System.currentTimeMillis()方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06如何優(yōu)雅的拋出Spring Boot注解的異常詳解
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12MyBatis實(shí)現(xiàn)批量插入方法實(shí)例
最近在公司項(xiàng)目開發(fā)中遇到批量數(shù)據(jù)插入或者更新,下面這篇文章主要給大家介紹了關(guān)于MyBatis實(shí)現(xiàn)批量插入的相關(guān)資料,需要的朋友可以參考下2022-10-10Java多線程并發(fā)開發(fā)之DelayQueue使用示例
這篇文章主要為大家詳細(xì)介紹了Java多線程并發(fā)開發(fā)之DelayQueue使用示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09