Java并發(fā)程序入門(mén)介紹
今天看了看Java并發(fā)程序,寫(xiě)一寫(xiě)入門(mén)程序,并設(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)境的配置文件
,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í)器功能,不基于特定的基類的方法,需要的朋友可以參考下2018-04-04
簡(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)配置文件加解密原理,文中通過(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),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
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
如何優(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í)例
最近在公司項(xiàng)目開(kāi)發(fā)中遇到批量數(shù)據(jù)插入或者更新,下面這篇文章主要給大家介紹了關(guān)于MyBatis實(shí)現(xiàn)批量插入的相關(guān)資料,需要的朋友可以參考下2022-10-10
Java多線程并發(fā)開(kāi)發(fā)之DelayQueue使用示例
這篇文章主要為大家詳細(xì)介紹了Java多線程并發(fā)開(kāi)發(fā)之DelayQueue使用示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

