Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. import java.io.*;
  2.  
  3. public class Third {
  4.  
  5.     static boolean end = false;
  6.  
  7.     static class Product {
  8.         Character c;
  9.  
  10.         boolean isUsed() {return c == null;}
  11.         boolean isReady() {return c != null;}
  12.     }
  13.  
  14.     static class Consumer extends Thread {
  15.         private final Product product;
  16.         PrintWriter pw;
  17.  
  18.         public Consumer(Product product) {
  19.             this.product = product;
  20.             try {
  21.                 pw = new PrintWriter(new FileOutputStream(new File("output.txt")));
  22.             } catch (FileNotFoundException e) {
  23.                 throw new IllegalStateException(e);
  24.             }
  25.         }
  26.  
  27.         @Override
  28.         public void run() {
  29.             while (!end) {
  30.                 synchronized (product) {
  31.                     while (!product.isReady()) {
  32.                         try {
  33.                             if(end) break;
  34.                             product.wait();
  35.                         } catch (InterruptedException e) {
  36.                             e.printStackTrace();
  37.                         }
  38.                     }
  39.                     useProduct();
  40.                     product.notify();
  41.                 }
  42.             }
  43.         }
  44.  
  45.         void useProduct(){
  46.             if(product.c == null) return;
  47.             pw.print(product.c);
  48.             product.c = null;
  49.             pw.flush();
  50.         }
  51.     }
  52.  
  53.     static class Producer  extends Thread {
  54.  
  55.         private final Product product;
  56.         Reader r;
  57.  
  58.         public Producer(Product product) {
  59.             this.product = product;
  60.             try {
  61.                 r = new InputStreamReader(new FileInputStream(new File("input.txt")));
  62.             } catch (FileNotFoundException e) {
  63.                 e.printStackTrace();
  64.             }
  65.         }
  66.  
  67.         @Override
  68.         public void run() {
  69.             while (!end){
  70.                 synchronized (product){
  71.                     while (!product.isUsed()) {
  72.                         try {
  73.                             if(end) break;
  74.                             product.wait();
  75.                         } catch (InterruptedException e) {
  76.                             e.printStackTrace();
  77.                         }
  78.                     }
  79.                     buildProduct();
  80.                     product.notify();
  81.                 }
  82.             }
  83.         }
  84.  
  85.         void buildProduct(){
  86.             int in = 0;
  87.             try {
  88.                 in = r.read();
  89.             } catch (IOException e) {
  90.                 e.printStackTrace();
  91.             }
  92.             if(in == -1) {
  93.                 end = true;
  94.                 return;
  95.             }
  96.             product.c = (char) in;
  97.         }
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         Product p = new Product();
  102.         Consumer c = new Consumer(p);
  103.         Producer pr = new Producer(p);
  104.  
  105.         c.start();
  106.         pr.start();
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement