Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Consider the BadReaderWriter.java program attached with this. 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
O Text Editor
Dec 6 09:28
BadReaderWriter.java
-/Downloads/Telegram Desktop
Open
Save
1 import java.util.Arraylist;
2
3 public class BadReaderwriter {
4
private statc votd acquirelLock() throws InterruptedException {
6.
// TODO: Add code for acqutring lock for critical section
8.
private static void releaselLock() throws InterruptedException {
10
11
12
// TODO: Add code for releastng lock for critical section
13
15 14
15
stattc class ReaderThread extends Thread {
16
String name = "";
ArrayList<Integer> numbers;
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
31
public votd run() {
for (int count = 0; count < 20; count++) {
try {
acqutrelock();
try {
Systen.out.printin(>
Systen.out.flush();
+ name +
read:+ numbers);
32
33
34
35
finally {
releaselock();
36
} catch (InterruptedException e) {
e.printStackTrace();
37
38
39
40
41
42
43
44
stattc class WriterThread extends Thread (
45
46
47
48
49
String name = "";
Arraylist<Integer> numbers;
50
WriterThread(String name, Arraylist<Integer> numbers) {
this.name = name;
this.numbers numbers;
51
52
53
54
55
public votd run() (
Java Tab Width: 8
Ln 1, Col 1
INS
expand button
Transcribed Image Text:Activities O Text Editor Dec 6 09:28 BadReaderWriter.java -/Downloads/Telegram Desktop Open Save 1 import java.util.Arraylist; 2 3 public class BadReaderwriter { 4 private statc votd acquirelLock() throws InterruptedException { 6. // TODO: Add code for acqutring lock for critical section 8. private static void releaselLock() throws InterruptedException { 10 11 12 // TODO: Add code for releastng lock for critical section 13 15 14 15 stattc class ReaderThread extends Thread { 16 String name = ""; ArrayList<Integer> numbers; 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 31 public votd run() { for (int count = 0; count < 20; count++) { try { acqutrelock(); try { Systen.out.printin(> Systen.out.flush(); + name + read:+ numbers); 32 33 34 35 finally { releaselock(); 36 } catch (InterruptedException e) { e.printStackTrace(); 37 38 39 40 41 42 43 44 stattc class WriterThread extends Thread ( 45 46 47 48 49 String name = ""; Arraylist<Integer> numbers; 50 WriterThread(String name, Arraylist<Integer> numbers) { this.name = name; this.numbers numbers; 51 52 53 54 55 public votd run() ( Java Tab Width: 8 Ln 1, Col 1 INS
Activities
A Text Editor
Dec 6 09:28
BadReaderWriter.java
-/Downloads/Telegram Desktop
Open
Save
41
42
43
44
45
statte class WriterThread extends Thread {
46
47
String name = "":
ArrayList<Integer> numbers;
48
49
50
51
52
53
15 54
WriterThread(string name, ArrayList<Integer> numbers) {
this.name = name;
this.numbers = numbers;
55
public votd run() {
56
for (int count = 0; count < 10; count++) {
try (
57
58
acqutrelock();
try (
Integer entry numbers.stze() + 1;
numbers.add(entry);
Systen.out.printin(>>>
Systen.out.flush();
59
60
61
62
63
64
65
+ name + added: +
+ entry);
66
67
68
finally {
releaselock();
69
70
71
72
73
74
75
76
77
78
79
} catch (InterruptedException e) {
e.printstackTrace();
80
public static void main(String[] args) {
81
82
83
84
Arraylist<Integer> listofNumbers = new Arraylist<Integer>();
ReaderThread ti = new Reader Thread("reader1", ltstofNumbers);
85
86
WriterThread t2 = new Writer Thread("writer1", ltstofNumbers);
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
expand button
Transcribed Image Text:Activities A Text Editor Dec 6 09:28 BadReaderWriter.java -/Downloads/Telegram Desktop Open Save 41 42 43 44 45 statte class WriterThread extends Thread { 46 47 String name = "": ArrayList<Integer> numbers; 48 49 50 51 52 53 15 54 WriterThread(string name, ArrayList<Integer> numbers) { this.name = name; this.numbers = numbers; 55 public votd run() { 56 for (int count = 0; count < 10; count++) { try ( 57 58 acqutrelock(); try ( Integer entry numbers.stze() + 1; numbers.add(entry); Systen.out.printin(>>> Systen.out.flush(); 59 60 61 62 63 64 65 + name + added: + + entry); 66 67 68 finally { releaselock(); 69 70 71 72 73 74 75 76 77 78 79 } catch (InterruptedException e) { e.printstackTrace(); 80 public static void main(String[] args) { 81 82 83 84 Arraylist<Integer> listofNumbers = new Arraylist<Integer>(); ReaderThread ti = new Reader Thread("reader1", ltstofNumbers); 85 86 WriterThread t2 = new Writer Thread("writer1", ltstofNumbers); 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
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education