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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:42:24+00:00 2026-06-03T11:42:24+00:00

I am using a thread-safe queue class and have a problem with the insert

  • 0

I am using a thread-safe queue class and have a problem with the insert method I’ve defined. A Buffer stores an array (elementData) that uses the start/end variables to know where to add/delete stuff from the queue. It’s thread-safe, so it uses synchronized methods so I can have multiple threads refer to the same buffer.

public class Buffer<T> {
    private T[] elementData;                     
    private int elementCount;
    private int start;                                      
    private int end;

    // Additional fields

    // Code to instantiate a Buffer, other methods (e.g. delete)

    public synchronized void insert(T t) throws InterruptedException {
        while (elementCount == elementData.length) {
            wait();
        }
        end = (end + 1) % elementData.length;
        elementData[end] = t;
        elementCount++;
        notifyAll();
    }

    public static void main(String[] args) {
        Buffer<Integer> b = new Buffer();
        b.insert(3);
    }
}

Here’s my understanding of the situation. When a method such as insert is called, we want to be able to throw an exception that could happen when the main method or some other thread gets called and tries to perform insert while it’s suspended. But what I don’t understand is why I get this unreported exception. I thought that having a “throws InterruptedException” after the method would be sufficient. Do I need a “try” block? My attempts with try blocks have all failed, so I’m a little stumped as to how to fix this error.

Also, I’m aware that I don’t have any actual threads running. I’ll do those once I can fix this unreported exception. 🙂 Thanks to anyone who can help.

Buffer.java:56: unreported exception java.lang.InterruptedException; must be caught or declared to be thrown
b.insert(3);
  • 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-03T11:42:28+00:00Added an answer on June 3, 2026 at 11:42 am

    The compile exception is because your insert method could throw an InterruptedException (even if you’re not throwing it on purpose), so every method that calls it must use a try/catch block, even if the error never arises:

    public static void main(String[] args) {
        Buffer<Integer> b = new Buffer();
        try {
            b.insert(3);
        } catch(InterruptedException ie) {
            //error handling
            e.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using non-thread safe libraries with threads. Say I have a library that makes a
I have a Queue object that I need to ensure is thread-safe. Would it
I'm using WinApi. Is SendMessage/PostMessage a good, thread safe method of communicating with the
I have a std::queue that is wrapped as a templated class to make a
I need to design a thread-safe logger. My logger must have a Log() method
I have a blocking queue of objects. I want to write a thread that
I'm trying to write a thread-safe queue using pthreads in c++. My program works
As MSDN says ConcurrentDictionary<TKey, TValue> Class Represents a thread-safe collection of key-value pairs that
I'm having trouble adding a copy constructor to this class: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html I need to
In the following blogpost: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html There is a 'push' method defined as follows: void

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.