This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

How to synchronize multiple theads  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have to start N tasks in parallel. Any tasks must use a parameter (i) to perform some executions and they take few seconds or a lot of minutes, it depends on 'i' param.
In my tasklist, I can have also multiple values of 'i' to repeat the same task; for example [1,2,3,1,2,3].
Of course, if a task finishes sooner the execution, it can proceed to the next task.
Any tasks return true or false to define if execution is ok or not. If task(x) is ok I can run the next task (and jump all next tasks with x param because x is ok)
Let suppose to use N=2 (2 thread), with task 1 and task 2 are fast, and task 3 takes a lot of time;
there is this possibility that more thread can run on the same task(i) because they are running, and I want to avoid this.
For example the thread1 is running the task(3) and the thread2 can run also task(3) (after task(1) and task(2) are ok and fast) because for param 3 there isn't OK yet.  



Is it correct to use newFixedThreadPool?
Could you help me?
Thank you in advance.
 
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want task3 to run at the same time or what?  newFixedThreadPool means you will have a set number of threads in the pool hence the 'fixed.'
 
pasquale vitale
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replay,
I don't want to run 2 threads that use the same 'i' param; for example 2 thread: task(3) and task(3)
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are they running over and over again?  Why don't you put them on a stack.   Or you can add all of them at the same time to the executor.  Basically your problem is you don't want duplicates and there are many ways you can do that.
 
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best solution depends on a few factors.

First, must tasks start executing in order that you define in the task list? Let's say that first task 1 and task 2 start executing. Let's say that task 2 then fails and the thread starts executing task 3. If task 1 fails shortly after, must its thread then start executing task 1 a second time, or is it allowed to start executing task 2 a second time?

Secondly, what happens if tasks fail a second time? If you run task 1 a second time because it failed the first time, and then it fails again, must a third attempt be made or is that the end of it?

If you don't care about execution order, you just register each task once, and when the task fails it registers a new attempt with the executor.

Don't extend Thread to create tasks. Implement Runnable or Callable.
 
pasquale vitale
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for replay, guys.
To be honest, here my requirements (for example N=2, i=3, X=3):
1. start N process in parallel with different 'i' param; so I need to run 3 tasks starting form 1 to i: task(1), task(2), task(3)
2. execute any tasks X times
3. if an instance finishes sooner, it can proceed to the next task

I've used Executors.newFixedThreadPool(N) and my params are i*X = 9, i.e. [1,2,3,1,2,3,1,2,3].
At this point my test was ok. I run 9 tasks, and only N=2 threads at time.

The problem is when I've added a new requirement: if a task(i) is ok then it's not necessary to run again for that 'i' but If it fail, I can run again, of course, until 3 times.
I tried to add in my Task a list of 'i' param (List<Integer> completed) where the execution is OK; so I check, in the run method, if that 'i' param is in the list to jump the execution.
But in this case there is the possibility to run 2 tasks with same 'i' param if that 'i' is not in the completed list, and I want to avoid this, for example I want to wait this execution.

Now I don't know if this approach is fine after this new requirement or I have to change approach.
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your requirements don't state if the tasks have to be started in the order [1, 2, 3, 1, 2, 3, 1, 2, 3] or whether it can also be [1, 2, 3, 2, 1, 2, 3, 1, 3].

Again, if the order doesn't matter, the easiest way to do this is to just let the task register itself another time when it fails. That way, execution of the next instance of task 3 would only start if the previous execution failed, and if the previous execution succeeded, then the next execution is never registered in the first place, thereby skipping it.
 
pasquale vitale
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan,
your solution is great, but I cannot start N thread at same time.
Anyway, I used my solution and in the Task thread class I'm using a synchronized clause on param 'i' (object = Integer),
so I'm sure that the next thread waits the end of previous task.
In the run() method I'm using also List<Integer> completed to jump all next task(i)
 
Stephan van Hulst
Saloon Keeper
Posts: 10206
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you solve it?

What's wrong with my solution? You say that you can't start N threads at the same time, but in the post before you say that a requirement is to "start N process [sic] in parallel". Your requirements are unclear.
 
Message for you sir! I think it is a tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!