分布与并行计算—用任务管理器画CPU正弦曲线(Java)
生活随笔
收集整理的這篇文章主要介紹了
分布与并行计算—用任务管理器画CPU正弦曲线(Java)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
class drawSin implements Runnable{@Overridepublic void run() {final double SPLIT = 0.01;// 角度的分割final int COUNT = (int) (2 / SPLIT);// 2PI分割的次數,也就是2/0.01個,正好是一周final double PI = Math.PI;final int interval = 100;// 時間間隔long[] busy = new long[COUNT];//線程運行時間long[] idle= new long[COUNT];//線程休眠時間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();//獲得當前系統的CPU數量,根據這個數值創建對應數量的線程ExecutorService executorService = Executors.newFixedThreadPool(runtime.availableProcessors());for (int i = 0;i < runtime.availableProcessors();i++){//每個核心都上一個線程executorService.execute(new drawSin());}executorService.shutdown();}
}
總結
以上是生活随笔為你收集整理的分布与并行计算—用任务管理器画CPU正弦曲线(Java)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到买鱼竿什么意思
- 下一篇: 梦到娘家房子漏水是什么预兆