Callable thread for singleThreadExecutor is working as synchronously

Saravana Kumar

I have tested the below callable sample code

ExecutorService executorService = Executors.newSingleThreadExecutor();
Future<String> futureResMap = executorService.submit(new Callable<String>() {
    @Override
    public String call() throws Exception {
        return "thread:1";
    }
});
executorService.shutdown();
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("Thread response:"+futureResMap.get());
System.out.println("4");
System.out.println("5");

and gets the output always as below

1
2
3
Thread response:thread:1
4
5

Which means Callable with single thread executor runs synchronously always.

When i tried for runnable as below

ExecutorService executorService = Executors.newSingleThreadExecutor();  
executorService.execute(new Runnable() {
    @Override
    public void run() {
        System.out.println("Thread response:thread:1");
    }
});
executorService.shutdown();
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");

Which provides different output every time.

Run:1

1
2
Thread response:thread:1
3
4
5

Run 2:

1
2
3
Thread response:thread:1
4
5

So according to my point of view for Callable with single thread executor is working as synchronous. Callable is mostly for multithreaded implementation. Is my understanding correct?

Sergey Afinogenov

Method get of Future class retrives result of a task, waiting endlessly if it is not avalible.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SingleThreadExecutor VS plain thread

Object is not callable - Thread

NodeJS - requiring module is not working synchronously

SingleThreadExecutor and ThreadFactory

How to give name to a callable Thread?

Spawning threads in a thread with callable object

How to guarantee that a Task runs synchronously on the current thread?

Running asynchronous code synchronously in separate thread

Text to Speech Audio not working synchronously in browser

Firebase callable functions response not working

Is it Possible to implement timer task with Callable Thread in java?

What happens to main thread when using a callable

TypeError: dict object is not callable (inside Thread function)

Create boost::thread without callable object

Start and stop Process Thread from Callable

Callable and future delay android main thread

Simple Implementation of Callable with Thread Executor and notify

Does .NET Task.Result block(synchronously) a thread

What happens to the callee's thread when an async function is invoked synchronously

WKWebView: trying to query javascript synchronously from the main thread

IIS executes custom error handler synchronously in a single thread

How to dispatch blocks with many arguments on a different thread synchronously?

Synchronously post a message to a worker_thread. Sending thread blocked, worker_thread unblocked

Writing a file using ExecutorService and Callable in Java not working

Callable Firebase Cloud Function with region is not working

Template Specialization With Callable Argument Not Working As Expected

Bound Callable Reference Not Working With Reactor Subscribe

Working with Strings/Text, TypeError: 'NoneType' object is not callable

java StructuredTaskScope.joinUntil is not working for blocking Callable