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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:02:27+00:00 2026-06-16T06:02:27+00:00

I am new to ExecutorService and wonder why following code prints correctly 10 15,

  • 0

I am new to ExecutorService and wonder why following code prints correctly “10 15”, even though I have created only one thread to process the timeouts? Why can I call schedule many times without previous tasks being cancelled on a single thread executor?

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class TestExecutorService implements Runnable {
    public static ScheduledExecutorService SERVICE = Executors.newSingleThreadScheduledExecutor();
    private int delay;


    public TestExecutorService(int delay) {
        this.delay = delay;
    }

    public void run () {
        System.out.println(delay);
    }

    public static void main (String[] args) {
        SERVICE.schedule(new TestExecutorService(10), 10, TimeUnit.SECONDS);
        SERVICE.schedule(new TestExecutorService(15), 15, TimeUnit.SECONDS);

        SERVICE.shutdown();
    }
}
  • 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-16T06:02:28+00:00Added an answer on June 16, 2026 at 6:02 am

    Reading the Javadoc would be very helpful in this case. It does explain that though the executor will be created with a single thread, it will be operating with an unbounded queue. This means that you can submit multiple tasks to the executor and they will be queued up to run one after the other, up to the maximum bounds of the queue (which in this case is infinity) or until the JVM runs out of resources.

    Creates an Executor that uses a single worker thread operating off an
    unbounded queue. (Note however that if this single thread terminates
    due to a failure during execution prior to shutdown, a new one will
    take its place if needed to execute subsequent tasks.) Tasks are
    guaranteed to execute sequentially, and no more than one task will be
    active at any given time. Unlike the otherwise equivalent
    newFixedThreadPool(1) the returned executor is guaranteed not to be
    reconfigurable to use additional threads.

    In your example your two tasks get queued up, and run sequentially one after the other, which is why you get the (expected) output.

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

Sidebar

Related Questions

One of actions in Struts2 is using below code: java.util.concurrent.ExecutorService myservice = Executors.newSingleThreadExecutor(); myservice.execute(new
Having the following code at hand: ExecutorService executor = Executors.newFixedThreadPool(10); Collection collection = new
My code snippet: ExecutorService executor = Executors.newSingleThreadExecutor(); try { Task t = new Task(response,inputToPass,pTypes,unit.getInstance(),methodName,unit.getUnitKey());
I have a fixed thread pool ExecutorService of width 10, and a list of
I have an ExecutorService which executes tasks which itself submit new tasks to the
Suppose I have an ExecutorService (which can be a thread pool, so there's concurrency
So I have this code here, that adds my NamePrinter runnable class as ExecutorService
I'm new to the package java.util.concurrent . I've created the following program testing if
In this code example, the ExecutorService is used one and allowed to go out
I meet a weired problem when using JUnit in multi-thread environment. The following code

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.