Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

2. Discuss various aspects of the training design process.

Answered: 1 week ago