Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Computer Science CSC321 – Concurrent Programming A3, 2005/06 
 
1 of 1 
Computer Science CSC321 
 
Concurrent Programming  
 
Assignment 3 – Monitors 
 
For Q2 you must include your Java listings and a disk or CD with the Java source 
code. 
 
 
1. A memory allocator has two operations: request(amount) and 
release(amount), where amount is a positive integer. When a process calls 
request, it delays until at least amount free pages of memory are available. A 
process returns amount pages to the free pool by calling release. Pages may be 
released in different quantities than they are acquired. 
 
Develop a monitor that implements request and release. Initially there are F free 
pages. The monitor should use condition queues and the Signal and Continue 
discipline. Do not worry about the order in which requests are serviced and do not 
worry about fairness. However, a request should never be delayed if there are 
enough free pages. 
 
 
2. Suppose there is a single producer process and n consumer processes, where 
n>=1. The producer process periodically calls broadcast(message) to send a 
copy of message to all consumers. Each consumer receives a copy of the message 
by calling fetch(id), where id uniquely identifies the consumer. 
 
Using the Java template below, complete the Java monitor, Broadcast, that 
implements the methods broadcast and fetch. The monitor should only store one 
message at a time, which means that after one call to broadcast, any further call of 
broadcast has to delay until every consumer has received a copy of the first 
message. Assume that the messages are single integers.  
 
For example, one interleaving of activities for n=3 would be the following.  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Broadcast 0 
   Consumer 2 receives 0 
   Consumer 0 receives 0 
   Consumer 1 receives 0 
Broadcast 2 
   Consumer 1 receives 2 
   Consumer 2 receives 2 
   Consumer 0 receives 2 
Broadcast 4 
   Consumer 1 receives 4 
   Consumer 0 receives 4 
   Consumer 2 receives 4 
Broadcast 6 
   Consumer 0 receives 6 
   Consumer 1 receives 6 
   Consumer 2 receives 6 
Broadcast 8 
   Consumer 0 receives 8 
   Consumer 1 receives 8 
   Consumer 2 receives 8 
 
Computer Science CSC321 – Concurrent Programming A3, 2005/06 
 
2 of 2 
 
 
 
 
import concurrent.*; 
 
class ProducerConsumer { 
    public static void main (String[] args) { 
        int nConsumer = 3; 
        Broadcast b = new Broadcast(nConsumer); 
        Consumer [] c = new Consumer[nConsumer]; 
         
        Producer p = new Producer(b); 
        p.start(); 
         
        for(int i=0; i