Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Homework (Week 4) Term 2, 2021 Announcements Course Outline Course Schedule Ed Forum Glossary Maths Resources Moodle - Lecture Recordings Assignment 0 Spec TeX Guide Web Submission Assignment 1 Spec TeX Guide Web Submission Assignment 2 Spec Web Submission Java Resources Monitors Video Multithreading Video Semaphores Video Volatile Video Web Tutorials Week 1 Homework Thursday Slides Condensed Thursday Slides Wednesday Slides Condensed Wednesday Slides Week 2 Homework Thursday Code Thursday Notes Thursday Slides Condensed Thursday Slides Wednesday Code Week 3 Homework Promela Code Thursday Code Thursday Slides Condensed Thursday Slides Wednesday Slides Condensed Wednesday Slides Week 4 Homework Thursday Code Thursday Slides Condensed Thursday Slides Wednesday Code and Notes Wednesday Slides Condensed Wednesday Slides Week 5 Homework Thursday Slides Condensed Thursday Slides Wednesday Code and Notes Wednesday Slides Condensed Wednesday Slides Week 7 Homework Thursday Notes Thursday Slides Condensed Thursday Slides Wednesday Notes Wednesday Slides Condensed Wednesday Slides Week 8 Homework Thursday Slides Condensed Thursday Slides Wednesday Code Wednesday Slides Condensed Wednesday Slides Week 9 Homework Thursday Notes Thursday Slides Wednesday Notes Wednesday Slides Condensed Wednesday Slides Week 10 Old Exams Thursday Slides Wednesday Notes Wednesday Slides Old Exam Papers final05s2 final06s2 final07s2 final08s2 final09s2 final10s2 final11s2 final13s2 final14s2 final17s2 Homework (Week 4) Table of Contents 1. Cigarette Smokers Problem (6 marks) 2. Semaphores with Monitors (6 marks) Submission: Due on Thursday, 1st of July, 4pm Sydney Time. Please submit using the CSE Give System either online or via this command on a CSE terminal: give cs3151 hw4 *Semaphore.java CigaretteSmokers.java You may also submit any other files with a .java extension, but these are the only ones we need. Download the code to get started with this homework. To avoid name conflicts, signal is here called V() and wait is called P(). 1 Cigarette Smokers Problem (6 marks) Three smokers are rolling their own cigarettes and smoking them. In order to do so, they need three resources: paper, tobacco, and matches. Each smoker has their own limitless supply of one of the three ingredients. For the other two ingredients, they must acquire them from an Agent. They can ask for an Agent to distribute resources to them by signalling on the agent semaphore. One of the Agent threads will then awaken, and produce two of the three different resources by signalling on two of the three semaphores tobacco, paper and match. Note that the smoker has no control over which Agent thread awakens and which resources are produced. The existing code where smokers get resources directly from the agents is highly vulnerable to a deadlock scenario. In this exercise, we will implement a solution to this problem that ensures progress by introducing a "middle-man" between the agents and smokers. Step One Introduce a dedicated semaphore for each smoker, and the smoker will just wait on that semaphore, smoke a cigarette, and signal an agent forever. Step Two Add a thread called a pusher for each of the three resources. The pusher will perpetually: Wait for its resource to become available. Update some shared state (shared with the other pushers) regarding what resources are available. If two of the three resources are available, update the shared state to remove those resources, and signal the smoker that requires those two resources, to indicate that they may smoke the cigarette. Caution You must make the loop body of the pusher a critical section - mutual exclusion is required when accessing the shared state. You can accomplish this by adding another semaphore for the pushers to use. 2 Semaphores with Monitors (6 marks) In the ProducerConsumer module you will find an implementation of the Producer-Consumer problem we analysed in class. This (and the Cigarette Smokers example from earlier) use a semaphore class called JavaSemaphore that wraps around Java's built-in semaphores. We have two other classes, BusyWaitSemaphore and WeakSemaphore, without implementations added. Step One Using only Java's built-in concurrency primitives, specifically synchronized blocks/methods and Thread.yield(), implement a busy-wait semaphore in BusyWaitSemaphore. You can test it using ProducerConsumer or CigaretteSmokers. (3 marks) Step Two Using only Java's built-in concurrency primitives and monitor-like constructs, specifically synchronized blocks, Object.wait() and Object.notify(), implement a weak semaphore in WeakSemaphore. You can test it using the same examples. (3 marks) For a Meaningless Brownie Point Using only Java's built-in concurrency primitives and monitor-like constructs, as well as an ArrayDeque of Object, implement a strong semaphore in StrongSemaphore. You may use Object.notifyAll() so long as the first process to leave its await is the first process in the queue. 2021-08-05 tor 15:45 Announcements RSS