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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:54:48+00:00 2026-06-01T20:54:48+00:00

I think I’m doing it wrong. I am creating threads that are suppose to

  • 0

I think I’m doing it wrong. I am creating threads that are suppose to crunch some data from a shared queue. My problem is the program is slow and a memory hog, I suspect that the queue may not be as shared as I hoped it would be. I suspect this because in my code I added a line that displayed the size of the queue and if I launch 2 threads then I get two outputs with completely different numbers and seem to increment on their own(I thought it could be the same number but maybe it was jumping from 100 to 2 and so on but after watching it shows 105 and 5 and goes at a different rate. If I have 4 threads then I see 4 different numbers).

Here’s snippet of the relevant parts. I create a static class with the data I want in the queue at the top of the program

static class queue_class {
        int number;
        int[] data;
        Context(int number,  int[] data) {
            this.number = number;
            this.data = data;
        }
    }

Then I create the queue after sending some jobs to the callable..

static class process_threaded implements Callable<Void> {
    // queue with contexts to process
    private Queue<queue_class> queue;

    process_threaded(queue_class request) {
        queue = new ArrayDeque<queue_class>();
        queue.add(request);
    }

    public Void call() {
        while(!queue.isEmpty()) {
            System.out.println("in contexts queue with a size of " + queue.size());
            Context current = contexts.poll();
            //get work and process it, if it work great then the solution goes elsewhere
            //otherwise, depending on the data, its either discarded or parts of it is added back to queue
            queue.add(new queue_class(k, data_list)); 

As you can see, there’s 3 options for the data, get sent off if data is good, discard if its totally horrible or sent back to the queue. I think the queues are going when its getting sent back but I suspect because each thread is working on its own queue and not a shared one.

Is this guess correct and am I doing this wrong?

  • 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-01T20:54:49+00:00Added an answer on June 1, 2026 at 8:54 pm

    You are correct in your assessment that each thread is (probably) working with its own queue, since you are creating a queue in the constructor of your Callable. (It’s actually very weird to have a Callable<Void> — isn’t that just a Runnable?)

    There are other problems there, for example, the fact that you’re working with a queue that isn’t thread-safe, or the fact that your code won’t compile as it is written.

    The important question, though, is do you really need to explicitly create a queue in the first place? Why not have an ExecutorService to which you submit your Callables (or Runnables if you decide to make that switch): Pass a reference to the executor into your Callables, and they can add new Callables to the executor’s queue of tasks to run. No need to reinvent the wheel.

    For example:

    static class process_threaded implements Runnable {
        // Reference to an executor
        private final ExecutorService exec;
        // Reference to the job counter
        private final AtomicInteger jobCounter;
        // Request to process
        private queue_class request;
    
        process_threaded( ExecutorService exec, AtomicInteger counter, queue_class request) {
            this.exec = exec;
            this.jobCounter = counter;
            this.jobCounter.incrementAndGet(); // Assuming that you will always
                                               // submit the process_threaded to
                                               // the executor if you create it.
            this.request = request;
        }
    
        public run() {
            //get work and process **request**, if it work great then the solution goes elsewhere
            //otherwise, depending on the data, its either discarded or parts of are added back to the executor
            exec.submit( new process_threaded( exec, new queue_class(k, data_list) ) );
    
            // Can do some more work
    
            // Always run before returning: counter update and notify the launcher
            synchronized(jobCounter){
                jobCounter.decrementAndGet();
                jobCounter.notifyAll();
            }
        }
    }
    

    Edit:

    To solve your problem of when to shut down the executor, I think the simplest solution is to have a job counter, and shutdown when it reaches 0. For thread-safety an AtomicInteger is probably the best choice. I added some code above to incorporate the change. Then your launching code would look something like this:

    void theLauncher() {
    
        AtomicInteger jobCounter = new AtomicInteger( 0 );
    
        ExecutorService exec = Executors.newFixedThreadPool( Runtime.getRuntime().availableProcesses());
    
        exec.submit( new process_threaded( exec, jobCounter, someProcessRequest ) );
        // Can submit some other things here of course...
    
        // Wait for jobs to complete:
        for(;;jobCounter.get() > 0){
            synchronized( jobCounter ){ // (I'm not sure if you have to have the synchronized block, but I think this is safer.
                if( jobCounter.get() > 0 )
                    jobCounter.wait();
            }
        }
    
        // Now you can shutdown:
        exec.shutdown();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think that there are some issues with the earth Gravity, so I wonder
I think this is a relatively simple problem. I wish to obtain some functionality
Think I have domain.com that redirects into some.otherdomain.com . I don't want to show
Think of a dictionary application that grabs words upon user's click from any other
I think the file that is produced is an .asm file, any idea how
I think most people here understand the importance of fully automated builds. The problem
I think that java executables (jar files) are trivial to decompile and get the
Think of the following code: static int Main() { byte[] data = File.ReadAllBytes(anyfile); SomeMethod(data);
I think that asking this might be kind of silly but I'm still wondering
Think tic-tac-toe. You are creating the buttons in rows and columns and you want

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.