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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:48:51+00:00 2026-06-11T23:48:51+00:00

I have to do schoolwork, and I have some code done, but got some

  • 0

I have to do schoolwork, and I have some code done, but got some questions:

must create a boss-workers application in java.

  1. I have these classes: Main WorkerThread BossThread Job

Basically what I want to do is, that BossThread holds a BlockingQueue and workers go there and look for Jobs.

Question 1:

At the moment I start 5 WorkingThreads and 1 BossThread.

Main:

Collection<WorkerThread> workers = new ArrayList<WorkerThread>();
    for(int i = 1; i < 5; i++) {
        WorkerThread worker = new WorkerThread();
        workers.add(worker);
    }
BossThread thread = new BossThread(jobs, workers);
thread.run();

BossThread:

private BlockingQueue<Job> queue = new ArrayBlockingQueue<Job>(100);
private Collection<WorkerThread> workers;

public BossThread(Set<Job> jobs, Collection<WorkerThread> workers) {
    for(Job job : jobs) {
        queue.add(job);
    }
    for(WorkerThread worker : workers) {
        worker.setQueue(queue);
    }
    this.workers = workers;
}

Is this normal, or I should create WorkerThreads in my BossThread ?

Question 2:

As you see I am giving the queue to each WorkerThread , is that reasonable or I could store the queue only in one place?

Question 3:

Must I keep my BossThread running somehow, just to wait if user adds more stuff to queue? And how I keep WorkerThreads running, to look for jobs from queue?

Any overall suggestions or design flaws or suggestions?

public class WorkerThread implements Runnable {

    private BlockingQueue<Job> queue;

    public WorkerThread() {

    }

    public void run() {
        try {
            queue.take().start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void setQueue(BlockingQueue<Job> queue) {
        this.queue = queue;
    }
}
  • 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-11T23:48:52+00:00Added an answer on June 11, 2026 at 11:48 pm

    Firstly, one important mistake I noticed:

    BossThread thread = new BossThread(jobs, workers));
    thread.run();
    

    Runnables must be passed to a Thread object and threads are started with start, not run. By calling run you get sequential execution on the same thread. So:

    Thread thread = new Thread(new BossThread(jobs, workers)));
    thread.start();
    

    Secondly, unless you absolutely must use BlockingQueue and explicit threads I would instead use ExecutorService. It neatly encapsulates a blocking work queue and a team of workers (whose size you can set). It’s basically what you’re doing but much simpler to use:

    class Job implements Runnable {
        public void run() {
            // work
        }
    }
    
    ...
    
    // create thread pool with 5 threads and blocking queue
    ExecutorService exec = Executors.newFixedThreadPool(5);
    
    // submit some work
    for(int i = 0; i < 10; i++) {
       exec.submit(new Job());
    }
    

    And that’s it! All the put and take stuff is handled by the executor automatically.

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

Sidebar

Related Questions

have a nice day. I got problem when trying to create an image from
Have some code: using (var ctx = new testDataContext()) { var options = new
have you got your maths hat on ? I am doing some credit card
I have some PHP 5.3 code which builds an array to be passed to
Have done quite a bit of searching for a guide (of any substance) for
have 2 questions : A computer with 32-bit address uses 2-level page table (9
im doing an exercise where i have to make a parking lot application. The
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Have converted devise new session from erb to Haml but doens't work, this is
Have searched the database but need to specifically sum(of hours flown or days off)in

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.