I. Thread SAFETY
A thread safety problem is a common thing in a program that is visited by multiple threads, for example: Static variables of a class
Thread Mutex: Refers to the two threads cannot be executed at the same time, they will be mutually exclusive, must wait for a thread to finish, there is a talent to execute
Two. Sync Lock
Is there any way to solve the problem of thread safety? And that's the lock in the program.
Java has two methods of locking:
1. Lock synchronized (this) {...} in the code block
2. Lock public synchronized void xxx () on the method {...}
Demo Sample code:
public class Traditionalthreadsynchronized {public static void main (string[] args) {new traditionalthreadsynchronized () . Init ();} private void Init () {final Outputer outputer = new Outputer ();//thread 1new threads (new Runnable () {@Overridepublic void run () {while (true) {try {Thread.Sleep]; OUTPUTER.OUTPUT1 ("1111111111");} catch (Exception e) {e.printstacktrace ();}}}). Start ();//thread 2new thread (new Runnable () {@Overridepublic void run () {while (true) {try {thread.sleep]; o UTPUTER.OUTPUT1 ("2222222222");} catch (Exception e) {e.printstacktrace ();}}}}). Start ();} Class Outputer {public void output1 (String name) {//Synchronized code block synchronized (this) {for (int i = 0; i < name.length (); i++) {System.out.print (Name.charat (i));} System.out.println ();}} Synchronization method public synchronized void Output2 (String name) {for (int i = 0; i < name.length (); i++) {System.out.print (name.ch Arat (i));} System.out.println ();}}}
Thread 1 and thread 2 have to call the Output1 method and print the incoming name string. The CPU will switch back and forth between two threads,
It is possible for thread 1 to print to half of the time to switch to thread 2. This is obviously something we don't want to see, so lock in the content of the code,
This ensures that a thread calls the method to end before it runs the next thread.
This in the code above refers to the Outputer object, which is a lock, two threads using the same lock ability to achieve synchronization.
Locking on the method also enables thread synchronization, and using Synchronizedkeyword on the method also takes this as a lock.
in fact, the Output1 method and the Output2 method are also synchronous, since they are all using this as a lock.
Think of a problem: Suppose a method is defined as static: public static synchronized
So what does it use as a lock? The answer is: The byte-code object of the class Xxx.class
Three. Synchonized and threadlocal
Threadlocal and synchonized are used to solve multi-threaded concurrent access, but there are essential differences between them, synchonized is to use the lock mechanism, so that the variable or block of code can only be visited by a thread at a time, Instead, Threadlocal provides a copy of the variable for each thread, and each thread does not access the same object at the same time, thus isolating multiple threads from sharing the data, while synchronized is the opposite. It is used to gain data sharing when communicating between multiple threads.
Summary: synchonized is used for inter-thread data sharing. Threadlocal is used for inter-thread data isolation.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Traditional Synchronizer Lock (two)