hterference problems in ach siulitu output of each of the following code sequenc
ID: 3732464 • Letter: H
Question
hterference problems in ach siulitu output of each of the following code sequences? List all possible ling the interference.ee proles in to What is the ou results and explain your answers. null unter c new Counter); c.increment); c.increment): System.out.printin(c); Counter c ne Counter): unnable r-new inc rease(c. 3); Threadt new Thread (r): t.startO: System.out.println(c): rom the ess to the c Counter cnew Counter ): ue class. a string t already from the for the Runnable r-new increase (e, 3); Thread tnew Thread (r) c.increment): t.start): System.out.println(c): d. Counter c new Counter): ns of an 4 bytes erhead ace used Runnable r-new increase (C. 3): Thread t-new Thread (r): t.startO: c.increment ): System.out.println (c): e. Counter c new Counter): Runnable r-new increase (c, 3); Thread t new Thread (r): t.startO: t.join): c.increment ): System.out.printin(c): Counter c new Counter): Runnable r -new increase (c, 3); Threadt - new Thread (r): t.startO: c.increment): t.join): System.out.println(c): hes use tof the -based end ofExplanation / Answer
import java.awt.*;
import javax.swing.*;
class Counter
{
private int count;
public Counter()
{
count = 0;
}
public void increment()
{
count++;
}
public int getCount()
{
return count;
}
}
class Increase implements Runnable
{
private Counter c;
private int amount;
public Increase(Counter c , int amount) {
this.c = c;
this.amount = amount;
}
@Override
public void run() {
for(int i =1; i<=amount; i++)
c.increment();
}
}
public class Tester {
public static void main(String... args) throws InterruptedException {
// part a
// Part a of the program instantiates a Counter object c. increment it two times
// and print c
// So the output will be memory address of c
// but i think the question is wrong they want to print count value
//In that case, it will be 2. I'm modifing the code.
//Output - 2
Counter c= new Counter();
c.increment();
c.increment();
System.out.println("Part a memory address of c = " + c);
System.out.println("Part a answer = " + c.getCount());
//part b
//Part a of the program instantiates a Counter object c1.uses c1 and 3 to
//instantiate Runnable object r1 and uses r1 to instantiate Thread object t1
// But this t1 is seprate thread and before its termenation Main thread will print
// the counter's count value.
// Output = 0
Counter c1= new Counter();
Runnable r1 = new Increase(c1, 3);
Thread t1 = new Thread(r1);
t1.start();
System.out.println("Part b answer = " + c1.getCount());
// part c
// Same as part b but here. we incremented the c value before starting it.
// Output = 1
Counter c2= new Counter();
Runnable r2 = new Increase(c2, 3);
Thread t2 = new Thread(r2);
c2.increment();
t2.start();
System.out.println("Part c answer = " + c2.getCount());
// part d
// Same as part c but here. we incremented the c value after starting the thread.
// But it wiil not take so much so that thread could finish its job.
// Output = 1
Counter c3= new Counter();
Runnable r3 = new Increase(c3, 3);
Thread t3 = new Thread(r3);
t3.start();
c3.increment();
System.out.println("Part d answer = " + c3.getCount());
// part e
// We have used join to block main thread till the thread executes completely.
// Then increment c's count value.
// So Output = 4
Counter c4= new Counter();
Runnable r4 = new Increase(c4, 3);
Thread t4 = new Thread(r4);
t4.start();
t4.join();
c4.increment();
System.out.println("Part e answer = " + c4.getCount());
// part f
//Same as e but we have increment c value after starting and befor joining the thread
// It is wrong to increament count value on different thread( Here, Main thread and t5 thread)
// In thoso cases we should use synchronized block
// Output = 4
Counter c5= new Counter();
Runnable r5 = new Increase(c5, 3);
Thread t5 = new Thread(r5);
t5.start();
c5.increment();
t5.join();
System.out.println("Part e answer = " + c5.getCount());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.