I am using an annotation on some classes and by reflection I get some string of that annotation which is unique for every class.
I am thinking of using that string to synchronize a block of code, but I want to block access of the code only if annotation string value is same, if string is not same than another thread can enter a perform its task. I know that I can't use string directly and ints don't have monitor so I can't use hashcode directly. Is this way going to create any issue when I use jMeter to load test this piece of code.
public class LockObject {
private String lockString;
public LockObject(String lockString) {
this.lockString = lockString;
}
public int hashCode() {
returns hashcode for string;
}
@Override
public boolean equals(Object obj) {
}
}
public class TestClass {
public void someMethod() {
String annotationString = "fetchedByAnnotation that i use on some class by reflection";
//what i want if string is same then second thread won't enter this block
synchronized (new LockObject(annotationString)) {
//some task based on that string
}
}
}