Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Concurrent threads in batch  RSS feed

 
chryssa neoh
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Lets say I have 10 transactions in a list where each batch consist of 5 transactions.

If all the first 5 transactions is complete (batch 1), then only the next batch is allowed to proceed.

Each transaction represents a thread in order to improve the performance as previously the list of transactions is processed one by one.

My question is when join() is used, isn't it the same concept as processing the transactions one by one?

Please advice. Thanks.


 
Henry Wong
author
Sheriff
Posts: 22867
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chryssa neoh wrote:
My question is when join() is used, isn't it the same concept as processing the transactions one by one?


Correct. The way you coded your program, there is effectively no concurrency. You have at most two threads running at a time; and one of the two threads is doing nothing but waiting for the other one.

Henry
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might with to consider making use of some of the new thread support classes eg CountDownLatch

http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/CountDownLatch.html
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

CountDownLatch will get the job done.
Since there are multiple batches of transactions,consider using a CyclicBarrier so that you can reuse it for subsequent transactions.

The easiest/dirtiest solution to the problem is to use join() after starting all threads that are part of a transaction.
In this specific snippet, place join() after the inner for loop.

Hope that helps.
 
chryssa neoh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I were to use the easiest/dirtiest solution by placing join() after the inner loop, does it still makes it run concurrently?
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Directly after that loop you have 5 running threads. You only then wait for all of them to stop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!