Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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; } } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote