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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:22:38+00:00 2026-05-27T15:22:38+00:00

Very recently I’ve asked this question, but wasn unable to fix this. So I

  • 0

Very recently I’ve asked this question, but wasn unable to fix this. So I have a thread hunter (2 of them actually), who “goes off to catch wild boars”. He stores these boars in a container Fridge. He will continue to do so until his working hours expire. However, in case the Fridge is full he has to wait. The aim is to wait until a wild boar is removed from the fridge, but if it takes more then 5 seconds of waiting test must be terminated. So everything works except one thing. After running test and interrupting these threads, the program still continues to run. So how do I completely terminate/stop these threads?

TEST CLASS (main)

class Test {

    public static void main(String[] args) {
        test1();
    }

    public static void test1() {

        Fridge fridge = new Fridge(4);

        Hunter hunter1 = new Hunter("hunter1", 4, fridge);
        Hunter hunter2 = new Hunter("hunter2", 7, fridge);
        Thread hunterThread1 = new Thread(hunter1);
        Thread hunterThread2 = new Thread(hunter2);
        hunterThread1.start();
        hunterThread2.start();

        try { Thread.sleep(1000); } catch (InterruptedException e) {}
        hunterThread1.interrupt();
        hunterThread2.interrupt();
        System.out.println(fridge.getSize());
        System.out.println(hunter1.getWorkTime());
        System.out.println(hunter2.getWorkTime());
    }
}

HUNTER CLASS

class Hunter extends Worker {

    private int workTime;
    private Fridge fridge;

    public Hunter(String name, int workTime, Fridge fridge) {
        super(name);
        this.workTime = workTime;
        this.fridge = fridge;
    }

    public int getWorkTime() {
        return workTime;
    }

    public void run() {
        while (workTime > 0) {
            /** Each hunt takes a random amount of time (1-50 ms) **/
            try { Thread.sleep(workGen()); } catch (InterruptedException e) {}
            /** Add new wild boars **/
            try { fridge.add(new WildBoar()); } catch (InterruptedException e) {}
            workTime--;
            /** If thread is interupted break the loop **/
            if( Thread.currentThread().isInterrupted()){
                break;
            }
        }
    }
}

FRIDGE CLASS

import java.util.Stack;

class Fridge extends Storage {

    private Stack<WildBoar> boars;

    public Fridge(int cap) {
        super(cap);
        boars = new Stack<WildBoar>();
    }

    public int getCap() {
        return cap;
    }

    public int getSize() {
        return boars.size();
    }

    public boolean hasFreeSpace() {
        if ( boars.size() < cap )
            return true;
        else
            return false;
    }

    public synchronized void add(WildBoar boar) throws InterruptedException {
        /** If there's no free space available wait **/
        while ( !hasFreeSpace() ) {
            wait();
        }
        /** Once there's free space available add new item **/
        boars.add(boar);

    }

    public synchronized WildBoar remove() {
        return boars.pop();
    }

}

ADDITIONAL CLASSES FOR COMPILING:

abstract class Worker implements Runnable {

    private String name;

    public Worker(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int workGen() {
        return 1 + (int)(Math.random() * (50 - 1));
    }
}

class WildBoar {

    public WildBoar() {}
}

abstract class Storage {

    protected int cap;

    public Storage(int cap) {
        this.cap = cap;
    }

    public int getCap() {
        return cap;
    }
}
  • 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-27T15:22:39+00:00Added an answer on May 27, 2026 at 3:22 pm

    After you interrupt() the thread which is currently waiting, the native wait method will actually reset the interruption flag. So when you evaluate the isInterrupted() here, it is actually reset and will appear as not interrupted.

    if( Thread.currentThread().isInterrupted()){
         break;
    }
    

    You will have to re-interrupt the thread after an interruption occurs during the waiting

    public synchronized void add(Object boar) {
        /** If there's no free space available wait **/
        while (!hasFreeSpace()) {
         try{
             wait();
         }catch(InterruptedException e){ 
            Thread.currentThread().interrupt(); 
            return; //or rethrow
         }
        }
        /** Once there's free space available add new item **/
        boars.add(boar);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is very similar to one I recently asked: Python threading- returning control
Very recently I asked this question on how to pass clicks through an element
I have just started C very recently and I have been asked to answer
I have been developing web applications for some time but have very recently been
I have very recently received a lot of traffic to my site that runs
I have recently started working on a very large C++ project that, after completing
I only very recently began developing professionally. I studied OOP while at University, but
I have a query that is very recently starting to throw: The built-in indices
I have started using django very recently . I am building a service with
I've been using GAE for months now, but very recently I've had difficulty getting

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.