简体   繁体   中英

Are all Thread methods (like getName/setName) thread-safe?

Is it safe to use Thread 's methods like setName / getName and some others from different threads? API does not say anything, but judging by the source code

private char name[];

public final void setName(String name) {
    checkAccess();
    this.name = name.toCharArray();
}

public final String getName() {
    return String.valueOf(name);
}

it seems that it may cause memory consistency errors.

Thread.getName() is a property than can be queried by anyone at any time. For example, a monitor utility constantly queries names of all threads. So the method has to be thread safe, otherwise, there's no clear protocol about who can safely access it and when.

Though it's always been puzzling why Thread uses a char[] to save its name, you are raising a more important question, getName() is obviously not correctly synchronized. If one thread does setName("abcd") , another thread may observe getName()->"ab\\0\\0" .

I'll post the question to concurrency-interest list. see http://cs.oswego.edu/pipermail/concurrency-interest/2013-March/010935.html

"API does not say anything"

If an API does not say anything, than you never can asume that a method /class is thread safe. As you can see from the source code, the acess to name is not mutual exclusive.
But for me it seems, that name has either the old value or the new one, nothing in between, so it looks safe for must uses of get/setName().

For starters, if one thread invokes setName while access to name isn't somehow synchronized, there's no guarantee that any other thread will ever see that new value.

Second, String.valueOf(char[]) iterates over name . That means if even one of name 's characters is set during this iteration, the iterating thread might see inconsistent data - the first characters of the original char array and the last characters of the other.

In this particular case, it's not one of characters, rather it's the pointer to the beginning of the char array. Assuming that an iteration over an array computes the next cell to access by adding the current iteration index to that pointer's referred address, it will indeed also cause inconsistent data.

** EDIT **

Regarding the second non-safe scenario, after reading this question and answer , it seems much less obvious as to how actually an array copy is implemented - it is platform-dependent. This explains why String,valueOf(char[]) isn't documented as thread-safe. So, anyway, the second scenario still applies as non-thread-safe.

If the thread object is shared amongst other threads (which might be unusual, but nevertheless possible - if you programmed a solution that way), then it is NOT thread-safe. It is susceptible to race conditions, just like any other class or object.

Example:

Thread t1 = new SomeThread(); t1.start();
(new MyThread(t1, new Runnable() { public void run() {t1.setName("HELLO");} })).start();
(new MyThread(t1, new Runnable() { public void run() {t1.setName("GOODBYE");} })).start();

Two threads accessing same thread object t1. Not safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM