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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:34:05+00:00 2026-06-02T03:34:05+00:00

I think I’m using threads wrong so I wanted to ask if this was

  • 0

I think I’m using threads wrong so I wanted to ask if this was good design. Basically I have a program that pulls data from a queue then processes it(processing is pure math so its 100% cpu intensive), then if the data is good its send to a ‘good’ queue otherwise its either completely discarded or part of it is sent back to the initial ‘work’ queue for further processing. That’s the high level logic of it and when my queue was in-memory my program used all cores and was really fast. As my data grows I decided to use a queue server to store the queue and then distribute the processing over multiple machines and now its slow(only 40%-60% of each core is being used).

I tried to profile my code(using yourkit and the built in one in netbeans) and it says most of the time(80%) is spent on the queue program. I thought I can maybe keep the number crunching going constantly in my program by pushing all external program stuff to another thread, but its not helping with performance and I’m wondering if I’m dong it wrong. I am not sure, but I’m wondering if a launch a thread(child-thread) from an existing thread(parent-thread), does the child one have to be complete before the parent one can finish?

My code is quite large and 99% is not needed, so I’ll just write a high level version of it(it may not compile but should give you an idea of what I’m doing).

public class worker {

    private static ExecutorService executor;
    static {
        final int numberOfThreads = 4;
        executor = new ThreadPoolExecutor(numberOfThreads, numberOfThreads, 1000, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());    
    }
    public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
        // TODO Auto-generated method stub
        System.out.println("starting worker..");
        //Connection information goes here
        channel.basicQos(50); //this is part of the connection, the queue server only gives 50 messages without acknowledgment

        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery(); //gets data from queue
            String message = new String(delivery.getBody());  
            executor.submit(new DoWork(channel, message, delivery));
        }

class DoWork implements Runnable{ //where the main work happens
    //setup variables, basically passing queue connection information as well as data here, so I only need to rely on one connection    
    public void run() {
        new Thread(new Awk_to_Queue(channel, delivery)).start(); //this sends an Awk_to_Queue to the queue, I launch a thread for this so my program can keep working.

        if (data is good) {
            new Thread(new Send_to_Queue("success_queue", message1, channel)).start();                
            continue;
        }  else if (Data is not good but not bad either ) {
        new Thread(new Send_to_Queue("task_queue", message2, channel)).start();             
        } 

class Send_to_Queue implements Runnable{       
    public void run() {
        //takes data in and sends to queue in the way I used to previous do it, but just does it in a thread. queue connection is passed over so I only need to have one connection
    }
}


class Awk_to_Queue implements Runnable{
    public void run() {
        //awk's queue so queue server can send one more piece of data to queue up
    }
}

There it is. I’m sorry if its a bit hard to read(I deleted a lot of stuff just to show you structure of what I’m doing). What am I doing wrong that forking threads did not affect speed(It don’t see it going faster nor is the profiler’s results changed)? Is the problem with the way I’m forking threads ( new Thread(new Awk_to_Queue(channel, delivery)).start();) or is it something else like my design of threading?

  • 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-02T03:34:08+00:00Added an answer on June 2, 2026 at 3:34 am

    Two things come to mind:

    1) The only thread reading the remote queue appears to be the main thread running your infinite loop in the main() method. However fast you stuff things into it, you’ll never process them faster than you can take them back out.

    2) Spawning new Thread();s is an “expensive” operation. Constantly creating new threads for single short tasks is just churning through memory allocation and native resources. You should offload those “queue puts” to a second ExecutorService that you can tune the size of, rather than spawning an unbounded number of threads.

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

Sidebar

Related Questions

I don't think that using .ini or .xml file is a good idea with
I think that this problem can be sorted using reflection (a technology which I'm
I think I'm pretty good at using semantic markup on my pages but I
I have a multi-threaded app that uses Core Data. I've been seeing a lot
I'm trying to implement what I think is a fairly simple design. I have
I'm currently developing an app that uses Core Data. I'm using two stores/configurations, one
I've been looking at C++0x threads and have this code: #include <vector> #include <iostream>
I m using OpenMP and I have a problem with wrong results. Here is
What do you think of using a metric of function point to lines of
I'd like know what people think about using RAISERROR in stored procedures to pass

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.