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 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 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 ]
expand button
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
[画像: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 ]
expand button
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 Mark
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
    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