Search This Blog

synchronized static method in python

Java code


class AClass {
private static int _count = 0;

public static synchronized inc(){
_count ++;
}

public static synchronized dec(){
_count --;
}
}

Python code


import threading

class AClass(object):
__count = 0
__lock = threading.RLock()

@classmethod
def inc(cls):
with cls.__lock:
cls.__count += 1

@classmethod
def dec(cls):
with cls.__lock:
cls.__count -= 1

No comments:

Post a Comment