Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when somebody says "Result is not Thread-Safe"

I was writing an app specific wrapper over Java HBase APIs when I read this doc:

http://hbase.apache.org/apidocs/org/apache/hadoop/hbase/client/Result.html

It says This class is **NOT THREAD SAFE**.

What exactly does it mean by not thread safe. I'm basically a C++ programmer and if someone says the function strtok() is not thread safe, I'll not use it in a multithreaded env. Its something like strtok() uses a static variable and calls to this function by two different threads is not a good idea.

Is it the same when it comes to JAVA?

I have a function:

public String get(String key, String family) {
    Get get = new Get(key.getBytes());
    get.addFamily(family.getBytes());

    Result result = null;
    try {
        result = _table.get(get);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return "";
}

The function get might be called by multiple threads. Does it make Result unsafe to use somehow?

like image 298
Mayank Avatar asked Jan 17 '26 18:01

Mayank


1 Answers

What exactly does it mean by not thread safe.

It means that if the given class object is accessed via various Threads then calling its method(s) within those Threads may result in unpredictable results because of unwanted interaction between the various Threads. The basic reason for this unpredictable result is the sharing of same data of an object among various threads. You can look at here at Wikipedia Article to know more about Thread-safety.

After going through your code I am seeing that you are using a member variable _table in line result = _table.get(get); . So , most probably it is not Thread-safe.

like image 153
Vishal K Avatar answered Jan 20 '26 09:01

Vishal K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!