Package com.czbk.thread;
/**
* Created by Chengtao on 17/12/3.
Thread safety issues:
The root cause of thread safety occurs:
1. There are two or more than two thread objects that share the same resource.
2. Multi-threaded operations share a resource's code with multiple sentences.
Solutions for thread safety issues:
Method One: You can use the synchronization code block to solve.
Format:
Synchronized (lock object) {
Code that needs to be synchronized
}
Things to keep in mind when synchronizing code blocks:
1. The lock object can be an arbitrary object.
2. A thread sleep in a synchronized code block and does not release the lock object.
3. If there is no thread-safety problem, do not use the synchronous code block, because it will reduce efficiency.
4. The lock object must be a resource for multi-threaded sharing, otherwise it cannot be locked.
Mode two: Synchronization function: The synchronization function is to use synchronized to modify a function.
Things to note about synchronization functions:
1. If the lock object that is a non-static synchronization function is the This object,
The lock object for a static synchronization function is the bytecode file (class object) of the class to which the current function belongs.
2. The lock object of the sync function is fixed and cannot be specified by you.
Recommended use: Synchronize code blocks.
Reason:
1. The lock object of the synchronization code block can be specified by us at will, which is convenient to control. The lock object of the synchronization function is fixed and cannot be specified by us.
2. Synchronizing code blocks makes it easy to control the range of code that needs to be synchronized, and the synchronization function must be all the code for the entire function to be synchronized.
*/
public class Thread03_traditionalthread_synchronized {
/**
* @param args
*/
public static void Main (string[] args) {
New Thread03_traditionalthread_synchronized (). Init ();
}
private void init () {
Final Outputer outputer = new Outputer ();
New Thread (New Runnable () {
public void Run () {
while (true) {
try {
Thread.Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Outputer.output ("Zhangxiaoxiang");
}
}
}). Start ();
New Thread (New Runnable () {
public void Run () {
while (true) {
try {
Thread.Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
OUTPUTER.OUTPUT3 ("lihuoming");
}
}
}). Start ();
}
Static Class outputer{
/*
Direct printing, there will be synchronization problems,
When the first thread prints to half the time,
If a second thread gets to the CPU,
Then the string of the first thread can only be printed in half
*/
public void output (String name) {
int len = Name.length ();
for (int i=0;i<len;i++) {
System.out.print (Name.charat (i));
}
System.out.println ();
}
/*
Control by synchronizing code blocks, note that lock objects must be unique
*/
public void Output1 (String name) {
int len = Name.length ();
Synchronized (Outputer.class)
{
for (int i=0;i<len;i++) {
System.out.print (Name.charat (i));
}
System.out.println ();
}
}
/*
The lock object of a non-static synchronization function that is controlled by a synchronous method is the This object
*/
Public synchronized void Output2 (String name) {
int len = Name.length ();
for (int i=0;i<len;i++) {
System.out.print (Name.charat (i));
}
System.out.println ();
}
/*
The lock object of a static synchronization function is a byte-code file (class object) of the class to which the current function belongs, using the synchronous method to control it.
*/
public static synchronized void Output3 (String name) {
int len = Name.length ();
for (int i=0;i<len;i++) {
System.out.print (Name.charat (i));
}
System.out.println ();
}
}
}
Java multithreaded day03 thread synchronization