Question

I have a few threads running, which are all required to constantly take pictures of different areas of screen. Each thread has its own instance of Robot. The problem is, rather than the shots being taken asynchronously by each thread, they seem to be taken one at a time.

E.g, if I take only one screenshot, it only takes about 20-30 milliseconds. But if I have 5 threads and they all try to take screenshots (of different parts of the screen), then it seems to take 100 - 150 milliseconds to get all 5 screenshots, clearly showing that the shots are being taken synchronously despite the robot.createScreenCapture() method being called from different threads. I want all 5 screenshots to be taken in 20-30 milliseconds.

Is there a way to make robot work asynchronously and send the screenshots to each thread simultaneously? Or a different method of taking screenshots asynchronously which will work in Windows?

Was it helpful?

Solution

At least in the OpenJDK implementation, createScreenCapture is synchronized because it's using the underlying OS calls to read the screen buffer directly, so no, it's explicitly impossible to do so. Presumably the underlying graphics primitives either aren't or can't be guaranteed to be reentrant.

OTHER TIPS

I don't agree with the solution.

createScreenCapture is synchronized but you can create two or more instances of the awt robot and use them in a ScheduledExecutorService to have the parallelization needed.

Writing my own screen capture software right now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top