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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:37:53+00:00 2026-06-11T13:37:53+00:00

In java, what is an efficient solution to the following problem: I have multiple

  • 0

In java, what is an efficient solution to the following problem:

I have multiple threads (10-20 or so) generating jobs (“Job Creators”), and a single thread capable of performing them (“The worker”). Once a job creator has posted a job, it should wait for the job to finish, yielding no result other than “it’s done”, before it keeps going.

For sending the jobs to the worker thread, I think a ring buffer or similar standard fan-in setup would perhaps be a good approach? But for a Job Creator to find out that her job has been done, I’m not so sure..

The job creators could sleep, and the worker interrupt them when done.. Or each job creator could have an atomic boolean that it checks, and that the worker sets. I dunno, neither of those feel very nice. I’d like to do it with as few (none, if possible) locks as absolutely possible. So to be clear: What I’m looking for is speed, not necessarily simplicity.

Does anyone have any suggestions? Links to reading about concurrency strategies would also be very welcome!

  • 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-11T13:37:55+00:00Added an answer on June 11, 2026 at 1:37 pm

    There’s a couple ways to do this. The fastest way would probably be to just use Semaphore and a single-threaded executor.

    private final Executor executor = Executors.newSingleThreadExecutor();
    
    public void submitJob() {
        Semaphore semaphore = new Semaphore(0);
        executor.execute(new Job(semaphore));
        semaphore.acquire(); // Will block until semaphore.release() below
    }
    

    Then in the Job class:

    public class Job implements Runnable {
    
        private final Semaphore semaphore;
    
        public Job(Semaphore semaphore) {
            this.semaphore = semaphore;
        }
    
        @Override
        public void run() {
            // Perform task and run the actual job
            semaphore.release(); // Cues the submitter to continue at semaphore.acquire()
        }
    }
    

    Semaphore is decently fast, much faster than using synchronized blocks.

    Edit: I should also note that this avoids a lot of overhead from Future, and so is probably also faster than the Future implementation suggested by others, but without testing it myself, I can’t be sure.

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

Sidebar

Related Questions

I have the following problem and wonder whether there is an efficient solution to
I have to store key value pairs in java which is more memory efficient
Consider following assumptions: I have Java 5.0 Web Application for which I'm considering to
I'm looking for the most efficient Java method that would allow me to 'delete
What's the most efficient way in Java to get the 50 most frequent words
What is the most efficient way to reverse a string in Java? Should I
Java 7 is supposed to fix an old problem with unpacking zip archives with
Java noob question: Consider the following C array and initializer code: struct { int
I have the following ( doctored ) class in a system I'm working on
I have a database that is similar to the following: create table Store(storeId) create

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.