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

Please help the following Java programming assignment!! Thank you!! Please do no

ID: 3885391 • Letter: P

Question

Please help the following Java programming assignment!! Thank you!! Please do not copy the answer from elsewhere.

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.

Explanation / Answer

import java.io.*;
import java.util.*;

class Data {
    private int a;
   
    public Data(int b){
        this.a=b;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a=a;
    }

}

class Notifier implements Runnable {

    private Data d;
   
    public Notifier(Data d1) {
        this.d = d1;
    }

   
    public void run() {
        String name = Thread.currentThread().getName();
        System.out.println(name+" started");
        try {
            while(true){
               Thread.sleep(1000);
               synchronized (d) {
                  int a = d.getA();
                  a++;
                  d.setA(a % 10);
                  System.out.println("Setting value :" + a % 10);    
                  d.notify();
               
              }
           }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       
    }

}


class Waiter implements Runnable{
   
    private Data d;
   
    public Waiter(Data d2){
        this.d=d2;
    }

   
    public void run() {
        String name = Thread.currentThread().getName();
        while (true) {
           synchronized (d) {
               try{
                  System.out.println(name+" waiting to get notified ");
                  d.wait();
               }catch(InterruptedException e){
                  e.printStackTrace();
               }
               System.out.println(name+" waiter thread got notified");
               System.out.println(name+" processed: "+ d.getA());
       
          }
       }
    }

}


public class Multi {

    public static void main(String[] args) {
        Data d = new Data(5);
        Waiter waiter = new Waiter(d);
        new Thread(waiter,"waiter").start();
       
        Waiter waiter1 = new Waiter(d);
        new Thread(waiter1, "waiter1").start();
       
        Notifier notifier = new Notifier(d);
        new Thread(notifier, "notifier").start();
        System.out.println("All the threads are started");
    }

}

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