For concurrent work, you never know when a thread is running, and you need some way to avoid two tasks accessing the same resources, that is, to avoid competing resources, at least not on critical code, or that multiple threads can operate on a memory region at the same time causing data corruption.
Critical sections in program code require mutually exclusive access, with only one thread accessing the critical section at the same time, which is mutually exclusive when the thread accesses the critical section.
Race condition: Data corruption occurs when multiple threads access a shared memory region at the same time and read and write to it. This is the competitive condition. The way to avoid competitive conditions is to synchronized lock.
The sample, which has a ready-made, task for the thread is to share the variable count value +1. Set the value of the count to the initial value of 0, open 1000 identical threads, then after the thread finishes executing, the value of count should be 1000 (because count is added to each thread), but the actual effect will be wrong because of the race condition.
Workaround: Use synchronized lock,
First define an object:
Private final Static Object lockobj = new Object ();
The following is the synchronization block, which is the key operation in the synchronization block.
Synchronized (lockobj) {syn.count = Syn.count+1;temp = temp+1;}
-------------------------------------------------------------------------------
Here's the chase.
The Synchronized keyword in Java can be used as a modifier of a function or as a statement within a function, that is, a synchronous method and a block of statements that are usually said. If subdivided, synchronized can be used for instance variables, object reference (objects reference, such as this), static functions and class literals (class name literal constants). The following is a discussion of the effects of synchronized on code in different places:
Suppose that P1, P2 is a different object of the same class, and this class defines synchronous blocks or synchronous methods for the following situations, P1, P2 can call them.
1. When synchronized is used as a function modifier, the sample code is as follows:
Public synchronized void Method () {
....
}
This is the synchronization method, then which object is synchronized locked? His lock is to call this synchronization method object. That is, when an object P1 the synchronization method in a different thread, they form mutual exclusion and achieve synchronization. However, another object generated by the class that this object belongs to P2 is able to invoke this method of adding the Synchronized keyword arbitrarily. The example code above is equivalent to the following code:
public void Method ()
{
Synchronized (this)//(1)
{
.....
}
}
(1) What is this referring to? He refers to the object that invokes the method, such as P1. It can be seen that the synchronization method is essentially the synchronized action on object Reference. The thread that got the P1 object lock is able to call P1 's synchronous method, and for P2, P1 this lock is irrelevant to him, and the program may get rid of the control of the synchronization mechanism in this situation, causing data confusion.
2. Synchronization block, the sample code is as follows:
public void Method (Someobject) {
Synchronized (SO)
{
.....
}
}
At this point, the lock is so the object, who gets the lock who will be able to run the code he controls. When there is a definite object as a lock, it is possible to write the program like this, but when there is no explicit object as a lock, just to allow a piece of code to synchronize, you can create a special instance variable (it has to be an object) to act as a lock:
Class Foo implements Runnable
{
Private byte[] lock = new Byte[0]; A special instance variable
public void Method ()
{
Synchronized (lock) {//...}
}
.....
}
Note: A 0-length byte array object is created to be more economical than any object. View compiled bytecode: Generate 0-length byte[] objects with only 3 opcode, and object lock = new Object () requires 7 lines of opcode.
3. The synchronized action is applied to the static function, and the sample code is as follows:
Class Foo
{
Public synchronized static void Method1 ()//synchronous static function
{
....
}
public void Method2 ()
{
Synchronized (Foo.class)//class literal (class name literal constant)
}
}
The Method2 () method in the code is the case of the class literal as a lock, and the effect of the synchronous static function is the same, the obtained lock is very special, the class that belongs to the object that is currently calling this method (class, Instead of being a specific object produced by this class.
Reprint Please specify: Struts Tutorial network»synchronized deep Understanding
How to define shared variables:
1. Define a final variable in the main class so that it can be executed in other threads as well.
2. Using static to define thread-common member variables, implement the Runnable interface in a class that defines a static variable that is common to all class objects that use this interface, that is, all class objects share a copy of the variable.
1. Synchronize in Java can be used to lock objects, blocks of code, and so on. The Synchronize keyword can appear outside the function, or it can appear in the function body
2, no volatile there is no Java multithreading
3. Thread.Join () can be set to allow the current thread to execute before executing other threads, which is useful in controlling the order in which threads are executed.
The following is a code that describes a possible resource race condition (race condition) problem in Java Multi-threading.
Use of the Synchronize lock volatile Thread.Join () method in Java