Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 4.45 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ConcurrentLinkedQueue with wait() and notify()
  2. class StartPeriodicTask implements Runnable {
  3.     public synchronized void run() {
  4.         Robot robot = null;
  5.         try {
  6.             robot = new Robot();
  7.         } catch (AWTException e1) {
  8.             e1.printStackTrace();
  9.         }
  10.         Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
  11.                 .getScreenSize());
  12.         BufferedImage image = robot.createScreenCapture(screenRect);
  13.         if(null!=queue.peek()){
  14.             try {
  15.                 System.out.println("Empty queue, so waiting....");
  16.                 wait();
  17.             } catch (InterruptedException e) {
  18.                 e.printStackTrace();
  19.             }
  20.         }else{
  21.             queue.add(image);
  22.             notify();
  23.         }
  24.     }
  25. }
  26.  
  27. public class ImageConsumer implements Runnable {
  28.         @Override
  29.         public synchronized void run() {
  30.             while (true) {
  31.                 BufferedImage bufferedImage = null;
  32.                 if(null==queue.peek()){
  33.                     try {
  34.                         //Empty queue, so waiting....
  35.                         wait();
  36.                     } catch (InterruptedException e) {
  37.                         e.printStackTrace();
  38.                     }
  39.                 }else{
  40.                     bufferedImage = queue.poll();
  41.                     notify();
  42.                 }
  43.                 File imageFile = getFile();
  44.                 if (!imageFile.getParentFile().exists()) {
  45.                     imageFile.getParentFile().mkdirs();
  46.                 }
  47.                     try {
  48.                         ImageIO.write(bufferedImage, extension, imageFile);
  49.                         //Image saved
  50.                     catch (IOException e) {
  51.                         tracer.severe("IOException occurred. Image is not saved to file!");
  52.                     }
  53.                 }
  54.             }
  55.        
  56. BlockingQueue<?> queue = new SynchrnousQueue<?>();
  57. class StartPeriodicTask implements Runnable {
  58.     public void run() {
  59.         Robot robot = null;
  60.         try {
  61.             robot = new Robot();
  62.         } catch (AWTException e1) {
  63.             e1.printStackTrace();
  64.         }
  65.         Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit()
  66.                 .getScreenSize());
  67.         BufferedImage image = robot.createScreenCapture(screenRect);
  68.         queue.offer(image); //1
  69. }
  70. public class ImageConsumer implements Runnable {
  71.         @Override
  72.         public void run() {
  73.             while (true) {
  74.                 BufferedImage bufferedImage = queue.poll(); //2
  75.  
  76.                 File imageFile = getFile();
  77.                 if (!imageFile.getParentFile().exists()) {
  78.                     imageFile.getParentFile().mkdirs();
  79.                 }
  80.                     try {
  81.                         ImageIO.write(bufferedImage, extension, imageFile);
  82.                         //Image saved
  83.                     catch (IOException e) {
  84.                         tracer.severe("IOException occurred. Image is not saved to file!");
  85.                     }
  86.             }
  87.        
  88. if(null!=queue.peek()){ // You are the producer, you don't care if the queue is empty
  89.         try {
  90.             System.out.println("Empty queue, so waiting....");
  91.             wait(); // This puts you to bed, your waiting and so is your consumer
  92.         } catch (InterruptedException e) {
  93.             e.printStackTrace();
  94.         }
  95.     }else{
  96.         queue.add(image);
  97.         notify();
  98.     }
  99.        
  100. queue.add(image);
  101.         notify();
  102.        
  103. }else{
  104.                 bufferedImage = queue.poll();
  105.                 notify();
  106.             }
  107.             File imageFile = getFile();
  108.             if (!imageFile.getParentFile().exists()) {
  109.                 imageFile.getParentFile().mkdirs();
  110.             }
  111.                 try {
  112.                     ImageIO.write(bufferedImage, extension, imageFile);
  113.                     //Image saved
  114.                 catch (IOException e) {
  115.                     tracer.severe("IOException occurred. Image is not saved to file!");
  116.                 }
  117.             }
  118.        
  119. }else{
  120.                 bufferedImage = queue.poll();
  121.  
  122.                 File imageFile = getFile();
  123.                 if (!imageFile.getParentFile().exists()) {
  124.                    imageFile.getParentFile().mkdirs();
  125.                 }
  126.  
  127.                 try {
  128.                     ImageIO.write(bufferedImage, extension, imageFile);
  129.                     //Image saved
  130.                 catch (IOException e) {
  131.                     tracer.severe("IOException occurred. Image is not saved to file!");
  132.                 }
  133.             }