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...


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<br>O Text Editor<br>Dec 6 09:28<br>BadReaderWriter.java<br>-/Downloads/Telegram Desktop<br>Open<br>Save<br>1 import java.util.Arraylist;<br>2<br>3 public class BadReaderwriter {<br>4<br>private statc votd acquirelLock() throws InterruptedException {<br>6.<br>// TODO: Add code for acqutring lock for critical section<br>8.<br>private static void releaselLock() throws InterruptedException {<br>10<br>11<br>12<br>// TODO: Add code for releastng lock for critical section<br>13<br>15 14<br>15<br>stattc class ReaderThread extends Thread {<br>16<br>String name =
numbers; 17 18 19 Reader Thread(String name, ArrayList 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 numbers; 50 WriterThread(String name, Arraylist numbers) { this.name = name; this.numbers numbers; 51 52 53 54 55 public votd run() ( Java Tab Width: 8 Ln 1, Col 1 INS "/>
Extracted 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 numbers; 17 18 19 Reader Thread(String name, ArrayList 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 numbers; 50 WriterThread(String name, Arraylist 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<br>A Text Editor<br>Dec 6 09:28<br>BadReaderWriter.java<br>-/Downloads/Telegram Desktop<br>Open<br>Save<br>41<br>42<br>43<br>44<br>45<br>statte class WriterThread extends Thread {<br>46<br>47<br>String name =
numbers; 48 49 50 51 52 53 15 54 WriterThread(string name, ArrayList 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 listofNumbers = new Arraylist(); 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 "/>
Extracted 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 numbers; 48 49 50 51 52 53 15 54 WriterThread(string name, ArrayList 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 listofNumbers = new Arraylist(); 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
Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here