Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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

Concurrent threads in batch  RSS feed

 
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.


 
author
Posts: 23832
140
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
 
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
 
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?
 
Sheriff
Posts: 21747
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
Do you pee on your compost? Does this tiny ad?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!