Sie sind auf Seite 1von 8

7/14/13

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Threads and Synchronization

Author

Problem is Parallel Processing of Jobs in Java


posted 6/22/2009 11:41:19 PM

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

Ranchers, we have a application where Jobs are running concurrently and which is updating the database column in a table. Hence we are noticing that it is not updating the database column with proper value. How do we handle this scenario of parallel processing/updating of values in database in java ??

Example : Job 1 triggers and runs a update query ,it successfully updates the value in DB(oracle) Job 2 is also triggered parallelely and runs a update query,it is executing the update query but the value is not getting reflected in database and hence in the app

we have checked for connections and other relevant details.all has been properly handled/closed connections. we have a "Auto commit" enabled ,so inspite of that in the second scenario,the update query is getting executed but the proper value is not getting reflected in da Example of snippet code There will be "N" number of jobs which will call the below updateDetails() method parallely.so need help to resolve this in Java.

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 .

p u b l i cv o i du p d a t e D e t a i l s ( ) { / / r u nu p d a t eq u e r y }

Need help guys

When The Going Gets Tougher,The Tougher gets Going Marco Ehrentreich best scout Bartender Joined: Mar 07, 2007 Posts: 1225
I like...

posted 6/22/2009 11:52:03 PM

Hi Deepak,

are you trying to update the same column in the same row or what? In this case the reason would most probably be that one job is simply overwriting changes of update the same database value multiple times directly after one another.

Anyway, it's definitely necessary to control correct synchronization of multiple concurrent jobs if they could possibly interfere with each other, i.e. they update som synchronization mechanisms in the Java code. Another solution would of course be to use correct transaction handling for the database queries. As I don't think yo multiple times it's more likely that the correct values get lost in your code because of thread interference.

Without more information I can hardly imagine any other reason why you should lose some updates. If the outcome of concurrent jobs is more or less unpredictab Marco

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507

posted 6/23/2009 1:44:20 PM

Anyway, it's definitely necessary to control correct synchronization of multiple concurrent jobs if they could possibly interfere with each other, i.e. they update some shared data mechanisms in the Java code.

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

1/8

7/14/13
I like...

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)


mechanisms in the Java code.

yes, it is to do with sychronization as you have indicated. how do i handle it in this scenario ???
Marco Ehrentreich best scout Bartender Joined: Mar 07, 2007 Posts: 1225
I like...

posted 6/23/2009 1:55:18 PM

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 .

h o wd oih a n d l ei ti nt h i ss c e n a r i o? ? ?

Well, by using correct synchronization

But seriously, it's difficult to tell if this problem is better fixed on the Java (thread synchronization) side or database side (

code as I suspect the root of these problems being there.

Unfortunately correct synchronization and multithreading in general for any non-trivial application is probably one of the most difficult things in software developme

here without knowing your application very well. As an advice start by looking for data (ususally object or class members) which could pontentially be updated or r

correctly synchronize every methods that could manipulate these data. This may sound very simple but it isn't. It's not sufficient to put the "synchronized" keyword

easy rule to solve problems with concurrency is often to avoid member variables whenever possible. If it's possible to use local variables instead, use it. They are i can't change their state after creation, are thread-safe by default, too. Besides that it's hardly possible to give general rules which work for all concurrency problem Additionally you will have to learn at least the basics of concurrency in Java. You could start with Sun's tutorial for example. I'm sorry that I really can't give you more helpful advices but feel free to ask any question that may come up Marco

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 6/24/2009 12:00:57 AM

Ranchers, we are still stuck up with this problem.

There are more than 1 job running in parallel.so we want all the jobs to complete and then based on all jobs status(i.e if OK then update the status of some other The scenario is as below

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 .

A s s u m ew eh a v eT A B L E 1a sb e l o w : 1 s tj o bc o m p l e t e dr u n n i n ga n du p d a t e st h es t a t u st oO Kb yr u n n i n ga nu p d a t eq u e r y( s oS T A T U Si nT A B L E 1i sO K) >1 s tr o w 2 n dj o bi ss t i l lr u n n i n ga n di si nI N( S t a t u si nT a b l e )>2 n dr o w 3 r dj o bi ss t i l lr u n n i n ga n di si nI N( S t a t u si nT a b l e )>3 r dr o w 4 t hj o bi ss t i l lr u n n i n ga n di si nI N( S t a t u si nT a b l e )>4 t hr o w

I nt h em e a n w h i l e ,t h eF i r s tj o bh a sc o m p l e t e dt h ej o ba n dh e n c e s t a t u si s" O K "n o w .n o ww ea r ec a l l i n ga n o t h e ru p d a t eq u e r yw h i c hu p d a t e s A B L E 2 )i nd a t a b a s e . T h eq u e r yi s"U p d a t eT A B L E 2s e tS T A T U S = " O K "w h e r eI N I T _ N O = ' 1 2 3 4 5 6 7 8 9 'A N DN O TE X I S T S ( S E L E C TC O U N T ( * )F R O M T A B L E 1W H E R ES T A T U Si n ( H e n c et h es u b q u e r yi sr e t u r n i n g3r o w sa sc o u n tf o rt h ei n n e rq u e r ye x e c u t i o nw h i c hs k i p st h eu p d a t i o no ft h eT A B L E 2 .

This is happening only when it is a parallel processing of jobs,so the requirement is that we first want all the 4 jobs to complete their running and become to OK st be fired. Please advice.How we can apply synchronization to the code.?? Need Help Java Experts...

Deepak

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 6/24/2009 12:38:46 AM

Sun has a pretty good concurrency tutorial, I think there is already a link to it in this thread. You should read it to find some strategies.

6
I like...

Some tools that may make things easier: - If the number of tasks you have is known before you start the tasks, then you should look at using a java.util.concurrent.CountDownLatch. You generate the cou you plan on running. At the end of each parallel task they countDown() on the latch. Your final update to table 2 task await()s until all of the other tasks reached c

- If the number of tasks you have are variable, and new tasks can be begun while others are finishing, and you want to make sure the update table 2 task is comp java.util.concurrent.locks.ReentrantReadWriteLock, where each of the parallel tasks takes a read lock, and the update to table 2 task takes a write lock. The upda complete. But would allow more parallel tasks to be completed after the update table 2 task.

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

2/8

7/14/13
Steve

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 6/24/2009 1:06:52 AM

Hi Steve, Thanks for the inputs,Im new to threads.how should i go about implementation for java.util.concurrent.CountDownLatch i have below sqls being fired

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .

p u b l i cv o i de x e c u t e ( ) { d a o . u p d a t e T a b l e 1 ( ) ;/ /c a l l sT a b l e 1a n du p d a t e si tt oO K .>T h i sh a st ob ep e r f o r m e df o rnn u m b e rf o rj o b s . F i n i s ha l lj o bc o m p l e t i o na n dt h e np r o c e e db e l o w . / / O n c eU p d a t e T a b l e 1i sc o m p l e t e d, a f t e rt h i sw ep e r f o r mt h eb e l o ws t e p d a o . u p d a t e T a b l e 2 ( ) ;/ / c a l l sT a b l e 2a n du p d a t e si tt oO K }

Since im new to Concurrency can you help me how should i refine my code to handle java.util.concurrent.CountDownLatch I had one more clarification,how do we implement CountDownLatch in jdk1.4 since i read in api it is since 1.5 Please advice and suggest... Deepak

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 6/24/2009 1:26:14 AM

I suggest you read, and understand thoroughly Sun's Concurrency Turorial, found here: http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html It is too big of a subject to do in a forum. Read the tutorial, write some code and when you have problems come back. This just isn't a good medium for teaching s

6
I like...

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 6/24/2009 1:29:12 AM


Deepak Lal wrote:

how do we implement C ountDownLatch in jdk1.4 since i read in api it is since 1.5

6
I like...

Woops, I missed that part.

First: Java 1.4 is past its End Of Life. You should do what you can to NOT use it. Java 5 is near its End Of Life as well. Do yourself a favor and update to Java 6. Second: If you can't update to a new version of java, there is a back-port of the java.util.concurrent package. You should do a Google search for 'java.util.concurre

Marco Ehrentreich best scout Bartender Joined: Mar 07, 2007 Posts: 1225
I like...

posted 6/24/2009 1:57:35 AM

Hi Deepak,

as Steve pointed out, too, there's really no other way than to learn about the basics (or more) of concurrency yourself! If it would be that easy, I'd be glad to help

If you only take some advices from us here without really understanding it, it's likely that you will mess up your code completely. Furthermore it's usually not suffici expected. Concurrency should be considered for the whole application design or at least the parts of your application that are confronted with concurrency effects by someone who knows the application and concurrent programming quite well. Anyway, this is a subject worth the effort for learning as it will become more and more important with multi-core/multi-processor computers in the future ;-) Marco

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 6/24/2009 11:13:14 PM

I do agree with you Marco and Steve,but please atleast help me with this for now.

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 .

p u b l i cv o i de x e c u t e ( ) { d a o . u p d a t e T a b l e 1 ( ) ;/ /c a l l sT a b l e 1a n du p d a t e si tt oO K .-

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

3/8

7/14/13
0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)


d a o . u p d a t e T a b l e 1 ( ) ;/ /c a l l sT a b l e 1a n du p d a t e si tt oO K .>T h i sh a st ob ep e r f o r m e df o rnn u m b e rf o rj o b s . F i n i s ha l lj o bc o m p l e t i o na n dt h e np r o c e e db e l o w . / / O n c eU p d a t e T a b l e 1i sc o m p l e t e d, a f t e rt h i sw ep e r f o r mt h eb e l o ws t e p d a o . u p d a t e T a b l e 2 ( ) ;/ / c a l l sT a b l e 2a n du p d a t e si tt oO K }

Atleast in the scenario explained.do you think it is feasible to use a synchronized block for updateTable1() and updateTable2() methods as shown in the Posts ear Im a newbie to all these concepts and im stuck with this... Please help.

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 6/25/2009 12:06:16 AM

If we take the code you posted to mean: You want to do:


view plain c opy to c lipboard print ?

6
I like...

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 .

d a o . u p d a t e T a b l e 1 ( ) ;

n times, and each concurrently. Then when they are all complete, you want to do:
view plain c opy to c lipboard print ?

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 .

d a o . u p d a t e T a b l e 2 ( ) ;

Then the answer is: Yes. Like I previously said, CountDownLatch would work real well here if n is a known value. You would create a number of runnable tasks (pe The tasks that do the dao.updateTable1() would call CountDownLatch#countDown() when they finish. The task that does dao.updateTable2() would call CountDo second task must wait for all the other tasks to complete before running.

If n is not known before you start executing taks then you need to use some other tool. For example, the ReadWriteLock method I mentioned earlier, or by using t tasks are submitted to wait for them to complete. I can't change the code you posted to make it work. You will have to read up on how this stuff works to figure that out.

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 6/25/2009 12:33:16 AM

Hi Steve, Do you have any Sample Code Example for the same so that i can get an idea as to how to proceed.?? Deepak

Marco Ehrentreich best scout Bartender Joined: Mar 07, 2007 Posts: 1225
I like...

posted 6/25/2009 1:52:09 AM

Hi Deepak, your problem seems to be really serious... I can imagine a relatively simple ad-hoc solution which _COULD_ work, too.

But in any case Steve and I should now if you have checked if it's possible to use the backported version of the java.util.concurrent package or upgrade to Java >= JDK which make working with concurrency much easier and safer, in particular because you're new to this subject! Maybe you can check this and let us know! Marco

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 6/25/2009 1:53:44 AM

Sure, the java.util.concurrent.CountDownLatch has example code.

6
I like...

Deepak Lal Ranch Hand

posted 6/25/2009 12:30:52 PM

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

Hi Steve,

4/8

7/14/13
Joined: Jul 01, 2008 Posts: 507
I like...

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)


Hi Steve, I hasd been to below site http://sourceforge.net/project/platformdownload.php?group_id=153802&sel_platform=4411 it says download backport-util-concurrent-*.zip - works with Java 1.4 and newer but im unable to find out the relevant backport package. Could you please help me with this.. Could you tell me the steps for using backport-util-concurrent with JDK 1.4

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 6/25/2009 4:25:49 PM

Sorry, I have never used the backport. Reading that site should give you the information you need though. If you can't figure it out you should check out the conta forum. If all else fails, download whatever the 'latest' link tells you to.

6
I like...

Marco Ehrentreich best scout Bartender Joined: Mar 07, 2007 Posts: 1225
I like...

posted 6/25/2009 4:41:48 PM

I have never used it, too. From a quick look at the README which is inside the ZIP you can download it seems to be sufficient to include the JAR file in your project documentation there are some limitations because it's a backport version but I think for your needs it should work ;-) Marco

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 6/26/2009 2:27:12 PM

Marco and steve. Im unable to find out the zip version,and how do i proceed once i get it ?? Please help me out. Deepak

Marco Ehrentreich best scout Bartender Joined: Mar 07, 2007 Posts: 1225
I like...

posted 6/26/2009 2:59:59 PM

I can't test it at the moment because I don't have enough time and no JDK 1.4 :-) But I would take this one: backport-util-concurrent-Java60-3.1.zip

Unzip it and add the included JAR in it to your project. Then the necessary classes (e.g. CountDownLatch) for Steve's example should hopefully be available to you Marco

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 6/26/2009 6:33:04 PM

Hi Deepak, that link you posted has a big green button at the center/top of the page with a download link. Did you click that link?

6
I like...

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 6/30/2009 5:52:09 PM

Hi Steve and Marco Ehrentreich, Yes,i have downloaded the backport-util-concurrent.jar and configured in my build path.Can you please tell me how to proceed on this ? How do i use the CountdownLatch and which classes i need to import in the below scenario ?? Please help....

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 .

p u b l i cc l a s sT e s t C o n c u r r e n c y{ p u b l i cs t a t i cv o i de x e c u t e ( ) {

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

5/8

7/14/13
0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 .

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)


u p d a t e T a b l e 1 ( ) ; / / s h o u l db ep r o c e s s e df o rj o b sr u n n i n gc o n c u r r e n t l y , / / O n c et h e yf i n i s hr u n n i n g , u p d a t eT a b l e 2 u p d a t e T a b l e 2 ( ) ; } p u b l i cs t a t i cv o i du p d a t e T a b l e 1 ( ) { } p u b l i cs t a t i cv o i du p d a t e T a b l e 2 ( ) { } p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ T e s t C o n c u r r e n c y . e x e c u t e ( ) ; } }

I have another clarification ,

1. what is the number of jobs that can be run concurrently and can be handled by the CountdownLatch as you have suggested ? concurrently. 3. Does the Backport with concurency have any limitations in terms of the above scenario ??

2. suppose i have 1000 jobs running concurrently ,will it degrade the performance of the running application /server will crash/ or any other possibility yo

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 7/1/2009 2:50:24 AM


Deepak Lal wrote:

How do i use the C ountdownLatch and which classes i need to import in the below scenario ?? Please help....

6
I like...

The JavaDoc is here: http://backport-jsr166.sourceforge.net/doc/api/. That should tell you what classes to import and how to use the class. Otherwise, any examp as showing how to use it.

1. what is the number of jobs that can be run concurrently and can be handled by the CountdownLatch as you have suggested ? concurrently. 3. Does the Backport with concurency have any limitations in terms of the above scenario ??

2. suppose i have 1000 jobs running concurrently ,will it degrade the performance of the running application /server will crash/ or any other possibility yo

Number of concurrent Jobs? Many. Theoretically, Integer.MAX_VALUE. Realistically, you will be limited by your specific application. Can you have 1000 jobs running c depends on the application. For example, if you have an application that spends a lot of time waiting on some external datasource (Input from external application

threads and get some benefit. If your application is heavy on processing, and takes a lot of processor time rather than waiting time, then you will gain little above too much more, in this case, may hurt performance. If your Job takes a lot of memory, then having 2 or more Jobs running simultaneously may cause memory conce Collection cycles so as to degrade performance. If your Job accesses files on disks then having even 2 threads concurrently reading from the disk may (almost cert

So how do you know, for your application? The first thing to do is to program the application with a single thread in the Thread Pool, and use a Profiler to test spee more Threads to the Thread Pool and see the affect. It quite often is the case that the actual number of Threads used should depend on the system on which the won't know until you test it out in the real world.

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 7/2/2009 10:06:22 PM

Hi Steve, I have written the below code in Java 1.4 using the backport version as suggested by you.

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 .

i m p o r te d u . e m o r y . m a t h c s . b a c k p o r t . j a v a . u t i l . c o n c u r r e n t . * ; / * *L a t c h i n gv a r i a b l e ss p e c i f yc o n d i t i o n st h a to n c es e tn e v e rc h a n g e . *T h i sp r o v i d e saw a yt os t a r ts e v e r a lt h r e a d sa n dh a v et h e mw a i t *u n t i las i g n a li sr e c e i v e df r o mac o o r d i n a t i n gt h r e a d . *T h ef o l l o w i n gp r o g r a mc r e a t e sas e to ft h r e a d s , *b u td o e s n ' tl e ta n yt h r e a ds t a r tu n t i la l lt h et h r e a d sa r ec r e a t e d . * * / p u b l i cc l a s sL a t c h T e s t { p r i v a t es t a t i cf i n a li n tC O U N T=1 0 ; p r i v a t es t a t i cc l a s sW o r k e ri m p l e m e n t sR u n n a b l e { C o u n t D o w n L a t c hs t a r t L a t c h ;

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

6/8

7/14/13

1 8 . 1 9 . 2 0 . 2 1 . 2 2 . 2 3 . 2 4 . 2 5 . 2 6 . 2 7 . 2 8 . 2 9 . 3 0 . 3 1 . 3 2 . 3 3 . 3 4 . 3 5 . 3 6 . 3 7 . 3 8 . 3 9 . 4 0 . 4 1 . 4 2 . 4 3 . 4 4 . 4 5 . 4 6 . 4 7 . 4 8 . 4 9 . 5 0 . 5 1 . 5 2 . 5 3 . 5 4 . 5 5 . 5 6 . 5 7 . 5 8 . 5 9 . 6 0 . 6 1 . 6 2 . 6 3 . 6 4 . 6 5 . 6 6 . 6 7 .

C o u n t D o w n L a t c hs t a r t L a t c h ; C o u n t D o w n L a t c hs t o p L a t c h ; S t r i n gn a m e ;

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)

W o r k e r ( C o u n t D o w n L a t c hs t a r t L a t c h ,C o u n t D o w n L a t c hs t o p L a t c h ,S t r i n gn a m e ) { t h i s . s t a r t L a t c h=s t a r t L a t c h ; t h i s . s t o p L a t c h=s t o p L a t c h ; t h i s . n a m e=n a m e ; } p u b l i cv o i dr u n ( ) { t r y { s t a r t L a t c h . a w a i t ( ) ;/ /w a i tu n t i lt h el a t c hh a sc o u n t e dd o w nt o z e r o }c a t c h( I n t e r r u p t e d E x c e p t i o ne x ) { e x . p r i n t S t a c k T r a c e ( ) ; } S y s t e m . o u t . p r i n t l n ( " R u n n i n g :"+n a m e ) ; s t o p L a t c h . c o u n t D o w n ( ) ; } } p u b l i cs t a t i cv o i dm a i n ( S t r i n ga r g s [ ] ) { / /C o u n t D o w n L a t c h ( i n tc o u n t ) / /C o n s t r u c t saC o u n t D o w n L a t c hi n i t i a l i z e dw i t ht h eg i v e nc o u n t . C o u n t D o w n L a t c hs t a r t S i g n a l=n e wC o u n t D o w n L a t c h ( 1 ) ; C o u n t D o w n L a t c hs t o p S i g n a l=n e wC o u n t D o w n L a t c h ( C O U N T ) ; f o r( i n ti=0 ;i<C O U N T ;i + + ) { n e wT h r e a d ( n e wW o r k e r ( s t a r t S i g n a l ,s t o p S i g n a l ,I n t e g e r . t o S t r i n g ( i ) ) ) . s t a r t ( ) ; } S y s t e m . o u t . p r i n t l n ( " G o " ) ; s t a r t S i g n a l . c o u n t D o w n ( ) ; t r y { s t o p S i g n a l . a w a i t ( ) ; }c a t c h( I n t e r r u p t e d E x c e p t i o ne x ) { e x . p r i n t S t a c k T r a c e ( ) ; } S y s t e m . o u t . p r i n t l n ( " D o n e " ) ; } }

Can you please suggest how should i refine the above code so that it works for my requirement as given below Requirement as stated in OP:

view plain

c opy to c lipboard

print

N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 .

u p d a t e T a b l e 1 ( ) ; / / s h o u l db ep r o c e s s e df o rj o b sr u n n i n gc o n c u r r e n t l y , / / O n c et h e yf i n i s hr u n n i n g , u p d a t eT a b l e 2 u p d a t e T a b l e 2 ( ) ; p u b l i cs t a t i cv o i du p d a t e T a b l e 1 ( ) { } p u b l i cs t a t i cv o i du p d a t e T a b l e 2 ( ) { }

Need help Steve..... Please could you refine the above code now....Im stuck up with this for a long time.... Deepak

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 7/3/2009 1:43:09 AM

Okay, Here is what you would do:

6
I like...

You want updateTable1() to run in several threads concurrently, which means you need: 1) Make a number of Threads, which will run concurrently (an ExecutorService suits this need nicely). 2) Each thread will run 1 task - usually a Runnable which does the work you want to do concurrently 3) Create a CountDownLatch with the same number of passes as there are tasks to run, and make sure all the tasks have access to it 4) Each task calls updateTable1() 5) When updateTable1() completes the task counts down once on the CountDownLatch You then want to call updateTable2() after all updateTable1() calls are done 1) Make a single task to run in a single thread (you can add it to the ExecutorService too if you make the ExecutorService big enough)

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

7/8

7/14/13

Problem is Parallel Processing of Jobs in Java (Threads forum at JavaRanch)


2) The task will await() on the CountDownLatch as its first step. So it does nothing till all the other tasks are done. 3) It then calls updateTable2().

Deepak Lal Ranch Hand Joined: Jul 01, 2008 Posts: 507
I like...

posted 7/3/2009 1:48:26 PM

Steve could you please refine the code which i have pasted... I'm unable to proceed...

Steve Luke Bartender Joined: Jan 28, 2003 Posts: 3159

posted 7/3/2009 6:39:50 PM


Deepak Lal wrote:

Steve could you please refine the code which i have pasted... I'm unable to proceed...

6 Hi Deepak, JavaRanch is Not a code mill, so I won't write the code for you. Try what I pointed out. If you can't do it, go over to the Java Concurrency Tutorial,
I like...

through that tutorial it should show you how to use the classes you worked so hard to get a backport for.

Then implement what you want in a very basic sketch, like the code you copy and pasted from the API above. Then fill in the real work. If you have more questions and ask them.

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Problem is Parallel Processing of Jobs in Java

Similar Threads JPA update not available to some successive queries Multiple Columns in a JCombobox Update in For loop. Its Very Urgent !!!! Help Me Now !!!!!! EDITABLE JTable and SQL QUERY
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:40:16 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/450812/threads/java/Parallel-Processing-Jobs-Java

8/8

Das könnte Ihnen auch gefallen