I was wondering do we require external synchronization to use the methods in java.lang.Thread?
For example, can we call the method t1.isAlive() from any thread without external synchronization and expect that it return:
true if t1 has already been started, false otherwise.
Or is external synchronization required to call the methods in java.lang.Thread?
public static void main(String args[]) {
final java.lang.Thread t1 = new java.lang.Thread(new java.lang.Runnable() {
@Override
public void run() {
while(true){
//task
}
}
});
java.lang.Thread t2 = new java.lang.Thread(new java.lang.Runnable() {
@Override
public void run() {
while (true) {
System.out.println(t1.isAlive()); // do we need synchronization before calling isAlive() ?
}
}
});
t2.start();
t1.start();
try {
java.lang.Thread.sleep(1000000);
} catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
}
Yes it should already be thread safe. You can look at the source code of Thread.java here all the important methods such as start etc are synchronized.
The is_Alive is a native method implemented in a lower layer so will give an instantaneous answer as to whether the thread is started or not, it is not synchronized so it may give a false right after the start method is called. Although this is very very rare.
The start method does however check the threadStatus member variable before proceeding with its operation and this is a volatile int i.e. will be instantly updated across all accessing threads. So you can use the getState call to check whether a thread is started rather than the isAlive method to avoid the calling start twice. I have copyied the relevant parts of Thread.java below.