If the argument of the println function is constant, there is no thread concurrency problem, but if the argument is in the form of an expression, the JVM will be executed in a few steps when executing the PRINTLN function, resulting in concurrency problems.
As shown in the following example:
Package Xiaoye.java;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.atomic.atomiclong;public class Test{public static void Main (string[] args) {Executorservice pool = Executors.newfixedthreadpool (2); Runnable T1 = new Myrunnable ("Zhang San", 2000); Runnable t2 = new Myrunnable ("John Doe", 3600); Runnable t3 = new Myrunnable ("Harry", 2700); Runnable T4 = new Myrunnable ("Lao Zhang", 600); Runnable T5 = new Myrunnable ("Lao Niu", 1300); Runnable T6 = new Myrunnable ("Lao Zhu", 800);//execute each thread pool.execute (t1);p ool.execute (T2);p Ool.execute (T3);p ool.execute (T4) ;p Ool.execute (T5);p Ool.execute (T6);//close thread pool Pool.shutdown ();}} Class Myrunnable implements Runnable{private static atomiclong along = new Atomiclong (10000);//atomic weight, each thread is free to operate the private St ring name; Operating person private int data; Operand myrunnable (String name, int data) {this.name = Name;this.data = data;} public void Run () {Thread.yield (); SYSTEM.OUT.PRINTLN (name + "executed" + Data + ", Current balance:" + along.addandget (data));}}
After several executions, one of the results is as follows:
John Doe performed 3600, current balance: 15600
Harry performed 2700, current balance: 18300
Lao Zhang performed 600, current balance: 18900
Old Ox performed 1300, current balance: 20200
Lao Zhu performed 800, current balance: 21000
Zhang San performed 2000, current balance: 12000
For
SYSTEM.OUT.PRINTLN (name + "executed" + Data + ", Current balance:" + along.addandget (data));
The following actual code is obtained after decompile:
System.out.println (New StringBuilder (string.valueof (name)). Append ("executed"). Append (", Current balance:"). Append (Along.addandget (data)). ToString ());
For the System.out.println () method, its execution code is as follows:
public void println (String x) { synchronized (this) { print (x); NewLine (); } }
Therefore, the output process takes two steps, transforming the string and synchronizing the output.
The actual execution process is: Zhang San-John Doe-Harry-Lao Zhang-Lao Niu-Lao Zhu, but actually the output Zhang San is called the last output. This is because Zhang San was interrupted before or after the lock.
If we want to output the correct order of execution, we can add a display lock:
Package Xiaoye.java;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.atomic.atomiclong;import Java.util.concurrent.locks.lock;import Java.util.concurrent.locks.reentrantlock;public class Test {public static void main (string[] args) {Executorservice Pool = Executors.newfixedthreadpool (2); Lock lock = new Reentrantlock (false); Runnable T1 = new Myrunnable ("Zhang San", +, lock); Runnable t2 = new Myrunnable ("John Doe", 3600, lock); Runnable t3 = new Myrunnable ("Harry", 2700, lock); Runnable T4 = new Myrunnable ("Lao Zhang", +, lock); Runnable T5 = new Myrunnable ("Old Bull", 1300, lock); Runnable T6 = new Myrunnable ("Lao Zhu", "N", "lock");//execute each thread pool.execute (t1);p ool.execute (T2);p Ool.execute (T3); Pool.execute (T4);p Ool.execute (T5);p Ool.execute (T6);//close thread pool Pool.shutdown ();}} Class Myrunnable implements Runnable {private static atomiclong along = new Atomiclong (10000);//atomic weight, each thread is free to operate private L Ock lock;private String name; Operating person private int data; Operand myrunnable (string name, int data, lock lock) {this.name = Name;this.data = Data;this.lock = Lock;} public void Run () {lock.lock (); SYSTEM.OUT.PRINTLN (name + "performed" + Data + ", Current balance:" + along.addandget (data)); Lock.unlock ();}}
In this way, the output order of the program and the execution order of the operation are consistent regardless of the operation.
Multithreading concurrency Problem of System.out.println