Requirements: Three threads have been created. threadA prints out 100 As, thread
ID: 3531020 • Letter: R
Question
Requirements: Three threads have been created. threadA prints out 100 As, threadB prints out 300 Bs, and threadC prints out 500 Cs. Your job is to add semaphores to the program to make it print out one A, followed by three Bs, then five Cs repeatedly for 100 times. You can add just semaphore-related statements that include declaring a semaphore and calling acquire or release function on a semaphore. Code: import java.lang.Thread; import java.util.concurrent.*; public class SyncABC { private static int count = 100; public static void main( String[] args ) { System.out.printf( "Begin main thread "); // create and name each runnable Runnable taskA = new PrintTaskA(); Runnable taskB = new PrintTaskB(); Runnable taskC = new PrintTaskC(); Thread threadA = new Thread( taskA ); Thread threadB = new Thread( taskB ); Thread threadC = new Thread( taskC ); threadA.start(); threadB.start(); threadC.start(); try { threadA.join(); threadB.join(); threadC.join(); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.printf( "End main thread "); } public static class PrintTaskA implements Runnable { public void run(){ for (int i=0; i<3*count; i++) { System.out.printf( "d: %s ", i/3, "B"); } } } public static class PrintTaskC implements Runnable { public void run(){ for (int i=0; i<5*count; i++) { System.out.printf( "d: %s ", i/5, "C"); } } } }Explanation / Answer
please rate
// your modified version of code which is written by me is this
import java.util.Scanner;
import java.lang.Thread;
import java.util.concurrent.*;
public class SyncABC {
public static void main(String[] args) {
Semaphore sem = new Semaphore(1);
System.out.printf("Begin main thread "); // create and name each runnable
Runnable taskA = new PrintTaskA(sem);
Runnable taskB = new PrintTaskB(sem);
Runnable taskC = new PrintTaskC(sem);
Thread threadA = new Thread(taskA);
Thread threadB = new Thread(taskB);
Thread threadC = new Thread(taskC);
threadA.start();
threadB.start();
threadC.start();
try {
threadA.join();
threadB.join();
threadC.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.printf("End main thread ");
}
}
class Shared {
static int chance = 0;
}
class PrintTaskA implements Runnable {
Semaphore sem;
PrintTaskA(Semaphore s) {
sem = s;
}
public void run() {
for (int i = 0; i < 100; i++) {
try {
while (Shared.chance != 0);
System.out.printf("A");
sem.acquire();
Shared.chance = 1;
sem.release();
} catch (InterruptedException ex) {
}
}
}
}
class PrintTaskB implements Runnable {
Semaphore sem;
PrintTaskB(Semaphore s) {
sem = s;
}
public void run() {
for (int i = 0; i < 3 * 100; i++) {
try {
while (Shared.chance != 1);
System.out.printf("B");
if ((i + 1) % 3 == 0) {
sem.acquire();
Shared.chance = 2;
sem.release();
}
} catch (InterruptedException ex) {
}
}
}
}
class PrintTaskC implements Runnable {
Semaphore sem;
PrintTaskC(Semaphore s) {
sem = s;
}
public void run() {
for (int i = 0; i < 5 * 100; i++) {
try {
while (Shared.chance != 2);
System.out.printf("C");
if ((i + 1) % 5 == 0) {
sem.acquire();
Shared.chance = 0;
sem.release();
}
} catch (InterruptedException ex) {
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.