Readability issues in multi-threading
When accessing variables of the same object in multiple threads, the read operation is not guaranteed to get the modified value of the write operation immediately if the read and write operations were in two different thread.
Because in the absence of synchronization, the compiler, the processor, and the runtime may make some unexpected adjustments to the order in which the operations are executed. In the absence of sufficient synchronization of multi-threaded programs, to determine the execution sequence of memory operations, it is almost impossible to get the correct conclusion.
For example, the following code may output 0, and it will loop indefinitely.
public class Novisibility {
private static Boolean ready;
private static int number;
private static class Readerthread extends Thread {public
void run () {
while (!ready)
Thread.yield ();
SYSTEM.OUT.PRINTLN (number);
}
}
public static void Main (string[] args) {
new Readerthread (). Start ();
Number =;
Ready = true;
}
}
This time must be used to ensure that the synchronization mechanism, which is another role of the synchronization mechanism, locking in addition to ensure that the operation of mutual exclusion, but also the visibility of the role (let other threads read to the latest variable value).
Solution 1: Use synchronized
Therefore, in JavaBean, the setting and reading of variables should be added synchronized to ensure that the data is immediately visible to other threads
public class Synchronizedinteger {
private int value;
public synchronized int get () {
return value;
}
Public synchronized void set (int value) {
this.value = value;
}
}
An example:
public class TT extends Thread {
int b = +;
Public synchronized void M1 () {
b = n;
try {
thread.sleep;
} catch (Exception e) {
e.printstacktrace ();
}
System.out.println ("M1 b=" + b);
}
public synchronized void M2 () {
b = +;
try {
thread.sleep (2500);
} catch (Exception e) {
e.printstacktrace ();
}
System.out.println ("m2 b=" + b);
}
public void Run () {
try {
m1 ();
} catch (Exception e) {
e.printstacktrace ()}
}
public static void Main (String args[]) {
TT TT = new TT ();
Tt.start ();
TT.M2 ();
System.out.println (tt.b);
}
}
This time the output
M2 b=2000
2000
M1 b=1000
But if the synchronized in the M2 () method is removed, it becomes
M2 b=1000
1000
M1 b=1000
Because M1 and M2 are called in different threads, if M2 is not using synchronization, the modified data written in M2 is not immediately visible.
Also, using shared mutable 64-bit variables in multi-threading, such as long and double, is unsafe because the JVM decomposes them into 32-bit atomic operations.
Solution 2: Use volatile variables
Variable of type volatile : Other threads can immediately read the update to the variable without locking it.
Solution 3: Thread Closure
The variable is enclosed in one thread, eliminating multithreading security issues.
Or use final to modify the variables in the class to implement thread closure.
In addition, using the Threadlocal class is a more canonical way to maintain thread closure. Threadlocal class usage See another posting.