Java Programming Problem: Write a short program that creates two threads, one of
ID: 3884974 • Letter: J
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, 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 in Section 7 of this module's commentary for an idea of how to proceed.Explanation / Answer
package vairables; public class Vairables { public static void main(String[] args) { class IntegerHolder { String variable = null; synchronized void insert(String v) { while (variable != null) { notify(); wait(); } variable = v; notify(); } synchronized int extract() { while ( variable == null) { notify(); wait(); } String temp = variable; variable = null; notify(); return temp; } } } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.