Would this work?  Just call getLock() in a while(! getLock()) {} loop
until you get it.

boolean getLock() {
 String lockIndex = "LockIndex" +
MemcacheService.increment("descriptiveLockName", 1, 0);
 MemcacheService.put("DescriptiveLockName",
    lockIndex,
    null,
    MemcacheService.SetPolicy.ADD_ONLY_IF_NOT_PRESENT);
  if(memcacheService.get("DescriptiveLockName") == lockIndex) {
    // Yay, got the lock.
    return lockIndex;
  }
  else {
    return false;
  }
}

void releaseLock() {
  MemcacheService.delete("DescriptiveLockName");
}



On Dec 14, 5:07 am, Andrei Cosmin Fifiiţă <[email protected]>
wrote:
> I was thinking about that (increment/decrement)...
> But, first, if the tasks read the value of the variable that needs to be
> incremented, isn't there a posibility that all tasks will read the same
> value at the same time and then increment it in the same time ?
>
> On 14 December 2010 11:59, Simon <[email protected]> wrote:
>
>
>
>
>
>
>
> > The synchronized block won't work at all, since it's not guaranteed
> > that you only have a single instance of your application running at
> > any point in time.  As soon as you have multiple instances then you
> > will be synchronizing in different JVMs and hence you'll get multiple
> > threads accessing the cache, and hence the same problem will occur.
>
> > You could probably use the Memcache increment/decrement functionality
> > to hook together a rough cross-instance synchronization, since they
> > increment and decrement atomically.
>
> > On Dec 14, 5:22 am, Michael Weinberg <[email protected]> wrote:
> > > You can use the standard Java synchronization to synchronize the task
> > > threads, e.g.
>
> > > synchronized (YourMemcachedDataClass.class) {
> > >   YourMemcachedDataClass cachedData =
> > > (YourMemcachedDataClass)cache.get(CACHE_KEY);
>
> > >   if (cachedData == null)
> > >      cachedData = new YourMemcachedDataClass ();
>
> > >   cachedData.add(...);
> > >   cachedData.set(...);
>
> > >   cache.put(CACHE_KEY, cachedData);
>
> > > }
>
> > > this way only 1 thread at a time will get into the synchronized block.
>
> > > Hope it helps..
>
> > > Michael Weinberg
>
> > > On Dec 13, 11:41 am, Ice13ill <[email protected]> wrote:
>
> > > > Maybe there's something that i don't know very well about the memcache
> > > > service and this problem is simpler then i thing it is... If so,
> > > > please advice :)
>
> > > > On Dec 13, 6:38 pm, Ice13ill <[email protected]> wrote:
>
> > > > > I need advice for implementing concurrent access to a memcache
> > > > > variable, when used by multiple running tasks.
> > > > > The problem is this: I have a number of tasks reading from datastore
> > > > > and doing some processing.
> > > > > When the processing is complete, i add a String to a List stored in
> > > > > memcache. So i need to get the List from memcache, add a new element,
> > > > > and put it back. The pb is that if 2 tasks write on that variable, i
> > > > > get an exception (I understand that the memcache service gives me a
> > > > > "clone" of the object, not the object itself, or am I wrong ?)
> > > > > So how can i avoid more than one tasks writing in the same object ?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google App Engine for Java" group.
> > To post to this group, send email to
> > [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<google-appengine-java%2B 
> > [email protected]>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-appengine-java?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to