• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling method B only after finishing method A  RSS feed

 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

need your help, How can i write a simple multithreading code to make sure it print out "B" only after "A" finish?

 
Paweł Baczyński
Bartender
Posts: 1997
42
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need it to be multithreading?
Why use multiple threads for sequential tasks?

You could use CountDownLatch for that.
Or wait()/notify().
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys, mind to give me some simple example?
 
K. Tsang
Bartender
Posts: 3620
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Thread's join() too.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeansonne Pierre wrote:need your help, How can i write a simple multithreading code to make sure it print out "B" only after "A" finish?

Simple: By ensuring that your two threads behave in a predictable sequence.

Which then begs the question that has already been asked: Why are you multi-threading?

The whole point about multi-tasking is that you shouldn't care what order your Threads execute, because you have no control over it.

What you can do is ensure that one Thread starts after another by launching Thread2 from inside Thread1. You can also ensure that Thread1 waits for Thread2 to finish by using join() as K.Tsang suggested - and I suspect that if you're simply learning about how Threads work, that may be all you need.

However, in normal practise, the use of either technique often suggests that you're "over-threading" and need to re-think your solution.

HIH

Winston
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

thanks for your time, appreciate it. So sorry that I did not describe my problem correctly.
Basically i I'm having problem on getting old data from database before the new value was successfully inserted. to fix this, i put some delay in the middle, and i believe this a bad approach. So i need your advise. Im a newbie i Java.

 
Tim Halloran
Greenhorn
Posts: 5
Android C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeansonne Pierre,

I don't believe you have a concurrency issue, but perhaps you do inside the API for your database. You need to determine what the line



is doing in your program (debugger, Javadoc). If it returns immediately and then does the database update, yes, you have a concurrency problem. Look for a call that doesn't return until it finishes or some sort of "wait until the update is done" to call. I don't recognize the API so this is probably a database interface unique to your company or program. Good luck!
 
Arun Chidambaram
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeansonne Pierre wrote:Hi Guys,

thanks for your time, appreciate it. So sorry that I did not describe my problem correctly.
Basically i I'm having problem on getting old data from database before the new value was successfully inserted. to fix this, i put some delay in the middle, and i believe this a bad approach. So i need your advise. Im a newbie i Java.



Highlighted text and code contradicts with each other. Are you talking about data corruption in multiple updates? If your using JPA interface it provides OPTLOCK mechanism to avoid saving corrupted record.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!