Advertisement
Guest User

Untitled

a guest
Jun 21st, 2016
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. class Q {
  2.     int n;
  3.     boolean value = false;
  4.    
  5.     synchronized int get() {
  6.         while(!value) {
  7.             try {
  8.                 wait();
  9.             } catch (InterruptedException e) {
  10.                 System.out.println("Getex1");
  11.             }
  12.         }
  13.         System.out.println("Get " + n);
  14.         value = false;
  15.         notify();      
  16.         return n;      
  17.     }
  18.    
  19.     synchronized void put(int n) {
  20.         while(value) {
  21.             try {
  22.                 wait();
  23.             } catch (InterruptedException e) {
  24.                 System.out.println("Getex2");
  25.             }  
  26.         }
  27.         this.n = n;
  28.         value = true;
  29.         System.out.println("Send " + n);
  30.         notify();
  31.     }
  32. }
  33.  
  34. class Produser implements Runnable {
  35.     Q q;
  36.    
  37.     Produser(Q q) {
  38.         this.q = q;
  39.         new Thread(this, "Postavwik").start();
  40.     }
  41.    
  42.     public void run() {
  43.         int i = 0;
  44.        
  45.         while(i < 20) {
  46.             q.put(i++);
  47.         }
  48.     }
  49. }
  50.  
  51. class Consumer implements Runnable {
  52.     Q q;
  53.    
  54.     Consumer(Q q) {
  55.         this.q = q;
  56.         new Thread(this, "Potrebitel'").start();
  57.     }
  58.    
  59.     public void run() {
  60.         int i = 0;
  61.         while(i < 20) {
  62.             q.get();
  63.         }
  64.     }
  65. }
  66.  
  67. class PC1 {
  68.     public static void main(String args[]) {
  69.         Q q = new Q();
  70.         new Produser(q);
  71.         new Consumer(q);       
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement