Database System Concepts
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
Bartleby Related Questions 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.
Transcribed Image Text:Activities
O Text Editor -
Dec 6 09:28
BadReaderWriter.java
-/Downloads/Telegram Desktop
Оpen
Save
1 import java.util.ArrayList;
2
3 public class BadReaderwriter {
4
private static void acquireLock() throws InterruptedException {
6.
// TODO: Add code for acquiring lock for critical section
}
7
8
10
private static void releaseLock() throws InterruptedException {
11
// TODO: Add code for releasing lock for critical section
}
12
13
14
15
static class ReaderThread extends Thread {
16
String name = "";
ArrayList<Integer> numbers;
17
19
ReaderThread (String name, ArrayList<Integer> numbers) {
this.name = name;
20
21
this.numbers = numbers;
}
public void run() {
26
27
for (int count = 0; count < 20; count++) {
28
try {
acquirelock();
try {
System.out.println(">>>
System.out.flush();
29
30
31
+ name + " read:
+ numbers);
32
33
finally {
releaselock();
}
} catch (InterruptedException e) {
e.printstackTrace();
}
34
35
36
37
38
39
}
}
40
41
42
43
}
44
45
static class WriterThread extends Thread {
46
String name = "";
ArrayList<Integer> numbers;
47
48
49
WriterThread(String name, ArrayList<Integer> numbers) {
this.name = name;
50
51
this.numberS = numbers;
}
52
53
54
55
public void run() {
Java
Tab Width: 8 ▼
Ln 1, Col 1
INS
Transcribed Image Text:Activities
O Text Editor -
Dec 6 09:28
BadReaderWriter.java
-/Downloads/Telegram Desktop
Оpen
Save
41
}
42
43
}
44
45
static class WriterThread extends Thread {
46
String name =
ArrayList<Integer> numbers;
47
48
49
WriterThread(String name, ArrayList<Integer> numbers) {
this.name = name;
this.numbers = numbers;
}
50
51
52
public void run() {
56
for (int count = 0; count < 10; count++) {
try {
57
58
acquirelock();
try {
Integer entry = numbers.size() + 1;
numbers.add(entry);
System.out.println(">>>
System.out.flush();
59
60
61
62
63
" added: "
+ entry);
+ name +
64
65
66
}
finally {
releaselock(O;
}
67
68
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> list0fNumbers = new ArrayList<Integer>();
83
84
ReaderThread t1 = new ReaderThread("reader1", listofNumbers);
85
86
WriterThread t2 = new WriterThread ("writer1", listofNumbers);
87
88
WriterThread t3 = new WriterThread("writer2", list0fNumbers);
89
t1.start();
t2.start();
t3.start();
90
91
92
93
}
95 }
94
Java
Tab Width: 8 ▼
Ln 1, Col 1
INS
Expert Solution
Check MarkThis question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
bartleby
Step by stepSolved in 2 steps
Knowledge Booster
Background pattern image
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_forwardConsider 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.arrow_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_forward
- Java Programming This week's project involves a text file with that holds all of the novel Don Quixote*, by Miguel Cervantes. The text file has been heavily processed. It contains only lowercase letters, spaces, and new lines. That is a good format for counting words. Write code that reads through the text file one word at a time using a Scanner, and the next() method. Put the words into a HashMap, where the words are used as keys, and the values are Integers used to keep track of how many times the words occur in the text. If the text was "one fish two fish red fish blue fish one two three" then the HashMap would look like this: key value "one".... 2 "fish"... 4 "two".... 2 "red".... 1 "blue"... 1 "three".. 1 Once you have created such a HashMap for the whole of the text you can write logic that prints out the answers to these questions: How many times does each of...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
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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