This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

SingleThreadPoolExecutor Usage  RSS feed

 
Ranch Hand
Posts: 370
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have one implementation of SingleThreadPoolExecutor in Java Concurrency package. It actually sequences the steps of execution rather than executing them in parallel. The main use of multithreading approach is to execute the program in parallel. So, I just want to understand the concept and rationale behind SingleThreadPoolExecutor?
 
Bartender
Posts: 9493
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But possibly still in a different thread than the one that submitted the task to the executor, right?

By the way, you're confusing the terms "parallel" and "concurrent". Yes, executor services are used to execute tasks in parallel, but you can execute tasks in parallel using only one thread. When you execute two tasks in parallel at the same time, that's called "concurrent".

An example of this is JavaScript. JavaScript makes heavy use of callbacks to execute tasks in parallel, but all of JavaScript only ever runs on one thread.
 
Vaibhav Gargs
Ranch Hand
Posts: 370
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan. I am bit confused that how can we execute tasks in parallel using one thread?
 
Stephan van Hulst
Bartender
Posts: 9493
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parallel just means "at some point in the future, you don't care when".

An executor service is free to decide for itself when to execute pending tasks. An executor service may just pick a random task that's waiting to be executed. From the point of view of the executor service, this process is serial, but for the outside world it looks parallel.

When we say that two tasks can be performed in parallel, it doesn't necessarily mean "at the same time". It means the outcome of running those tasks doesn't depend on the order in which those tasks are executed.
 
Vaibhav Gargs
Ranch Hand
Posts: 370
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan.

So, is it correct to say that on a machine with single core cpu, we will have only parallel execution and on machines with multiple cores CPU/multiple CPUs, we can achieve concurrency.
 
Marshal
Posts: 61713
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vaibhav Gargs wrote:. . . on a machine with single core cpu, we will have only parallel execution and on machines with multiple cores CPU/multiple CPUs, we can achieve concurrency.

I lways thought it was the other way round.
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I lways thought it was the other way round.



I thought so too....

Regardless, these two terms seems to be used interchangeably (the regards to threading) these days.

Henry
 
Vaibhav Gargs
Ranch Hand
Posts: 370
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Campbell Ritchie wrote:I lways thought it was the other way round.



I thought so too....

Regardless, these two terms seems to be used interchangeably (the regards to threading) these days.

Henry



Hi Henry, Campbell,  concurrent doesn't mean two threads of execution at same point of time? Is my understanding incorrect?
 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parallel computing + Shared Resource is concurrency.

Parallel computing  - Each thread does its own task and it doesnt depend on each other. Its similar like you 5 dogs and 5 bowls to feed the dogs. Now each dog will quietly eat from its own bowl, and are happy. Thing about five dogs, but you have only one bowl to feed. Now the dogs needs to be disciplined to avoid any chaos, this is called concurrency. The discipline is enforced using locks/conditions.
 
Stephan van Hulst
Bartender
Posts: 9493
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not necessarily true. You can write a program that contains race conditions and it's still said to be concurrent. In fact, you can't have race conditions unless you have multiple threads running concurrently.

As for the difference between "concurrent" and "parallel": I've always understood that "parallel" refers to the dependency graph of tasks. You can draw a directed graph without cycles of tasks that a program needs to perform from start to end. Tasks that don't depend on each other are drawn parallel to each other. They can be performed at the same time, but it's not necessary.

"Concurrent" refers to actual threads that are running. "Concurrent" literally means "at the same time".
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!