The Artima Developer Community
Sponsored Link

Java Answers Forum
Threads for calling procedures

2 replies on 1 page. Most recent reply: Aug 16, 2009 10:11 PM by Ranjit Radhakrishnan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Ranjit Radhakrishnan

Posts: 7
Nickname: ranju4all
Registered: Aug, 2009

Threads for calling procedures Posted: Aug 13, 2009 12:32 PM
Reply to this message Reply
Advertisement
I have 2 procedures to be called to build my view layer. Each one of my procedure is taking around 10 seconds execution time. If I go in a sequential way then my wait time will be 20 seconds (after completion on both the calls) and then I will have to take it to the view layer.
Can I use thread implementation in this scenario? Have a Thread A to fetch Procedure 1 and have Thread B to fetch Procedure 2.

Thanks
Ranjit Radhakrishnan


Chris Kelly

Posts: 2
Nickname: bigkahuna
Registered: Aug, 2009

Re: Threads for calling procedures Posted: Aug 15, 2009 1:02 PM
Reply to this message Reply
You probably could use a CyclicBarrier or a CountdownLatch to handle the two different parallel process to run. Each of these constructs allow use to perform an action when the threads themselves have finished.

Thus you could have two threads doing the required units of work as parallel processes. When both threads are finished, another thread would be kicked off which would update your UI via some form of callback.

http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/util/concurrent/CountDownLatch.html
http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/util/concurrent/CyclicBarrier.html

Both of these threading constructs are built into Java 5 onwards as part of the Java Concurrency Library (java.util.concurrent).

There are some good tutorials available on Sun's Java site but a good book to read would be Brian Goetz's Concurrency in practice. It has a good chapter on how you would use threads with Swing so your UI remains responsive. That book's worth its weight in gold :)

Ranjit Radhakrishnan

Posts: 7
Nickname: ranju4all
Registered: Aug, 2009

Re: Threads for calling procedures Posted: Aug 16, 2009 10:11 PM
Reply to this message Reply
Thanks Chris. That really helps.

Flat View: This topic has 2 replies on 1 page
Topic: java program problem.. Previous Topic   Next Topic Topic: GC - Thread safe

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use