Starting from:

$35

CSC3002F-Assignment 6 Synchronization Part III (of III) Solved

Aim
This assignment aims to give you experience in thread (and, by extension, process) synchronization using semaphores. In this project, you will write multithreaded programs in Java to solve three classic synchronization problems, using only semaphores.  A secondary aim is to give you experience in reading, understanding and correcting existing code.  The assignment is in three parts, increasing in difficulty.  The third part is described below.

Part III: Making methane with semaphores
Methane is a molecule with four hydrogens attached to a single carbon (CH4). For this project, you will run a simulation of the combination of carbon and hydrogen atoms into methane molecules.

There are two kinds of atoms (threads), carbon and hydrogen, in your simulations. In order to assemble these atoms into methane molecules, each atom must wait until the right number (and type) of atoms are available to bond (by calling the bond() method) into a complete methane molecule.

There are a number of synchronization constraints on this simulation, as follows.

Ø Only one methane molecule forms at a time.  You must guarantee that all the atoms from one molecule invoke bond() before any of the atoms from the next molecule.  In other words:

If a carbon thread arrives at the barrier when no hydrogen threads are present, it has to wait for four hydrogen
If a hydrogen thread arrives at the barrier when no other threads are present, it has to wait for a carbon thread and a further three hydrogen threads.
You do not have to matching the atoms (threads) up explicitly; that is, the atoms (threads) do not necessarily know which other atoms (threads) they are paired up with. The key is just that atoms pass the barrier in complete sets; thus, if we examine the sequence of atoms that invoke bond()and divide them into groups of five, each group should contain one carbon and four hydrogen threads.

Assignment: Write synchronization code in Java for carbon and hydrogen molecules that enforces these constraints, using only the semaphore construct. We will  provide a reusable barrier class (an extension of Part I) for the barrier you will need.

1.1 Code skeleton: molecule

You must use and extend the molecule package provided, correctly

implementing the classes Hydrogen and Oxygen (and all the methods), so that the        RunSimulation class will execute correctly, always.   You may not add any methods to these classes. The semaphores you will need are defined already in the Methane class.  However, do not alter this class or any anther class at all (although you must submit them).  You will need to inspect the various classes to see how they work – this is part of the assignment and no explanation other than the code will be given.

An example of a correct execution of            molecule     is:

Ø java       molecule.RunSimulation       12        3

Starting simulation with 12 H and 3 C

---Group ready for bonding---

...Bonding....H4

...Bonding....H3

...Bonding....H1

...Bonding....H2

...Bonding....C2

---Group ready for bonding---

...Bonding....H7

...Bonding....H6

...Bonding....H5

...Bonding....H10

...Bonding....C1

---Group ready for bonding---

...Bonding....H12

...Bonding....H11

...Bonding....C3

...Bonding....H9

...Bonding....H8

 

1         Code requirements
You will code your solutions in Java, adding to the skeleton code provided.

You may only use the Semaphore class in the java.util.concurrent library: no other Java synchronization constructs at all.

All code must be deadlock free. 

The output must comply with the stated synchronization constraints and with

More products