Java 食唔盡個CPU ?

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;

  8. public class Runner {
  9.     public static void main(String[] args) {

  10.         ExecutorService newCachedThreadPool= Executors.newCachedThreadPool();
  11.         //ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(500);
  12.         ArrayList<String> testlist;
  13.         File file = new File("C:\\test.txt");
  14.         BufferedReader reader = null;

  15.         try {
  16.             reader = new BufferedReader(new FileReader(file));
  17.             String tempString = null;
  18.             Integer line = 0;
  19.             testlist= new ArrayList<String>();
  20.             while ((tempString = reader.readLine()) != null) {
  21.                 portlist.add(tempString);
  22.                 line++;
  23.             }
  24.             reader.close();
  25.             System.out.println(testlist.size());
  26.             for(int i=0;i<testlist.size();i++){
  27.                 final String tmpString = testlist.get(i);
  28.                 newCachedThreadPool.execute(new Runnable() {
  29.                     @Override
  30.                     public void run() {
  31.                         try {
  32.                             TestProgram a = new TestProgram();
  33.                             a.start(tmpString);
  34.                         } catch (InterruptedException e) {
  35.                             System.out.println("exception");
  36.                         } catch (Exception e) {
  37.                             e.printStackTrace();
  38.                         }
  39.                     }
  40.                 });
  41.             }
  42.         } catch (IOException e) {
  43.             e.printStackTrace();
  44.         } finally {
  45.             if (reader != null) {
  46.                 try {
  47.                     reader.close();
  48.                 } catch (IOException e1) {
  49.                     e1.printStackTrace();
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
複製代碼
無論用newFixedThreadPool 定 newCachedThreadPool,CPU使用率都係幾%,點解決?
似乎好似剩係用左單核咁樣樣,code 中 test.txt 為600行,即New 左TestProgram Class六百個同時執行,都係冇乜CPU使用率

你個TestProgram 行咩先?
行唔行得盡睇你Coding 食幾多resource..

無論用newFixedThreadPool 定 newCachedThreadPool,CPU使用率都係幾%,點解決?
似乎好似剩係用左單核咁樣 ...
geffon 發表於 2018-12-29 18:15

TOP

你個TestProgram 行咩先?
行唔行得盡睇你Coding 食幾多resource..
vichui 發表於 2018-12-29 18:25


行HTTPClient 大量 GET POST
同時行600—1000個
newFixedThreadPool 無論教幾多 都係卡下卡下
newCachedThreadPool 都卡下卡下

TOP

http get/post 大部份時間係等資料

TOP

咁呢個係network唔夠快,唔關CPU事, network 可以係自己, 可以係Internet...

http get/post 大部份時間係等資料
rabbit82047 發表於 2018-12-29 19:55

TOP

咁呢個係network唔夠快,唔關CPU事, network 可以係自己, 可以係Internet...
vichui 發表於 2018-12-29 20:42


重點係,成個PROGRAM係IO INTENSIVE,所以根本唔會用到好快嘅CPU
就算你個NETWORK 快到好似HDD,甚至SSD咁快,一樣唔會用到好多CPU

TOP

一睇你catch io exception 就知唔係 cpu intensive process, 食得幾多cpu  
你想食cpu 咪寫d cpu intensive process, 例如做挖礦果d sha256  hash運算囉

無論用newFixedThreadPool 定 newCachedThreadPool,CPU使用率都係幾%,點解決?
似乎好似剩係用左單核咁樣 ...
geffon 發表於 2018-12-29 18:15

TOP

我都寫過類似 program, 但network通常係bottleneck (用屋企個100Mb/s)
其實java multithreading係用得盡 CPU 的

TOP

Java用thread pool開600條thread 真係玩自己, 建議用Java NIO或Node.js呢d event driven既方法

TOP

行HTTPClient 大量 GET POST
同時行600—1000個
newFixedThreadPool 無論教幾多 都係卡下卡下
n ...
geffon 發表於 2018-12-29 18:33


    自己開 VisualVM 睇啦  

用JVM 其中一大原因係因為 Debugging tool 發展得好

via HKEPC IR Pro 3.4.0 - iOS(2.3.3)

TOP