Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tune single Thread into SingleThreadExecutor #5409

Closed
Tracked by #5362
halibobo1205 opened this issue Aug 9, 2023 · 5 comments · Fixed by #5410
Closed
Tracked by #5362

Tune single Thread into SingleThreadExecutor #5409

halibobo1205 opened this issue Aug 9, 2023 · 5 comments · Fixed by #5410
Assignees

Comments

@halibobo1205
Copy link
Contributor

halibobo1205 commented Aug 9, 2023

The SingleThreadExecutor pool consists of just one thread. It executes the submitted tasks sequentially. If an exception occurs and the thread gets terminated, a new one is created.

Thread vs. Single Thread Executor Service

1. Task Handling

Threads can only handle Runnable tasks, whereas a single thread executor service can execute both Runnable and Callable tasks. Therefore, using this, we can also run tasks that can return some value.

The submit() method in the ExecutorService interface takes either a Callable task or a Runnable task and returns a Future object. This object represents the result of an asynchronous task.

Also, a thread can handle just one task and exit. But a single thread executor service can handle a series of tasks and executes them sequentially.

2. Thread Creation Overhead

There is an overhead involved in creating threads. For instance, JVM needs to allocate memory. It impacts performance when threads are created repeatedly in the code. But in the case of a single thread executor service, the same worker thread is reused. Therefore, it prevents the overhead of creating multiple threads.

3. Memory Consumption

Thread objects take a significant amount of memory. Therefore, if we create threads for each asynchronous task, it can lead to OutOfMemoryError. But in a single thread executor service, the same worker thread is reused, which leads to less memory consumption.

4. Release of Resources

A thread releases resources once its execution completes. But in the case of executor service, we need to shut down the service or the JVM won't be able to shut down. Methods like shutdown() and shutdownNow() shutdown the executor service.

@tomatoishealthy
Copy link
Contributor

Wondering all tasks which are create by new Thread() will migrate to ONE SingleThreadExecutor ?

Means these tasks can't run in parallel?

@halibobo1205
Copy link
Contributor Author

Wondering all tasks which are create by new Thread() will migrate to ONE SingleThreadExecutor ?

Means these tasks can't run in parallel?

Yes, optimization is only for tasks that cannot be parallelized, such as rePush, DPosMiner task.

@yuekun0707
Copy link

Wondering all tasks which are create by new Thread() will migrate to ONE SingleThreadExecutor ?
Means these tasks can't run in parallel?

Yes, optimization is only for tasks that cannot be parallelized, such as rePush, DPosMiner task.

Will it be possible that some tasks block the executor? Are there timeout or other ways we can do to terminate the abnormal task?

@halibobo1205
Copy link
Contributor Author

Wondering all tasks which are create by new Thread() will migrate to ONE SingleThreadExecutor ?
Means these tasks can't run in parallel?

Yes, optimization is only for tasks that cannot be parallelized, such as rePush, DPosMiner task.

Will it be possible that some tasks block the executor? Are there timeout or other ways we can do to terminate the abnormal task?

In fact, most of the thread execution is a while (true) logic , see https://github.com/tronprotocol/java-tron/pull/5410/files#diff-f208d2bedf0ca1d3e795cc8f42e520e273735ea25536bba295c274e7531e0bdfL48

@yuekun0707
Copy link

Wondering all tasks which are create by new Thread() will migrate to ONE SingleThreadExecutor ?
Means these tasks can't run in parallel?

Yes, optimization is only for tasks that cannot be parallelized, such as rePush, DPosMiner task.

Will it be possible that some tasks block the executor? Are there timeout or other ways we can do to terminate the abnormal task?

In fact, most of the thread execution is a while (true) logic , see https://github.com/tronprotocol/java-tron/pull/5410/files#diff-f208d2bedf0ca1d3e795cc8f42e520e273735ea25536bba295c274e7531e0bdfL48

If thread executor process tasks one by one, will while (true) block the tasks list?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants