GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Threads Synchronization Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads Synchronization Issue" Watch "Threads Synchronization Issue" New topic
Author

Threads Synchronization Issue

Suren Singh Khatana
Greenhorn

Joined: Mar 05, 2009
Posts: 9
Guys ,

I am new to java and stuck in a problem here.

I have written a java code to generate unique User ID.

I am maintaining a .properties file which stores the numerical suffix of the user ID and the UserIDGenerator class reads the file and do some calculation on the value read from file and Generates a unique ID and writes back the incremented value to the file again .


“LastUsedNumber” is the key in file which stores the suffix of next ID to be generated. The User ID generator class reads this look up for the suffix value and does some processing on this value in accordance with the User ID Generation algorithm and generates a new unique User ID.


After the User ID is generated, it writes back the incremented value to “LastUsedNumber” in file for next User ID generation.


This set up fails when we process large number users as multiple threads run concurrently and the read, write operation on LastUsedNumber is not synchronized.

So one thread read the data from above file and generate a unique ID, in the same time another thread also reads the same data from the file (before the first thread writes the incremented value)

And generates a ID which is same as the previous one and duplicate User ID are getting generated.


So it’s a classic thread synchronization issue which we are facing here

Any help to resolve this issue .

Sample Code

Public Class UniqueIDGenerator {


Public synchronized generateUserID(arg1,arg2,….){



// Read the file and the key ““LastUsedNumber” for the suffix



// generate new user ID using some logic



updateFileWithNewData (new incremented value)

}







Public updateFileWithNewData (value){



//Update the "LastUsedNumber" with the incremented value



}

}

Thanks
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

This is a very basic synchronization example. Why don't you create a class which creates the numbers. Just read the properties file in the constructor and add a shutdown-hook to write to the file. Then just synchronize/lock the get method.

// Edit: and please edit your post to remove the enters and please UseCodeTags


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Suren Singh Khatana
Greenhorn

Joined: Mar 05, 2009
Posts: 9
Thank you for reply .
Can you guide me how to add a shutdown hook ?
OR you can point me to some reference

Thanks for help
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700


No problem and welcome to the JavaRanch
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
In a production system, waiting until JVM shutdown to update a data file
is much too long to wait. It could be days, for example, before this would
happen. Reading the file for every request is too extreme also, even if it
is properly synchronized. I'm sure someone can suggest something in
between, a timer perhaps.

Jim ... ...


BEE MBA PMP SCJP-6
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

My suggestion would be to read/write to the file each request unless it proves itself to be to slow, then adjust as needed. So far that hasn't been shown, and really doesn't address the issue.

The original code is this:


This seems safe, but:
1) I would make updateFileWithNewData (and any other methods used in the generateUserID method) private, so that the only way to get to them would come through the synchronized block of the generateUserID.
2) This only works if all Objects that access the code use the same instance of UniqueIDGenerator. You may want to consider a Singleton pattern for this, or use a static final Object to synchronize on, rather than the instance.


Steve
Suren Singh Khatana
Greenhorn

Joined: Mar 05, 2009
Posts: 9
Thanks Steve ,

The application which invokes my UserIDGenerator code created a new instance of this class always when its invoked .
Thats why i am not able to make it thread safe ..

When you said static final object , does that mean making generateUserCII method as static ..

Thanks
Suren
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Making the methods in UniqueIDGenerator static could work, not sure, it depends on the rest of the class. Rather, I was talking about using a static lock object to synchronize on, instead of synchronizing the method (which synchronizes on the particular instance). Something like this:



That would ensure all the instances are synchronized on the same Object.
 
GeeCON Prague 2014
 
subject: Threads Synchronization Issue