Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Q {
- int n;
- boolean value = false;
- synchronized int get() {
- while(!value) {
- try {
- wait();
- } catch (InterruptedException e) {
- System.out.println("Getex1");
- }
- }
- System.out.println("Get " + n);
- value = false;
- notify();
- return n;
- }
- synchronized void put(int n) {
- while(value) {
- try {
- wait();
- } catch (InterruptedException e) {
- System.out.println("Getex2");
- }
- }
- this.n = n;
- value = true;
- System.out.println("Send " + n);
- notify();
- }
- }
- class Produser implements Runnable {
- Q q;
- Produser(Q q) {
- this.q = q;
- new Thread(this, "Postavwik").start();
- }
- public void run() {
- int i = 0;
- while(i < 20) {
- q.put(i++);
- }
- }
- }
- class Consumer implements Runnable {
- Q q;
- Consumer(Q q) {
- this.q = q;
- new Thread(this, "Potrebitel'").start();
- }
- public void run() {
- int i = 0;
- while(i < 20) {
- q.get();
- }
- }
- }
- class PC1 {
- public static void main(String args[]) {
- Q q = new Q();
- new Produser(q);
- new Consumer(q);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement