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 7837981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:56:22+00:00 2026-06-02T14:56:22+00:00

I was trying to write a Producer Consumer model (Producer thread and Consumer Thread

  • 0

I was trying to write a Producer Consumer model (Producer thread and Consumer Thread in java)

I was wondering how to handle the InterruptedException that is thrown by Thread.sleep method and the Object Class’s wait() method

package producerconsumer;

import java.util.ArrayList;

public class Consumer implements Runnable {

    ArrayList<Integer> container;

    @Override
    public void run() {
        while(true){
        System.out.println("Consumer Thread Running");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(null==container){
            System.out.println("container is null");
        }
        //Removing values from Arraylist
        synchronized (container) {
            if(container.size()==0){
                try {
                    container.wait();
                } catch (InterruptedException e) {
                    //How to tackle this exception
                    e.printStackTrace();
                }
            }
            Integer removedInteger = container.remove(container.size()-1);
            System.out.println("removedInteger..."+removedInteger);
        }
        }
    }

    public void setContainer(ArrayList<Integer> container) {
        this.container = container;
    }
}

This is one example I have taken, In this example there may not be any need to take care of these exception (My Assumption).

But I wanted to know what could be the different scenarios possible in which we need to handle this exception.

  • 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-06-02T14:56:24+00:00Added an answer on June 2, 2026 at 2:56 pm

    I was wondering how to handle the interrupted Exception that is thrown by Thread.sleep method and the Object Class’s wait method

    There are two important things to realize about InterruptedException. First off, when it is thrown the interrupt flag on the Thread is cleared. So you should always do something like:

    } catch (InterruptedException e) {
        // re-add back in the interrupt bit
        Thread.currentThread().interrupt();
        ...
    }
    

    This is a very important pattern because it allows other code that might be calling yours to detect the interrupt as well.

    Secondly, in terms of threads, if they are interrupted, they should most likely cleanup and quit. This is certainly up to you, the programmer, but the general behavior is to terminate and quit the operation being performed – often because the program is trying to shutdown.

    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        // we are being interrupted so we should stop running
        return;
    }
    

    Lastly, with any exception handling, the default Eclipse template is usually the wrong thing to do. Never just e.printStackTrace(); the exception. Figure out what you want to do with it: re-throw it as another exception, log it somewhere better, or exit the thread/method/application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a C/C++/Java programmer working with JavaScript. I'm trying to write a function that
I'm trying to write a seemingly simple implementation of the classic producer - consumer
I'm trying to write sql that produces the desired result from the data below.
I am trying write a function that generates simulated data but if the simulated
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a function to see how often an object exists and give
Trying to write a code at the moment that basically tests to see if
I'm trying to write a simple thread pool program in pthread. However, it seems
I'm trying to write a buffermanager that manages 3 Streams. The typical usage would
I'm trying to write a bit of SQL for SQLITE that will take a

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.