分布与并行计算—用任务管理器画CPU正弦曲线(Java)
生活随笔
收集整理的這篇文章主要介紹了
分布与并行计算—用任务管理器画CPU正弦曲线(Java)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
class drawSin implements Runnable{@Overridepublic void run() {final double SPLIT = 0.01;// 角度的分割final int COUNT = (int) (2 / SPLIT);// 2PI分割的次數(shù),也就是2/0.01個(gè),正好是一周final double PI = Math.PI;final int interval = 100;// 時(shí)間間隔long[] busy = new long[COUNT];//線程運(yùn)行時(shí)間long[] idle= new long[COUNT];//線程休眠時(shí)間int half = interval / 2;double x = 0.0;for (int i = 0; i < COUNT; i++) {busy[i] = (long) (half + (Math.sin(PI * x) * half))/2;idle[i] = interval - busy[i];x += SPLIT;}long start = 0;int j = 0;while (true){j = j % COUNT;start = System.currentTimeMillis();while (System.currentTimeMillis() - start < busy[j]) ;try {Thread.sleep(idle[j]);} catch (InterruptedException e) {e.printStackTrace();}j++;}}
}
public class cpuSin {public static void main(String[] args) throws Exception {Runtime runtime = Runtime.getRuntime();//獲得當(dāng)前系統(tǒng)的CPU數(shù)量,根據(jù)這個(gè)數(shù)值創(chuàng)建對(duì)應(yīng)數(shù)量的線程ExecutorService executorService = Executors.newFixedThreadPool(runtime.availableProcessors());for (int i = 0;i < runtime.availableProcessors();i++){//每個(gè)核心都上一個(gè)線程executorService.execute(new drawSin());}executorService.shutdown();}
}
總結(jié)
以上是生活随笔為你收集整理的分布与并行计算—用任务管理器画CPU正弦曲线(Java)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到买鱼竿什么意思
- 下一篇: 分布与并行计算—生命游戏(Java)