[Last Call] Learn how to a build a cloud-first strategyRegister Now


returning unique numbers concurrency in multi threaded application in JAVA

Posted on 2014-08-11
Medium Priority
Last Modified: 2014-09-05
I have written a web service. It returns a number based on todays date+count
eg 201408111 will be first number returned when it is hit on 11th August 2014 first time.
Second time it will return 201408112, third time 201408112....same on
the initial date remains todays date and concatenate with the number of times it is called.

How its currently implemented is each time a hit comes it fetches the last record in a column and returns it
and then inserts a new row with next column value. So next time the service automatically returns the last column and adds the next.

Problem with this approach is that there can be synchronization or race condition issues.
Like if two there are multiple requests from different machines. They may get the same number resulting in inconsistency.
Please suggest how to avoid this.

Question by:Rohit Bajaj

Author Comment

by:Rohit Bajaj
ID: 40254918
No this is not a database question. Its just an approach i used in Java. There can be multiple approaches which does not use database.

Author Comment

by:Rohit Bajaj
ID: 40255195
yes Java advice
LVL 17

Expert Comment

ID: 40255839
Not sure if you actually have a problem, or if I don't understand your problem, but since you know which day it is all day long, you just increment and dish out that Integer every time there's a hit.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 21

Accepted Solution

MogalManic earned 1000 total points
ID: 40256326
Since you are using the database for your web service 'state', I would use the database for concurrency locking.  You could do a select..for update that would "lock" the last row in the table.  If any other threads call your web service before you inserted the new row, the row lock would block at the "select...for update".  Then after the insert completes and the new row is the "last Row", release the lock created by the "select..for update".  

You also have to make sure that you wrap the code in a try/catch/finally so that if an exception is generated, you still release the lock on the database.
LVL 28

Assisted Solution

dpearson earned 500 total points
ID: 40257659
In the case where you aren't using a database and you're storing the state in memory, then you'd just wrap it with a synchronized call:

private int counter = 0 ;
private String lastDate = null ;

// synchronized makes sure only one thread can update at a time
public synchronized String updateCounter() {
     String date = getDate() ;
      if (!date.equals(lastDate))
           counter = 0 ;

      lastDate = date ;
      counter++ ;
      return date + counter ;

Assisted Solution

by:Jan Louwerens
Jan Louwerens earned 500 total points
ID: 40259713
If you want to do it in memory, you can use an AtomicInteger object.

If you want to keep it in the database, you can use a sequence within the database.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question