Answer the question
In order to leave comments, you need to log in
Is it always necessary to use volatile in multi-threading when working with shared data?
While I was studying multithreading, I noticed that I forgot volatile in this code:
public class Test {
static int j = 0;
public static void main(String[] args){
th Th = new th();
Thread thread = new Thread(Th);
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(j);
}
public static class th implements Runnable{
public void run(){
for (int i = 0; i < 1000; i++) {
j++;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question