CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Named locks (mutexes)

  1. #1
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Named locks (mutexes)

    Hi everyone,

    I'd like to run a design by you and ask on how to implement it. Here is the basic problem:
    I have a servelet A that returns full JPG images.
    I have a servelet B that returns JPG images that are only parts of the images.
    (e.g. the whole image is 1600x1200 and servelet B returns only the upper right corner 400x400 part of the image).
    The filesystem on the server stores a TIFF master file for the full image.
    When servelet A is called, it checks whether a copy of the JPG exists in a cache (EhCache), and if it does, this is returned. Otherwise, the TIFF is converted to JPG, put into the cache and delivered.
    When servelet B is called, it also checks whether a copy of the JPG exists in a cache and if it does, it cuts part of the image out and delivers it. Otherwise, the TIFF is converted to JPG, the whole JPG is saved in the cache, the part is cut out and delivered.

    The main concurrency problem here is that when A or B is in the process of creating the JPG from the TIFF, the other (or even the same) servelet should not be able to read from it. This is file-specific. I.e. if aaaaa.tiff is being converted, another instance can still convert bbbb.tiff at the same time.

    My naive idea is to wrap the conversion part in a mutex, where I say something like:
    getLock(filename);
    // do conversion
    releaseLock(filename);

    So if the lock has the same name as filename, my condition would work. Is there a type of locking in Java that can be made to work like this?

    I've looked at java.util.concurrent.locks (http://download.oracle.com/javase/1....e-summary.html), but haven't figured out a way to use this to do what I want.

    Any help is appreciated,

    Yves
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Named locks (mutexes)

    You could have a Map keyed on file names and with java.util.concurrent.Semaphore objects as the values. You create the Semaphore with 1 permit so only one thread at a time can obtain the lock for the given filename.

    Threads request the Semaphore for a filename and then block until they can acquire the permit from the Semaphore. If there is no key in the Map a new pairing will need to be created.

    You will also need to synchronize access to the Map to ensure 2 threads don't simultaneously try to create and add a new filename-Semaphore pairing. In fact it may best to create a proxy class for the Map to restrict threads to a single point of access which will be easier to synchronize.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: Named locks (mutexes)

    That makes sense, thanks. So I guess I'll have to implement it on my own then.

    Thanks,

    Yves
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)