- ConcurrentLinkedQueue with wait() and notify()
- class StartPeriodicTask implements Runnable {
- public synchronized void run() {
- Robot robot = null;
- try {
- robot = new Robot();
- } catch (AWTException e1) {
- e1.printStackTrace();
- }
- Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
- .getScreenSize());
- BufferedImage image = robot.createScreenCapture(screenRect);
- if(null!=queue.peek()){
- try {
- System.out.println("Empty queue, so waiting....");
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }else{
- queue.add(image);
- notify();
- }
- }
- }
- public class ImageConsumer implements Runnable {
- @Override
- public synchronized void run() {
- while (true) {
- BufferedImage bufferedImage = null;
- if(null==queue.peek()){
- try {
- //Empty queue, so waiting....
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }else{
- bufferedImage = queue.poll();
- notify();
- }
- File imageFile = getFile();
- if (!imageFile.getParentFile().exists()) {
- imageFile.getParentFile().mkdirs();
- }
- try {
- ImageIO.write(bufferedImage, extension, imageFile);
- //Image saved
- catch (IOException e) {
- tracer.severe("IOException occurred. Image is not saved to file!");
- }
- }
- }
- BlockingQueue<?> queue = new SynchrnousQueue<?>();
- class StartPeriodicTask implements Runnable {
- public void run() {
- Robot robot = null;
- try {
- robot = new Robot();
- } catch (AWTException e1) {
- e1.printStackTrace();
- }
- Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
- .getScreenSize());
- BufferedImage image = robot.createScreenCapture(screenRect);
- queue.offer(image); //1
- }
- public class ImageConsumer implements Runnable {
- @Override
- public void run() {
- while (true) {
- BufferedImage bufferedImage = queue.poll(); //2
- File imageFile = getFile();
- if (!imageFile.getParentFile().exists()) {
- imageFile.getParentFile().mkdirs();
- }
- try {
- ImageIO.write(bufferedImage, extension, imageFile);
- //Image saved
- catch (IOException e) {
- tracer.severe("IOException occurred. Image is not saved to file!");
- }
- }
- if(null!=queue.peek()){ // You are the producer, you don't care if the queue is empty
- try {
- System.out.println("Empty queue, so waiting....");
- wait(); // This puts you to bed, your waiting and so is your consumer
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }else{
- queue.add(image);
- notify();
- }
- queue.add(image);
- notify();
- }else{
- bufferedImage = queue.poll();
- notify();
- }
- File imageFile = getFile();
- if (!imageFile.getParentFile().exists()) {
- imageFile.getParentFile().mkdirs();
- }
- try {
- ImageIO.write(bufferedImage, extension, imageFile);
- //Image saved
- catch (IOException e) {
- tracer.severe("IOException occurred. Image is not saved to file!");
- }
- }
- }else{
- bufferedImage = queue.poll();
- File imageFile = getFile();
- if (!imageFile.getParentFile().exists()) {
- imageFile.getParentFile().mkdirs();
- }
- try {
- ImageIO.write(bufferedImage, extension, imageFile);
- //Image saved
- catch (IOException e) {
- tracer.severe("IOException occurred. Image is not saved to file!");
- }
- }