public class SharedCounter {int counter = 0;public static void main(String [] args) throws InterruptedException{IntCell intCell = new IntCell();//Create incrementer and decrementer objects and pass intCell to the constructor. //Call .start() on both, then .join() on both.//Finally, print the value of intcell: intCell.getVal();} } class IntCell{private int val = 0;synchronized void increment() {val++;}synchronized void decrement() {val--;}int getVal() {return this.val;}} class Incrementer extends Thread{//IntCell reference //Set via parameterized constructor //Override the run method with a for loop that runs 10000 times and calls intCell.increment();} class Decrementer extends Thread{//IntCell reference //Set via parameterized constructor //Override the run method with a for loop that runs 10000 times and calls intCell.decrement();}
public class SharedCounter {
int counter = 0;
public static void main(String [] args) throws InterruptedException{
IntCell intCell = new IntCell();
//Create incrementer and decrementer objects and pass intCell to the constructor.
//Call .start() on both, then .join() on both.
//Finally, print the value of intcell: intCell.getVal();
}
}
class IntCell{
private int val = 0;
synchronized void increment() {
val++;
}
synchronized void decrement() {
val--;
}
int getVal() {
return this.val;
}
}
class Incrementer extends Thread{
//IntCell reference
//Set via parameterized constructor
//Override the run method with a for loop that runs 10000 times and calls intCell.increment();
}
class Decrementer extends Thread{
//IntCell reference
//Set via parameterized constructor
//Override the run method with a for loop that runs 10000 times and calls intCell.decrement();
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images