Consider the BadReaderWriter.java program attached with this homework. The program has three threads, namely, one reader thread and two writer threads, and they all access the same list of numbers. The reader thread reads the list and prints it to the terminal. The writer threads append numbers to the list. At any point in time, if either of the writer threads finds that the list contains n elements, then it appends the number n + 1 to the list. Run this program, examine the output, and identify the problems. Fix these problems by implementing the acquireLock() and releaseLock() methods in the code.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Consider the BadReaderWriter.java program attached with this homework. The program has three threads, namely, one reader thread and two writer threads, and they all access the same list of numbers. The reader thread reads the list and prints it to the terminal. The writer threads append numbers to the list. At any point in time, if either of the writer threads finds that the list contains n elements, then it appends the number n + 1 to the list.
Run this program, examine the output, and identify the problems.
Fix these problems by implementing the acquireLock() and releaseLock() methods in the code.

Activities
2 Text Editor
Dec 6 09:28
BadReaderWriter.java
-/Downloads/Telegram Desktop
Open
Save
41
42
43
44
45
stattc class WriterThread extends Thread {
46
47
String name = "":
ArrayList<Integer> numbers;
48
49
50
51
52
53
WriterThread(String name, ArrayList<Integer> numbers) {
this.name = name;
this.numbers numbers;
15
54
55
public votd run() {
56
57
for (int count = 0; count < 10; count++) {
58
try {
acqutrelock();
try {
Integer entry = numbers.stze() + 1;
numbers.add(entry);
Systen.out.printin(">>
59
60
61
62
63
+ name +" added: " +
+ entry);
64
Systen.out.flush();
65
66
67
68
finally {
releaselock();
69
70
} catch (InterruptedException e) {
e.printstackTrace();
71
72
73
74
75
76
77
78
79
80
public static void main(String[] args) (
81
82
ArrayList<Integer> listofNumbers = new Arraylist<Integer>();
83
84
ReaderThread t1 = new Reader Thread("reader1", ltstofNumbers);
85
86
WriterThread t2 = new Writer Thread("writer1", listofNumbers);
87
88
89
writerThread t3 = new WriterThread( "writer2", ltstofNumbers);
t1.start();
t2.start();
t3.start();
90
91
92
93
94
95 }
Java Tab Width: 8
Ln 1, Col 1
INS
Transcribed Image Text:Activities 2 Text Editor Dec 6 09:28 BadReaderWriter.java -/Downloads/Telegram Desktop Open Save 41 42 43 44 45 stattc class WriterThread extends Thread { 46 47 String name = "": ArrayList<Integer> numbers; 48 49 50 51 52 53 WriterThread(String name, ArrayList<Integer> numbers) { this.name = name; this.numbers numbers; 15 54 55 public votd run() { 56 57 for (int count = 0; count < 10; count++) { 58 try { acqutrelock(); try { Integer entry = numbers.stze() + 1; numbers.add(entry); Systen.out.printin(">> 59 60 61 62 63 + name +" added: " + + entry); 64 Systen.out.flush(); 65 66 67 68 finally { releaselock(); 69 70 } catch (InterruptedException e) { e.printstackTrace(); 71 72 73 74 75 76 77 78 79 80 public static void main(String[] args) ( 81 82 ArrayList<Integer> listofNumbers = new Arraylist<Integer>(); 83 84 ReaderThread t1 = new Reader Thread("reader1", ltstofNumbers); 85 86 WriterThread t2 = new Writer Thread("writer1", listofNumbers); 87 88 89 writerThread t3 = new WriterThread( "writer2", ltstofNumbers); t1.start(); t2.start(); t3.start(); 90 91 92 93 94 95 } Java Tab Width: 8 Ln 1, Col 1 INS
Activities
2 Text Editor
Dec 6 09:28
BadReaderWriter.java
-/Downloads/Telegram Desktop
Оpen
Save
1 import java.util.ArrayList;
2
3 public class BadReaderwriter {
private static void acquirelLock() throws InterruptedException {
6.
7
// TODO: Add code for acquiring lock for critical section
8
private static void releaselLock() throws InterruptedException {
10
11
12
// TODO: Add code for releasing lock for critical section
13
15 14
15
stattc class ReaderThread extends Thread {
16
String name = "";
ArrayList<Integer> numbers%3;
17
18
19
Reader Thread (string name, ArrayList<Integer> numbers) {
this.name = name;
this.numbers = numbers;
20
21
22
23
24
25
26
27
28
29
30
public votd run() {
for (int count = 0; count < 20; count++) {
try {
acquirelock();
try {
Systen.out.println(">>>
Systen.out.flush();
read: "
+ numbers);
31
+ name +
32
33
34
35
36
37
finally {
releaselock();
} catch (InterruptedException e) {
e.printstackTrace();
38
39
40
41
42
43
44
45
46
47
48
49
stattc class WriterThread extends Thread {
String name = "";
ArrayList<Integer> numbers;
50
51
writerThread(String name, ArrayList<Integer> numbers) {
this.name = name;
this.numbers numbers;
}
52
53
54
55
public votd run() (
Java Tab Width: 8
Ln 1, Col 1
INS
Transcribed Image Text:Activities 2 Text Editor Dec 6 09:28 BadReaderWriter.java -/Downloads/Telegram Desktop Оpen Save 1 import java.util.ArrayList; 2 3 public class BadReaderwriter { private static void acquirelLock() throws InterruptedException { 6. 7 // TODO: Add code for acquiring lock for critical section 8 private static void releaselLock() throws InterruptedException { 10 11 12 // TODO: Add code for releasing lock for critical section 13 15 14 15 stattc class ReaderThread extends Thread { 16 String name = ""; ArrayList<Integer> numbers%3; 17 18 19 Reader Thread (string name, ArrayList<Integer> numbers) { this.name = name; this.numbers = numbers; 20 21 22 23 24 25 26 27 28 29 30 public votd run() { for (int count = 0; count < 20; count++) { try { acquirelock(); try { Systen.out.println(">>> Systen.out.flush(); read: " + numbers); 31 + name + 32 33 34 35 36 37 finally { releaselock(); } catch (InterruptedException e) { e.printstackTrace(); 38 39 40 41 42 43 44 45 46 47 48 49 stattc class WriterThread extends Thread { String name = ""; ArrayList<Integer> numbers; 50 51 writerThread(String name, ArrayList<Integer> numbers) { this.name = name; this.numbers numbers; } 52 53 54 55 public votd run() ( Java Tab Width: 8 Ln 1, Col 1 INS
Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY