What needs to be modified to synchronize the variables, use the Synchronized keyword to decorate
Package com.ehealth.thread;class foo{private int x=100;public int getx () {return x;} Public synchronized int fix (int y) {x=x-y; System.out.println ("THREAD :" +thread.currentthread (). GetName () + " thread end ,it  TAKE AWAY&NBSP: "+y +" now ,the value is "+x"; return x;}} class mythread extends thread{private foo f;private int y=0; MyThread (string name,foo f,int y) {super (name); this.f=f;this.y=y;} Public void run () {f.fix (y);}} Public class locktest {public static void main (String[] args) {// todo auto-generated method stubfoo f=new foo (); Mythread m1=new mythread ("Thread-a", f,10); Mythread m2=new mythread ("Thread-b", f,2); Mythread m3=new mythread ("Thread-c", f,-3); Mythread m4=new mythread ("thread-d", f,5); M1.start (); M2.start (); M3.start (); M4.start ();}}
The above code understands:
First, we create a class of objects to be modified by multiple threads
Then we're going to create a thread class. In this class, we first want a reference to a private instance object. Then, when the thread is instantiated, the modified object that is actually created is passed in. This time All operations are for this object. Because the keyword is used. So the data is synchronized
the principle of lock :
Each object in Java has a built-in lock.
There are several important points to follow:
1) Synchronization method only, cannot synchronize class or variable
2) should be clear on which object to synchronize
3) If there are two threads to execute the synchronized method of the same object. Then only one thread can execute this method, and the other thread waits.
Thread Synchronization Summary
1, the purpose of thread synchronization is to protect multiple threads to access a resource when the resource is destroyed.
2, thread synchronization method is implemented by the lock, each object has a cut only a lock , the lock with a specific object, the thread once the object lock, the other access to the object's thread can no longer access the object's other synchronization methods.
3, for the static synchronization method, the lock is for this class, the lock object is the class object. Static and non-static methods of locking do not interfere. A thread acquires the lock, which is obtained when a synchronization method on another object is accessed in a synchronous method.
4, for synchronization, to be awake at all times on which object synchronization, this is the key.
5, write thread-safe classes, you need to pay attention to multiple threads competing access to the logic and security of the resources to make the right judgment, the "atomic" operation to make an analysis, and ensure that other threads during atomic operation can not access the competing resources.
6. When multiple threads wait for an object lock, the thread that does not acquire the lock will block.
7, deadlock is between the threads waiting for lock-lock caused by, in practice, the probability of occurring is very small. Really let you write a deadlock program, not necessarily good, hehe. However, once the program has a deadlock, the program will die.
This article from "ehealth" blog, declined reprint!
Synchronization and locking of threads in Java