Question: Java programming problem: Write a short program that creates two threads, one of which successively sets a variable to the integers from 1 to 10,

Java programming problem:

Write a short program that creates two threads, one of which successively sets a variable to the integers from 1 to 10, and another that reads the values, printing each one as it goes. Use synchronized methods, wait( ) and notify( ). Use a separate condition variable to signify that the integer variable is empty. Since the whole point of the exercise is to make sure that every written value is read, without any values being skipped or overwritten, pay special attention to access control. Look at the sample code of this module's commentary for an idea of how to proceed.

p.s. module's sample code:

class DataHolder { String data = null; synchronized void insert(String s) { try { while (data != null) { // data already holds a string // wait for some other Thread //to remove it! // awaken one other waiting Thread notify(); // release the monitor wait(); } } catch (InterruptedException e) {} // data is now null data = s; notify(); } synchronized String extract() { try { while (data == null) { // no data to extract // wait for some to arrive // awaken one other waiting Thread notify(); // release the monitor and go to sleep wait(); } } catch (InterruptedException e) {} // data is now not full String temp = data; data = null; notify(); return temp; } // more methods } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!