0

I want to write a program that captures parts of my screen. In order to improve the number of pictures taken per second, I use 4 threads. My threads look like this:

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);
        }
    }
}

}

With different numbers for the rectangle object in each thread. I call this 4 times so i can get the most out of my i5 processor. However when i try to run it, the cpu usage is at about 30%. If I fill the threads with while(true){} I get 100% usage. Does this mean code cant be run parallel ? If so, what can I do to execute it parallel?

xingbin
  • 27,410
  • 9
  • 53
  • 103
krise
  • 485
  • 1
  • 10
  • 22

1 Answers1

0

You program is working in parallel. But CPU is not the only bottleneck of your program, while I/O is the real bottleneck of your program.

I'm not an expert about screen capture program, but I think maybe the I/O operation performed by such as BufferedImage is the reason why your CPU usage about 30%, because CPU is spending time on waiting I/O.

xingbin
  • 27,410
  • 9
  • 53
  • 103