与机器人的并行screencapture

时间:2018-02-04 16:09:33

标签: java multithreading runtime screen-capture

我想编写一个捕获部分屏幕的程序。为了改善每秒拍摄的照片数量,我使用了4个线程。我的主题看起来像这样:

class Sub1 extends Thread{
public void run(){
    Rectangle screenRect1 = new Rectangle(0,0,89,864);
    for(int i = 0; i<1000; i++) {
        try {
            Robot robot = new Robot();
            BufferedImage screenLeft = robot.createScreenCapture(screenRect1);
        } catch (AWTException ex) {
            System.err.println(ex);
        }
    }
}

}

每个线程中矩形对象的编号不同。 我这叫了4次,所以我可以充分利用我的i5处理器。但是,当我尝试运行它时,CPU使用率约为30%。如果我用while(true){}填充线程,我得到100%的使用率。这是否意味着代码无法并行运行?如果是这样,我可以做什么来并行执行它?

1 个答案:

答案 0 :(得分:0)

你的程序正在并行工作。 But CPU is not the only bottleneck of your program, while I/O is the real bottleneck of your program

我不是关于屏幕捕获程序的专家,但我认为可能由BufferedImage执行的I / O操作是你的CPU使用率大约30%的原因,因为 CPU花时间等待I / O