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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:24:04+00:00 2026-05-19T09:24:04+00:00

In some code I’m working on, there are a couple of places where I

  • 0

In some code I’m working on, there are a couple of places where I do something like this:

public MyThread extends Thread{
    boolean finished = false;
    BlockingQueue<Foo> blockingQueue new LinkedBlockingQueue<Foo>();

    public void run(){
        while(!finished || (finished && !blockingQueue.isEmpty())){
            try{
                Foo f = blockingQueue.take();

                insertIntoDatabase(f);
            }
            catch(InterruptedException e){

            }
        }
    }

    public void insertThis(Foo f) throws InterruptedException{
        blockingQueue.put(f);
    }

    public void stop(){
        finished = true;
        this.interrupt();
    }
}

This, however, is causing problems as the thread is sometimes interrupted when it is in the insertIntoDatabase() method. As we’re using Apache Derby, it throws a hissy fit (to wit: “java.sql.SQLException: Derby thread received an interrupt during a disk I/O operation, please check your application for the source of the interrupt.”), and all subsequent database communication falls down in a nasty mess. Is there any tidy way to protect the thread from an interrupt when it’s not waiting on the blocking queue, or alternatively to target the interrupt at the blocking queue?

Two solutions that I have seen suggested or have occurred to me are to insert a “poison pill” object into the queue to shut it down, and to have an extra boolean interruptsWelcome field that could be checked by the stop() method, but neither is particularly attractive to me – I’d either have to mess with the class hierarchy (Foo is not a trivial class) or produce masses of synchronisation code. Is there something neater?

  • 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-19T09:24:04+00:00Added an answer on May 19, 2026 at 9:24 am

    The first thing you may want to do is to use an ExecutorService. In which you can use a single thread to submit the foo requests.

        class FooRunner{
    
         ExecutorService service = Executors.newSingleThreadExecutor();
    
         //where RunnableFoo extends Foo and implements Runnable
            public void insertThis(RunnableFoo f) throws InterruptedException{ Run
                service.submit(f);
            }
            public void stop(){
                service.shutdown();
            }
    
        }
    

    Your runnable itself can just ignore interrupted exception if you want

    class RunnableFoo extends Foo implements Runnable{
    
     public void run(){
         try{
             insertIntoDatabase(this);
         }catch(InterruptedException ex){
           ex.printStackTrace();
         }
     }
    }
    

    Edit:

    I saw your comment on the other answer and to answer that question in terms of using an ExecutorService. By having a singleThreadExeuctor you are restricting to one upload via thread-confinement. If only one thread is running within the service only one runnable will run at a time. The others will just queue up until the previous finished.

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

Sidebar

Related Questions

In some code that I read, there was an initializing statement like this char
In some code I saw recently there was a structure defined like this: typedef
Some code I've been asked to modify looks rather like this: namespace XXX {
So some code in MainApp.py goes like this, the lines I am concerned about
Given some code like this: class Json { } class Program { static void
In some code I am writing, sometimes I get ticks values like this Clear[z];
Got some code here that isn't working: $(#sidebar ul li:last).each(function(){ $(this).addClass(last); }); Basically I
Some code for context: class a { } class b { public a a{get;set;}
Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
In some code I've been reading, I've come across this : class Someclass {

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.