您可能要考虑的一种选择是对多个线程进行处理.一个线程可以专用于截屏,而其他许多线程可以写入磁盘.由于写入磁盘不是占用大量CPU的操作,因此您可以让它们中的许多并发运行,每个写入一个不同的文件.以下程序在具有512M堆大小的计算机上可以正常运行:

import javax.imageio.ImageIO;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.Date;

import java.util.Timer;

import java.util.TimerTask;

import java.util.concurrent.*;

import java.util.concurrent.atomic.AtomicInteger;

public class ImageWritingMain

{

public static void main(String[] args) throws Exception

{

// a queue

final BlockingQueue queue =

new LinkedBlockingQueue();

// schedule a thread to take 20 images per second and put them in

// the queue

int fps = 20;

final ScreenShotRecorder recorder =

new ScreenShotRecorder(new Robot(), queue);

Timer timer = new Timer();

timer.scheduleAtFixedRate(recorder, 0, (1000L/fps));

// make a directory to hold the screenshot images

String id = new Date().toString().replace(' ', '-').replace(':', '-');

File imageDir = new File("images-" + id);

imageDir.mkdirs();

// start 10 threads, and each thread reads from the queue and

// writes the image to a file

int nWriterThreads = 10;

ExecutorService threadPool = Executors.newFixedThreadPool(nWriterThreads);

for (int i = 0; i < nWriterThreads; i++)

{

ImageWriter task = new ImageWriter(queue, imageDir);

threadPool.submit(task);

}

System.out.println("Started all threads ..");

// wait as long as you want the program to run (1 minute, for example) ...

Thread.sleep(60 * 1000L);

// .. and shutdown the threads

System.out.println("Shutting down all threads");

threadPool.shutdownNow();

timer.cancel();

if (! queue.isEmpty())

{

System.out.println("Writing " + queue.size() + " remaining images");

// write the remaining images to disk in the main thread

ImageWriter writer = new ImageWriter(queue, imageDir);

BufferedImage img = null;

while ((img = queue.poll()) != null)

{

writer.writeImageToFile(img);

}

}

}

}

class ScreenShotRecorder extends TimerTask

{

private static final Rectangle screenRect =

new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

private static final AtomicInteger counter = new AtomicInteger();

private final Robot robot;

private final BlockingQueue imageQueue;

ScreenShotRecorder(Robot robot, BlockingQueue imageQueue)

{

this.robot = robot;

this.imageQueue = imageQueue;

}

@Override

public void run()

{

try

{

BufferedImage image = robot.createScreenCapture(screenRect);

imageQueue.put(image);

System.out.println(Thread.currentThread() +

": Took screenshot #" + counter.incrementAndGet());

}

catch (InterruptedException e)

{

System.out.println("Finishing execution of " + Thread.currentThread());

return;

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

class ImageWriter implements Runnable

{

private static final AtomicInteger counter = new AtomicInteger();

private final BlockingQueue imageQueue;

private final File dir;

ImageWriter(BlockingQueue imageQueue, File dir)

{

this.imageQueue = imageQueue;

this.dir = dir;

}

@Override

public void run()

{

while (true)

{

try

{

BufferedImage image = imageQueue.take();

writeImageToFile(image);

}

catch (InterruptedException e)

{

System.out.println("Finishing execution of " + Thread.currentThread());

return;

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

public void writeImageToFile(BufferedImage image) throws IOException

{

File file = new File(dir, "screenshot-" + counter.incrementAndGet());

ImageIO.write(image, "JPG", file);

System.out.println(Thread.currentThread() +

": Wrote " + file.getCanonicalPath());

}

}

java获取磁盘读取速度_记录屏幕Java磁盘速度相关推荐

  1. java 获取 反射 方法 名_乐字节Java反射之一:反射概念与获取反射源头Class

    一.Java反射机制概念 "程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言",如Python, Ruby是动态语言:显然C++,Java,C#不是动态语言,但是JAV ...

  2. java 文件流读取文本_如何在Java 8中处理流和读取文本文件

    java 文件流读取文本 我已经使用最新的Java8转换了一个旧的实用程序类. 我经常使用它来打印清单文件的内容,以检查任何神秘的jar文件的版本等.只需运行" java ztools.Pr ...

  3. java 获取文件扩展名_如何在Java中获取文件扩展名

    java 获取文件扩展名 Sometimes while working with files, we need to process them differently based on their ...

  4. java 获取时区的时间_如何使用Java获取时区的当前日期和时间?

    如何使用Java获取时区的当前日期和时间? 我的应用托管在伦敦服务器中. 我在西班牙马德里. 因此,时区为-2小时. 如何获取带有时区的当前日期/时间. Date curr_date = new Da ...

  5. java获取文件hash值_怎样用java获取到文件的hash值?

    public static byte[] createChecksum(String filename) throws Exception { InputStream fis = new FileIn ...

  6. java 线程中创建线程_如何在Java 8中创建线程安全的ConcurrentHashSet?

    java 线程中创建线程 在JDK 8之前,还没有办法在Java中创建大型的线程安全的ConcurrentHashSet. java.util.concurrent包甚至没有一个名为Concurren ...

  7. java获取tomcat启动时间不对_部署在Tomcat 服务器中的web应用读取时间与系统时间不一致问题...

    我在部署应用到Ubantu系统上的tomcat服务器中运行,发现操作系统的时间和tomcat中的应用程序获取的时间不一致,总是相差8个小时,但是查看当前操作系统的时区也是CST时区(中国标准时区). ...

  8. java 获取所有及假日_电子商务网站所有者假日销售指南

    java 获取所有及假日 If this is the first time you've thought seriously about holiday sales, you're already ...

  9. easyloging 获取日志文件名字_愉快地学Java语言:第十五章 断言与日志

    导读 本文适合Java入门,不太适合Java中高级软件工程师.本文以<Java核心技术基础知识卷I>第10版为蓝本,采用不断提出问题,然后解答问题的方式来讲述.本篇文章只是这个系列中的一篇 ...

最新文章

  1. 超详细 Nginx 极简教程,傻瓜一看也会!
  2. gorm配置logger显示执行的sql
  3. 要让Fiddler能够监控加密过后的HTTPS请求,需要执行哪些步骤?
  4. wget整站抓取、网站抓取功能;下载整个网站;下载网站到本地
  5. linux的自定义input,linux键值到Android键值的转换与自定义
  6. OpenStack 如何跨版本升级
  7. android联系人添加公司,android添加联系人(直接添加到联系人数据库)
  8. java输出string变量名_java – 从String获取名称变量
  9. webstorm如何自定义代码模板
  10. sis9280触摸ic 基于rk3288 的安卓4.4的 多点触摸
  11. 看书学python靠谱吗_自学Python靠谱吗?
  12. 【层级多标签文本分类】融合标签层级结构的文本分类
  13. 有DMX512协议控制的整套硬件解决方案吗?来看一下,舞台灯光同步视频播放DMX控制台
  14. 修复IE主页被篡改劫持的方法之一:自定义xxx.reg文件
  15. 光驱叹号,我的电脑看不见光驱
  16. Android进入欢迎界面前显示黑乎乎的或者白白的布局
  17. 鹰眸安全帽识别系统对施工现场管理水平的提升
  18. 2021年两次系统集成项目管理工程师真题各章节占分比对比
  19. python到底有多牛!用Python开源机器人和5美元,我在Instagram上搞到了2500个真粉儿
  20. 【模块】更新ESP32连接PS4手柄,PS4手柄控制遥控车,遥控车控制方案

热门文章

  1. JAVA SE 基础复习-IO与序列化(4)
  2. cmd下添加删除启动项
  3. 论文笔记|Self-Supervised Test-Time Learning for Reading Comprehension
  4. 深度学习 14. 深度学习调参,CNN参数调参,各个参数理解和说明以及调整的要领。underfitting和overfitting的理解,过拟合的解释。
  5. 浪潮服务器系统下收集日志,浪潮:打卡了解Venus 详解这款OpenStack日志管理项目...
  6. ROS: Publisher and Subscriber
  7. RGB图像转三通道灰度图像
  8. 51Nod-1289 大鱼吃小鱼
  9. 这8大信用卡秘密!银行绝不会说
  10. pandas数据清洗:drop函数案例详解、dropna函数案例详解、drop_duplicates函数案例详解