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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:49:23+00:00 2026-06-13T08:49:23+00:00

I want to have 3 threads doing a task concurrently and also I want

  • 0

I want to have 3 threads doing a task concurrently and also I want a thread that do a scheduled task over a period of time (runs every 10 secs). But I want when the 3 threads finish to run only once the scheduled task and then I want this thread to terminate.
What is the best way to implement these threads? Is the ExecutorService Interface suitable for this.

  • 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-13T08:49:24+00:00Added an answer on June 13, 2026 at 8:49 am

    Here an example that I’ve just written:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledFuture;
    import java.util.concurrent.TimeUnit;
    
    public class Example {
    
        public static void main(String[] args) throws InterruptedException {
            ScheduledFuture future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new MyScheduledTask(), 0, 10, TimeUnit.SECONDS);
            ExecutorService executor = Executors.newFixedThreadPool(3); // pool composed of 3 threads
            for (int i = 0; i < 3; i++) {
                // for the example assuming that the 3 threads execute the same task.
                executor.execute(new AnyTask()); 
            }
            // This will make the executor accept no new threads
            // and finish all existing threads in the queue
            executor.shutdown();
            // expect current thread to wait for the ending of the 3 threads
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.TimeUnit.NANOSECONDS); 
            future.cancel(false); // to exit properly the scheduled task
            // reprocessing the scheduled task only one time as expected
            new Thread(new ScheduledTask()).start(); 
    
        }
    
    
    }
    
    class MyScheduledTask implements Runnable {
        @Override
        public void run() {
            //Process scheduled task here
        }
    }
    
    class AnyTask implements Runnable {
        @Override
        public void run() {
            //Process job of threads here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have a Task that runs on a background thread and does some
In Ruby, I want to have two threads running at the same time, and
I have 100 threads. I want to run 10 thread before 10 another threads
I have a for loop that spawns threads... I want it to spawn 5
I have a map that i want to update from a separate thread. Im
i have a mainFrame that contains panel. i want in this panel a thread
I have one requirement that I want to create a pool of 5 threads
I need to write a simple app that runs two threads: - thread 1:
I have an array of threads, and I want to Join them all with
I have thread where downloading xml file a this xml file i want to

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.