Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Q {
- boolean val = false;
- synchronized void values(int n) {
- if (!val){
- try {
- wait();
- } catch (Exception e) {}
- }
- for (int i = n; i < n+5; ++i) System.out.println(i);
- val = false;
- notify();
- }
- synchronized void text(String s) {
- if (val)
- try {
- wait();
- } catch (Exception e) {}
- System.out.println(s);
- val = true;
- notify();
- }
- }
- class producer implements Runnable {
- Q q;
- Thread t;
- boolean live;
- producer (Q q) {
- live = true;
- Thread t = new Thread(this);
- this.q = q;
- t.start();
- }
- public void run () {
- q.text("BEFORE");
- q.text("AFTER");
- }
- }
- class consumer implements Runnable {
- Q q;
- Thread t;
- boolean live;
- consumer (Q q) {
- live = true;
- t = new Thread(this);
- this.q = q;
- t.start();
- }
- public void run () {
- q.values(0);
- q.values(5);
- }
- }
- public class app {
- public static void main(String args[]) {
- Q q = new Q();
- new producer(q);
- new consumer(q);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment