
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
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.

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](https://content.bartleby.com/qna-images/question/eadd2534-1307-4273-82e3-a888d564b7e7/d24ce02e-d8be-42a8-b8c7-f4ad8adbf5f8/yvbim6h_thumbnail.jpeg)
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

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps

Knowledge Booster
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
- Given that an ArrayList of Strings named friendList has already been created and names have already been inserted, write the statement necessary to change the name "Tyler", stored at index 8, to "Bud". This is needed in Javaarrow_forwardWrite a Java program that will allow the user to play a hangman game repeatedly until theychoose to stop. The game should randomly choose a secret word from a pre-set list of wordsfor the user to guess. Store the list of words available for play in an array so you can randomlychoose them by index during game play.During a turn, the user will guess one letter at a time. Each letter in the secret word shoulddisplay as an asterisk until the letter is guessed by the user. When a correct letter is guessed,the letter should be displayed in the correct position in place of the asterisk. The user hangshimself with the 8th wrong guess and loses the game.Make a character array to hold the characters and asterisks representing the word beingguessed. You can change the contents of this array as the letters are guessed and revealed. Inother words, this array can be used to display the current state of the secret word each round.Here is the list of words the game should choose from for the secret…arrow_forwardNew JAVA code can only be added between lines 9 and 10, as seen in image.arrow_forward
- In JAVA, use a HashMap to count and store the frequency counts for all the words from a large text document. Then, display the contents of this HashMap with given words and their frequency in the document. Next, please create a set view of the Map and store the contents in an array. Sort this array based on key value and display it. Finally, sort the array in decreasing order by frequency and display it.arrow_forwardin java please Create a method for the client perspective to determine if a ListInterface has the same numbers in the same order. method header: public static boolean equivalentLists(ListInterface<Integer> numberListInterface, List<Integer> numberList)arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education