I want to use InheritableThreadLocal to store some variables. So I wrote some code like this:
public class ThreadContext
{
private static ThreadLocal current = new InheritableThreadLocal();
public static HashMap getContext()
{
if (current.get() == null) {
createContext();
}
return (HashMap) current.get();
}
public static void createNewContext(){
createContext();
}
public static IClientContext getClientContext()
{
return (IClientContext) ThreadContext.getContext().get("CLIENT_CONTEXT");
}
public static void setClientContext(IClientContext ctx) {
ThreadContext.getContext().put("CLIENT_CONTEXT", ctx);
}
private static void createContext()
{
current.set(new HashMap());
}
}
But when other code call getClientContext, NullPointerException happens occasionally:
java.lang.NullPointerException
at com.xxx.util.ThreadContext.getClientContext(ThreadContext.java:19)
It looks like getContext returned a null value. But in getContext, it can not return null.
Because if get returns null, it will create a new one.
public static HashMap getContext()
{
if (current.get() == null) {
createContext();
}
return (HashMap) current.get();
}
Any one met this problem before? Or any ideas?
I am not sure if this fixes your problem but a clearer way to write this is
public class ThreadContext {
private static ThreadLocal<Map<String, IClientContext>> current = new InheritableThreadLocal<Map<String, IClientContext>>() {
protected Map<String, IClientContext> initialValue() {
return new LinkedHashMap<String, IClientContext>();
}
};
public static IClientContext getClientContext(){
return ThreadContext.getContext().get("CLIENT_CONTEXT");
}
This will mean you use the supported way of initialising the thread local value.
Related
I am currently creating a database util class but my mongodb driver is async, my question now is how can I sync him? My current try looks something like this:
public boolean isBanIDFree(String banid) {
boolean value = false;
Thread thread = Thread.currentThread();
MongoCollection<Document> collection = database.getCollection("Bans");
collection.find(new Document("ID", banid)).first(new SingleResultCallback<Document>() {
#Override
public void onResult(Document result, Throwable t) {
if(result == null) {
value = true;
}
thread.notify();
}
});
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
return value;
}
but I can't edit the veriable value in the onResult Callback, how can I bypass this. i want to return a boolean and want the calling thread to wait until I got the response from the database
Variables used in anonymous classes must be effectively final.
That means you cannot assign them to something else, but you can call a setter on them.
So, you can do something like:
import java.util.concurrent.CompletableFuture;
public class Main {
public static void main(String[] args) {
BooleanWrapper b = new BooleanWrapper();
CompletableFuture.runAsync(() -> b.setValue(true));
// ...
}
private static class BooleanWrapper {
private boolean value;
public boolean getValue() {
return value;
}
public void setValue(boolean value) {
this.value = value;
}
}
}
I outputted the same object at the same time but I got different results...
What could be the cause of DIFFERENT result?
The function in UserHelper.class:
public void login(String phone, String password) {
UserModel.logInInBackground(phone, password, new LogInCallback<UserModel>() {
#Override
public void done(UserModel userModel, AVException e) {
if (null != userModel) {
if (userModel.getPosition() == UserModel.USER_BUYER) {
refresh();
DebugLog.e("fuck" + mUserStatus + UserInstance.getInstance().getUserStatus());
for (UserListener listener : listeners)
listener.OnUserLogin();
} else if (userModel.getPosition() == UserModel.USER_SELLER)
logout();
} else for (UserListener listener : listeners)
listener.HandleError(e.getCode());
}
}, UserModel.class);
public USER_STATUS getUserStatus() {
return mUserStatus;
}
And the UserInstance.class.
public class UserInstance {
public static UserHelper mInstance;
public static UserHelper getInstance() {
if (null == mInstance) mInstance = new UserHelper();
DebugLog.e(mInstance.toString());
return mInstance;
}
}
First of all, if you meant the UserHelper class to be a singleton,
why do you access the USER_STATUS instance using UserInstance.getInstance().getUserStatus() instead of just getUserStatus() ?
Second of all, you probably get different instances of UserHelper if the singleton is accessed from different threads, because your implementation is not thread-safe.
A correct implementation would be using a double locking pattern:
public class UserInstance {
public static UserHelper mInstance;
private static final ReentrantLock lock = new ReentrantLock();
public static UserHelper getInstance() {
if (null == mInstance){
lock.lock();
try{
if (null == mInstance){
mInstance = new UserHelper();
}
}
finally{
lock.unlock();
}
}
DebugLog.e(mInstance.toString());
return mInstance;
}
}
Ultimately, I get the same instance..
Thanks to Shlomi Uziei. I forgot to use double locking pattern. And I should not make mInstance static...
first of all i am new to threads and shared variables. So please be kind with me ;-)
I'm having a class called Routing. This class recieves and handles messages. If a message is of type A the Routing-Object should pass it to the ASender Object which implements the Runnable Interface. If the message is of type B the Routing-Class should pass it to the BSender Object.
But the ASender and BSender Objects have common variables, that should be stored into the Routing-Object.
My idea now is to declare the variables as synchronized/volatile in the Routing-Object and the getter/setter also.
Is this the right way to synchronize the code? Or is something missing?
Edit: Added the basic code idea.
RoutingClass
public class Routing {
private synchronized Hashtable<Long, HashSet<String>> reverseLookup;
private ASender asender;
private BSender bsender;
public Routing() {
//Constructor work to be done here..
reverseLookup = new Hashtable<Long, HashSet<String>>();
}
public void notify(TopicEvent event) {
if (event.getMessage() instanceof AMessage) {
asender = new ASender(this, event.getMessage())
} else if (event.getMessage() instanceof BMessage) {
bsender = new BSender(this, event.getMessage())
}
}
public synchronized void setReverseLookup(long l, Hashset<String> set) {
reverseLookup.put(l, set);
}
public synchronized Hashtable<Long, Hashset<String>> getReverseLookup() {
return reverseLookup;
}
}
ASender Class
public class ASender implements Runnable {
private Routing routing;
private RoutingMessage routingMessage;
public ASender(Routing r, RoutingMessage rm) {
routing = r;
routingMessage = rm;
this.run();
}
public void run() {
handleMessage();
}
private void handleMessage() {
// do some stuff and extract data from the routing message object
routing.setReverseLookup(somethingToSet)
}
}
Some comments:
Hashtable is a thread-safe implementation, you do not need another "synchronized" keyword see this and this for more information
Avoid coupling, try to work with interfaces or pass the hashtable to your senders, see this for more information
Depending on the amount of senders, you might want to use a ConcurrentHashMap, it greatly improves the performance, see ConcurrentHashMap and Hashtable in Java and Java theory and practice: Concurrent collections classes
This would conclude something like...:
public interface IRoutingHandling {
void writeMessage(Long key, HashSet<String> value);
}
public class Routing implements IRoutingHandling {
private final Hashtable<Long, HashSet<String>> reverseLookup;
private ASender asender;
private BSender bsender;
public Routing() {
//Constructor work to be done here..
reverseLookup = new Hashtable<Long, HashSet<String>>();
}
public void notify(TopicEvent event) {
if (event.getMessage() instanceof AMessage) {
asender = new ASender(this, event.getMessage())
} else if (event.getMessage() instanceof BMessage) {
bsender = new BSender(this, event.getMessage())
}
}
#Override
public void writeMessage(Long key, HashSet<String> value) {
reverseLookup.put(key, value);
}
}
public class ASender implements Runnable {
private IRoutingHandling _routingHandling;
public ASender(IRoutingHandling r, RoutingMessage rm) {
_routingHandling = r;
routingMessage = rm;
this.run();
}
public void run() {
handleMessage();
}
private void handleMessage() {
// do some stuff and extract data from the routing message object
_routingHandling.writeMessage(somethingToSetAsKey, somethingToSetAsValue)
}
}
I am making a bukkit plugin, and I am using an API called MCStats, to create the graph, you add Plotters like so...
mobs.addPlotter(new Metrics.Plotter("Player") {
#Override
public int getValue() {
return 0;
}
});
But I want to get the values from a HashMap, and idealy something like this...
for(String mob: mobNames) {
mobs.addPlotter(new Metrics.Plotter(mob) {
#Override
public int getValue() {
return Stats.getValue(mob);
}
});
}
But obviously, it can't access the mob variable, if I set it to final, it still wont be able to change in the loop. How can I work around this problem?
You can, in fact, use final in an enhanced for loop:
for(final String mob: mobNames) {
mobs.addPlotter(new Metrics.Plotter(mob) {
#Override
public int getValue() {
return Stats.getValue(mob);
}
});
}
You can use the final keyword for mob and it still be changed in the loop.
Try to run this code below:
public class Test2 {
public static void main(String args[]) {
String[] data = new String[] {"1", "2"};
List<MyClass> test = new ArrayList<MyClass>();
for (final String word: data) {
test.add(new MyClass() {
#Override
public void testMethod() {
System.out.println(word);
}
});
}
for (MyClass myClass: test) {
myClass.testMethod();
}
}
static class MyClass {
public void testMethod() {
}
}
}
The output will be "1" and "2".
Hi all someone can explain why the last line at this code is legal:
public class HashCodeTest {
private String value = null;
HashCodeTest(String value) {
this.value = value;
}
public static void main(String[] args) {
Map<HashCodeTest, String> aMap = new HashMap<HashCodeTest, String>();
aMap.put(new HashCodeTest("test"), "test");
aMap.put(new HashCodeTest("test"), "test");
System.out.println(aMap.size());
}
#Override
public int hashCode() {
int result = 17;
return 31 * result + value.hashCode();
}
public boolean equals(HashCodeTest test) {
if (this == test) {
return true;
}
if (!(test instanceof HashCodeTest)) {
return false;
}
return test.value.equals(value);
}
}
At the last line there is access to private field of test class but this is illegal.
Thanks,
Maxim
Private fields are accessible by all instances of this class.
Because it is an instance of the same class you are using it in.
value is not a private variable of another class; it is a private variable of another instance of the same class. Therefore the access is completely legal.
Access modifiers define access for a type, not the instance of a type.