Commit d195c8ad authored by OlaBola98's avatar OlaBola98
Browse files

Task 5 beginning

parent a73deabb
......@@ -44,4 +44,38 @@
s2.P()
write("5");
write("6");
}
\ No newline at end of file
}
##Task 4
4.2
printer() {'
Sem s1 = new Sem(0);
Sem s2 = new Sem(0);
process P1 {
write("1");
write("2");
s1.release();
}
process P2 {
s1.aquire();
write("3");
write("4");
s2.release();
}
process P3 {
s2.aquire();
write("5");
write("6");
}
##Task 5
Imports:
CyclicBarrier is a syncrhonization tool for allowing a set of threads to wait for each other
to reach a barrier. The cyclic portion is that this can be done again after the waiting
threads have been released.
BrokenBarrierException throws an exception when a thread tries to wait on another thread that is
in a broken state.
\ No newline at end of file
package task3;
package src.task3;
public class printer {
public void printer(){
process P1
process P1;
}
}
package src.task4;
public class Sem {
private int permits;
public Sem(int permits) {
this.permits = permits;
}
public synchronized void acquire() throws InterruptedException {
if (permits > 0) {
// TODO: Your solution goes here.
return;
} else {
// TODO: Your solution goes here.
while (permits == 0) {
//wait
}
permits = -1;
}
public synchronized void release() {
// TODO: Your solution goes here.
permits = 1;
}
}
}
package src.task5;
public class Doll {
int id;
int qualityScoreMachine;
boolean imperfect, isPainted;
public Doll(int id, int i) {
return;
}
public boolean setPainted(boolean b) {
return isPainted;
}
public int getQualityScore() {
return qualityScoreMachine;
}
public boolean hasImperfections(boolean b) {
return imperfect;
}
}
package src.task5;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class DollFactory {
List<Doll> dolls;
public CyclicBarrier stageA, stageB, stageC;
private void execution(int dollsNumber) throws InterruptedException {
stageA = new CyclicBarrier(dollsNumber);
stageB = new CyclicBarrier(dollsNumber);
// TODO stageC = new CyclicBarrier(...);
stageC = new CyclicBarrier(dollsNumber + 1);
dolls = new ArrayList<>(dollsNumber);
for (int i = 0; i < dollsNumber; i++) {
Process task = new Process(i);
// TODO: Your solution goes here
Thread thread = new Thread(task);
thread.start();
}
try {
stageC.await();
System.out.println("Packaging process D");
// TODO: print results
System.out.println();
System.out.println(dolls);
} catch (BrokenBarrierException e) {
e.printStackTrace(); }}// c
}
package src.task5;
import java.util.List;
import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class Process implements Runnable {
int id;
List dolls;
CyclicBarrier stageA, stageB, stageC;
public Process(int id, List dolls, CyclicBarrier stageA, CyclicBarrier stageB, CyclicBarrier stageC) {
this.id = id;
this.stageA = stageA;
this.stageB = stageB;
this.stageC = stageC;
this.dolls = dolls;
}
public void run() {
// TODO: Your solution goes here
Doll doll = assembly();
try {
stageA.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
painting(doll);
try {
stageB.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
qualityControl(doll);
try {
stageC.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
void painting(Doll d) {
d.setPainted(true);
}
Doll assembly() {
Random r = new Random();
return new Doll(id, r.nextInt(4) + 7);
}
void qualityControl(Doll d) {
if (d.getQualityScore() >= 9) {
d.hasImperfections(false);
dolls.add(d);
}
}
}
package src.task5;
public class main {
public static void main(String[] args) throws InterruptedException {
DollFactory dcb = new DollFactory();
dcb.execution(100);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment