Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mk.ukim.finki.os.synchronization.exam16.s2;
- import mk.ukim.finki.os.synchronization.ProblemExecution;
- import mk.ukim.finki.os.synchronization.TemplateThread;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.concurrent.Semaphore;
- public class OxalicAcid {
- //#########
- //Mart Cube
- //#########
- public static void init() {
- }
- static int C = 0;
- static int H = 0;
- static int O = 0;
- static Semaphore carbon = new Semaphore(2);
- static Semaphore hydrogen = new Semaphore(2);
- static Semaphore oxygen = new Semaphore(4);
- static Semaphore start_bond = new Semaphore(0);
- public static class Carbon extends TemplateThread {
- public Carbon(int numRuns) { super(numRuns); }
- @Override
- public void execute() throws InterruptedException {
- carbon.acquire();
- synchronized (carbon) {
- C++;
- if( C==2 && H==2 && O==4){ // posledniot atom sto vleguva od C H O toj ke gi pusti site ( togas ke bide se spremno za bond )
- start_bond.release(8); // vkupno C H i O se 8 atomi
- }
- }
- start_bond.acquire(); // site cekaat tuka
- state.bond(); // zaedno site go izvrsuvaat koga ke mu bide dozvoleno
- synchronized (carbon) {
- C--;
- if( C==0 && H==0 && O==0){
- state.validate(); // posledniot atom pravi validacija
- // reset
- carbon.release(2);
- hydrogen.release(2);
- oxygen.release(4);
- }
- }
- }
- }
- public static class Hydrogen extends TemplateThread {
- public Hydrogen(int numRuns) {super(numRuns); }
- @Override
- public void execute() throws InterruptedException {
- hydrogen.acquire();
- synchronized (hydrogen) {
- H++;
- if( C==2 && H==2 && O==4){
- start_bond.release(8);
- }
- }
- start_bond.acquire();
- state.bond();
- synchronized (carbon) {
- H--;
- if( C==0 && H==0 && O==0){
- state.validate(); // the last atom is running this
- // reset for new atom
- carbon.release(2);
- hydrogen.release(2);
- oxygen.release(4);
- }
- }
- }
- }
- public static class Oxygen extends TemplateThread {
- public Oxygen(int numRuns) {super(numRuns);}
- @Override
- public void execute() throws InterruptedException {
- oxygen.acquire();
- synchronized (carbon) {
- O++;
- if( C==2 && H==2 && O==4){
- start_bond.release(8);
- }
- }
- start_bond.acquire();
- state.bond();
- synchronized (carbon) {
- O--;
- if( C==0 && H==0 && O==0){
- state.validate(); // the last atom is running this
- // reset for new atom
- carbon.release(2);
- hydrogen.release(2);
- oxygen.release(4);
- }
- }
- }
- }
- static OxalicAcidState state = new OxalicAcidState();
- public static void main(String[] args) {
- for (int i = 0; i < 10; i++) {
- run();
- }
- }
- public static void run() {
- try {
- int numRuns = 1;
- int numScenarios = 300;
- HashSet<Thread> threads = new HashSet<Thread>();
- for (int i = 0; i < numScenarios; i++) {
- Oxygen o = new Oxygen(numRuns);
- threads.add(o);
- if (i % 2 == 0) {
- Hydrogen h = new Hydrogen(numRuns);
- Carbon c = new Carbon(numRuns);
- threads.add(c);
- threads.add(h);
- }
- }
- init();
- ProblemExecution.start(threads, state);
- System.out.println(new Date().getTime());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement