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

  • Home
  • SEARCH
  • 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 3271072
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:41:43+00:00 2026-05-17T18:41:43+00:00

i have this scenario: class MyClass { Producer p; Consumer c; public static void

  • 0

i have this scenario:

class MyClass {
   Producer p;
   Consumer c;
    public static void main(String[] args){
        BlockingQueue q = new LinkedBlockingQueue();
        p = new Producer(q);
        c = new Consumer(q);
        Thread t = new Thread(p);
        t.start();
        new Thread(c).start();
        while (true) {
            if (!p.getContinuer()) {
                c.setContinuer(false);
                System.out.println("here:"+p.getContinuer().toString());
                break;
            }
        }
        System.out.println("finish all");
     }
}

class Producer implements Runnable {
private final BlockingQueue queue;
private AtomicBoolean continuer = new AtomicBoolean(true);
   public Boolean getContinuer() {
      return continuer.get();
   }
    @Override
    public void run() {
      while(true){
        //open socket
        //read data from socket
        queue.put(data);
        if(end){
            System.out.println("Shutting down Producer");
            continuer.getAndSet(false);
        }
      }
    }
}

class Consumer implements Runnable {
    private final BlockingQueue queue;
    private static AtomicBoolean continuer = new AtomicBoolean(true);

    public void setContinuer(Boolean continuerr) {
        continuer = new AtomicBoolean(continuerr);
    }

    public Boolean getContinuer() {
        return continuer.get();
    }

    @Override
    public void run() {
          while (getContinuer()) {
             //Do some work
             consume(queue.take());
          }
          System.out.println("shut down Consumer");
    }
}

this is what i’m getting:

Shutting down Producer

here:false

finish all

that means that the consumer still working, and the variable “continuer” isin’t updated.

i saw also this and this posts, i tried it, but nothing changed.

what’s my problem ?

EDIT: i changed my code, and apparently the consumer is blocked (waiting if no elements are present on this queue. ) when trying to read data from the BlockingQueue (see consume(queue.take()); in the consumer class).

  • 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-17T18:41:43+00:00Added an answer on May 17, 2026 at 6:41 pm

    Well,

    public void setContinuer(Boolean continuerr) {
        continuer = new AtomicBoolean(continuerr);
    }
    

    Looks wrong. Why don’t you

    public void setContinuer(boolean continuerr) {
        continuer.set(continuerr);
    }
    

    Furthermore, if the end variable is not volatile, it may be cached in threads.


    We will need to see more code. (Or at least compiling code.) Because this code works as expected:

    import java.util.concurrent.*;
    
    public class MyClass {
        static Producer p;
        static Consumer c;
    
        public static void main(String[] args) {
            BlockingQueue q = new LinkedBlockingQueue();
            p = new Producer();
            c = new Consumer();
            Thread t = new Thread(p);
            t.start();
            new Thread(c).start();
            while (true) {
                if (!p.getContinuer()) {
                    c.setContinuer(false);
                    break;
                }
            }
            System.out.println("finish all");
        }
    }
    
    class Producer implements Runnable {
        private boolean end = true;
        private BlockingQueue queue;
        private AtomicBoolean continuer = new AtomicBoolean(true);
    
        public boolean getContinuer() {
            return continuer.get();
        }
    
        @Override
        public void run() {
            while (true) {
                // open socket
                // read data from socket
                if (end) {
                    System.out.println("Shutting down Producer");
                    continuer.getAndSet(false);
                    break;
                }
            }
        }
    }
    
    class Consumer implements Runnable {
        private BlockingQueue queue;
        private static AtomicBoolean continuer = new AtomicBoolean(true);
    
        public void setContinuer(Boolean continuerr) {
            continuer = new AtomicBoolean(continuerr);
        }
    
        public Boolean getContinuer() {
            return continuer.get();
        }
    
        @Override
        public void run() {
            while (getContinuer()) {
                // Do some work
            }
            System.out.println("shut down Consumer");
        }
    }
    

    It prints

    Shutting down Producer
    finish all
    shut down Consumer
    

    Regarding your edit:

    i changed my code, and apparently the consumer is blocked (waiting) when trying to read data from the BlockingQueue (see consume(queue.take()); in the consumer class).

    You should, after your c.setContinuer(false); do consumerThread.interrupt(). The consumer thread that’s blocked in the read method, will throw an InterruptedException, (which you may ignore), and then exit the loop and terminate gracefully.

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

Sidebar

Related Questions

I have this simple scenario: main.m (which is my main class) myClass.m (which is
I have a similar scenario as this one: public class TestLinq2Xml { private XElement
I have a class that acts like this at the moment: public class MyClass
I have this scenario: class A { public virtual int Id { get; set;
Let me give you an example: public class MyClass { public string MyProperty {
Consider this scenario we have a class A which is singleton it has a
I have this scenario where I need data integrity in the physical database. For
we have this scenario: A server which contains needed data and client component which
I have this typical scenario. I have a smartclient application built on .net 2.0
This is a question for a WSS/SharePoint guru. Consider this scenario: I have an

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.