• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Concurrent threads in batch

 
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 43
Netbeans IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic