Java Week9(B)   Notepad
Upcoming SlideShare
Loading in...5
×
 

Java Week9(B) Notepad

on

  • 642 views

 

Statistics

Views

Total Views
642
Views on SlideShare
641
Embed Views
1

Actions

Likes
0
Downloads
10
Comments
0

1 Embed 1

http://www.slideshare.net 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Java Week9(B)   Notepad Java Week9(B) Notepad Document Transcript

  • JAVA WEEK9(B) /* Week 9 : b) Write a Java program that correctly implements producer consumer problem using the concept of inter thread communication. */ class LabPro17 { int n; boolean vs = false; synchronized int get() { if(!vs) try{wait();} catch(InterruptedException e) { System.out.println("InterruptedException caught");} System.out.println("got:" + n); vs = false; notify(); return n; } synchronized int put(int n) { if(vs) try{wait();} catch(InterruptedException e) { System.out.println("InterruptedException caught");} this.n = n; vs = true; System.out.println("put:" + n); notify(); return n; } } class Producer implements Runnable { LabPro17 k; Producer(LabPro17 k) { this.k = k; new Thread(this, "Producer").start(); } public void run() { int i = 0; while(true) {k.put(i++);} } } class Consumer implements Runnable { LabPro17 k; Consumer(LabPro17 k) { this.k = k; new Thread(this, "Consumer").start(); } public void run() { while(true) {k.get();} } } class PCFixed Page 1
  • JAVA WEEK9(B) { public static void main(String args[]) { LabPro17 k = new LabPro17(); new Producer(k); new Consumer(k); System.out.println("press control - c to stop. "); } } Page 2