Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:31:39+00:00 2026-05-23T09:31:39+00:00

I have a class Producer and a class Printer . The producers read from

  • 0

I have a class Producer and a class Printer.
The producers read from a file data to create PrinttJobs objects.
A Producer generates one PrintJob and add to a Queue, notifying the Printer. Producer than waits 1 – 5 seconds to create new PrintJobs.
When the Printer is notyfied it turns on and get the jobs from the queue and print them. In this period Producer can’t work. Printer prints everything and wait again, letting the Producer work again.
The app works with 2 Producers and 1 Printer.
My problem is that sometimes it go well, sometimes it doens’t print produce everything. Also I think that my wait with the time limit 1-5 seconds is not working well/ may be the problem. Code is below:

EDITED

When the Producers actually produce something, they send at the same time almost always. And sometimes it stop producing but still data in the file.

class Printer implements Runnable {

    protected long MILLIS_PER_PAGE = 500;

    private String name;
    Queue queue;
    boolean lock = false;

    public Printer(String name, Queue queue) {
        this.name = name;
        this.queue = queue;
    }

    public String getPrinterName() {
        return name;
    }

    @Override
    public void run() {
        System.out.println("["+getPrinterName()+"] Ligando...");
        while(true) {
            synchronized(this){
                if(queue.isEmpty()) {
                    try {
                        System.out.println("["+getPrinterName()+"] Esperando por tabalho de impressão...");
                        lock = false;
                        halt();
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                else {
                    lock = true;
                    PrintJob pj = queue.removeFront();
                    System.out.println("Imprimindo "+ pj.getJobName());
                    try {
                        wait(pj.getNumberOfPages() * MILLIS_PER_PAGE);
                        System.out.println(pj.getJobName() + " ok.");
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }


                }
            }
        }
    }

    public void halt() throws InterruptedException {

        wait();

    }

}
`

`
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

class Producer implements Runnable {

    private String name;
    Queue queue;

    String job;
    int pags;
    String arquivo;

    public Producer(String name, Queue queue, String arquivo) {
        this.name = name;
        this.queue = queue;
        this.arquivo = arquivo;
    }

    public String getProducerName() {
        return name;
    }

    @Override
    public void run(){
        synchronized (PrinterApp.printer) {

            FileReader fin;

            try {
                fin = new FileReader(arquivo);
                Scanner src = new Scanner(fin);

                while (src.hasNext() ) {
                    if (PrinterApp.printer.lock == true){
                        PrinterApp.printer.wait();
                    }
                    job = src.next();
                    pags = src.nextInt();
                    PrintJob p = new PrintJob(job, pags);

                    queue.addBack(p);

                    System.out.println("["+getProducerName()+"] produzindo arquivo " + job +", número de páginas: " + pags);
                    PrinterApp.printer.notify();
                    PrinterApp.printer.wait(1000 + (int)Math.round((Math.random() * (5000 - 1000))));  

                }
                fin.close();
            }


            catch (FileNotFoundException e) {
                e.printStackTrace();

            } 
            catch (QueueException e) {

                e.printStackTrace();
            }

            catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:31:39+00:00Added an answer on May 23, 2026 at 9:31 am

    The problem is with the following

                    if (PrinterApp.printer.lock == true){
                        PrinterApp.printer.wait();
                    }
    

    lock may not be true after wait ends. Waits should always be in a loop.

    Also the printer never notifies the producers that lock has changed. You should call notify before calling wait in the printer.

    If this isn’t for isn’t homework, then I’d recommend using a blocking queue which will handle all the waits and notifies for you.

    class Printer implements Runnable {
    
    protected long MILLIS_PER_PAGE = 500;
    
    private String name;
    Queue queue;
    boolean lock = false;
    
    public Printer(String name, Queue queue) {
        this.name = name;
        this.queue = queue;
    }
    
    public String getPrinterName() {
        return name;
    }
    
    @Override
    public void run() {
        System.out.println("["+getPrinterName()+"] Ligando...");
        while(true) {
            synchronized(this){
                if(queue.isEmpty()) {
                    try {
                        System.out.println("["+getPrinterName()+"] Esperando por tabalho de impressão...");
                        lock = false;
                        notifyAll();
                        halt();
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
                else {
                    lock = true;
                    PrintJob pj = queue.removeFront();
                    System.out.println("Imprimindo "+ pj.getJobName());
                    try {
                        wait(pj.getNumberOfPages() * MILLIS_PER_PAGE);
                        System.out.println(pj.getJobName() + " ok.");
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
    
                }
            }
        }
    }
    
    public void halt() throws InterruptedException {
    
        wait();
    
    }
    
    }
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;
    
    class Producer implements Runnable {
    
    private String name;
    Queue queue;
    
    String job;
    int pags;
    String arquivo;
    
    public Producer(String name, Queue queue, String arquivo) {
        this.name = name;
        this.queue = queue;
        this.arquivo = arquivo;
    }
    
    public String getProducerName() {
        return name;
    }
    
    @Override
    public void run(){
        FileReader fin;
    
        try {
             fin = new FileReader(arquivo);
             Scanner src = new Scanner(fin);
    
             while (src.hasNext() ) {                    
             synchronized (PrinterApp.printer) {
                    while (PrinterApp.printer.lock == true){
                        PrinterApp.printer.wait();
                    }
                    job = src.next();
                    pags = src.nextInt();
                    PrintJob p = new PrintJob(job, pags);
    
                    queue.addBack(p);
    
                    System.out.println("["+getProducerName()+"] produzindo arquivo " + job +", número de páginas: " + pags);
                    PrinterApp.printer.notifyAll();
                 }
                 // don't wait here since your not waiting on a condition to change
                 Thread.sleep(1000 + (int)Math.round((Math.random() * (5000 - 1000))));  
    
                }
                fin.close();
            }
    
    
            catch (FileNotFoundException e) {
                e.printStackTrace();
    
            } 
            catch (QueueException e) {
    
                e.printStackTrace();
            }
    
            catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.