Java Sync Lock

Source: Internet
Author: User

Java Sync Lock

Thread synchronization is a breach of data that prevents multiple threads from accessing a data object.
For example: Two threads Threada, threadb all manipulate the same object Foo object, and modify the data on the Foo object.

Package Cn.thread;public class Foo {    private int x = +;    public int GetX () {        return x;    }    public int fix (int y) {        x = x = y;        return x;    }}
Package Cn.thread;public class Myrunnable implements Runnable {    private foo foo = new Foo ();    public static void Main (string[] args) {        myrunnable run = new myrunnable ();        Thread ta = new Thread (Run, "thread-a");        Thread TB = new Thread (Run, "Thread-b");        Ta.start ();        Tb.start ();    }    public void Run () {for        (int i = 0; i < 3; i++) {            this.fix ();            try {                thread.sleep (1);            } catch (Interruptedexception e) {                e.printstacktrace ();            }            System.out.println (Thread.CurrentThread (). GetName () + ": The X-value of the current Foo object =" + Foo.getx ());}    }    public int fix (int y) {        return foo.fix (y);    }}

Operation Result:

Thread-b: The x value of the current Foo object = 40thread-a: The x value of the current Foo object = 40thread-b: The x value of the current Foo object = -20thread-a: The x value of the current Foo object = -20thread-b: The x of the current Foo object Value = -80thread-a: The x value of the current Foo object =-80

The result shows that the output value is obviously unreasonable. The reason is that two threads do not control access to Foo objects and modify their data as a result.

If you want to keep the results reasonable, you only have to achieve one goal, which is to restrict the access to Foo, and only one thread at a time can access it. This will ensure that the data in the Foo object is reasonable.

There are two things to do in the specific Java code:
Identify the resource class Foo variable x for the competing access as private;
Synchronize the code that modifies the variable, using the Synchronized keyword synchronization method or code.

Package Cn.thread;public class Foo2 {    private int x = +;    public int GetX () {        return x;    }    Synchronization method public    synchronized int fix (int y) {        x = x = y;        SYSTEM.OUT.PRINTLN ("Thread" +thread.currentthread (). GetName () + "run end, decrease" + y                + "", Current value is: "+ x);        return x;    }    //Sync code block//public    int fix (int y) {///        synchronized (this) {//            x = x-y;//            System.out.println ( "Thread" +thread.currentthread (). GetName () + "run End, decrease" "+ y//                    +", Current value: "+ x";//        }//        //        return x;//    }}
Package Cn.thread;public class MyRunnable2  {public    static void Main (string[] args) {        MyRunnable2 run = new My Runnable2 ();        Foo2 foo2=new Foo2 ();                MyThread T1 = run.new MyThread ("Thread A", Foo2, ten);        MyThread t2 = run.new MyThread ("Thread B", Foo2,-2);        MyThread t3 = run.new MyThread ("Thread C", Foo2,-3);        MyThread T4 = run.new MyThread ("Thread D", Foo2, 5);                T1.start ();        T2.start ();        T3.start ();        T4.start ();    }        Class MyThread extends Thread {        private Foo2 foo2;        /** Current Value */        private int y = 0;                MyThread (String name, Foo2 foo2, int y) {            super (name);            This.foo2 = Foo2;            This.y = y;        }        public void Run () {            foo2.fix (y);}}    }
Thread A runs the end, reduces "10", the current value is: 90 thread thread C runs the end, reduces "3", the current value is: 93 thread thread B runs the end, reduces "2", the current value is: 95 thread D run end, reduce "5", the current value is: 90

second, sync and lock

1, principle of lock

Every object in Java has a built-in lock.

When a program runs to a non-static synchronized synchronization method, the lock associated with the current instance of the executing code class (this instance) is automatically obtained. Acquiring a lock on an object is also known as acquiring a lock, locking an object, locking on an object, or synchronizing on an object.

The object lock does not work when the program runs to the synchronized synchronization method or code block.

An object has only one lock. Therefore, if a thread obtains the lock, no other thread can get the lock until the first thread releases (or returns) the lock. This also means that no other thread can enter the synchronized method or block of code on the object until the lock is freed. A

Release lock is a lock thread that exits the synchronized synchronization method or block of code.

About locks and synchronizations, there are a few key points:
1), can only synchronize methods, and cannot synchronize variables and classes,
2), each object has only one lock; when it comes to synchronization, what should be clearly synchronized? In other words, on which object is it synchronized?
3), you do not have to synchronize all the methods in the class, and the class can have both synchronous and non-synchronous methods.
4), if two threads are to execute the synchronized method in a class, and two threads use the same instance to invoke the method, only one thread can execute the method at a time, and the other waits until the lock is freed. That is, if a thread obtains a lock on the object, no other thread can enter any one of the synchronization methods in the class (the object).
5), if the thread has synchronous and non-synchronous methods, the non-synchronous method can be freely accessed by multiple threads without being restricted by the lock.
6), when the thread sleeps, any locks it holds are not freed.
7), the thread can obtain multiple locks. For example, a synchronous method that invokes another object in a synchronous method of an object acquires a synchronization lock for two objects.
8), synchronization damage concurrency, should be as narrow as possible synchronization range. Synchronization can not only synchronize the entire method, but also synchronize some of the code blocks in the method.
9), when using a synchronous block of code, you should specify which object to synchronize on, that is, which object to get the lock. Example:
public int, fix (int y) {
      synchronized (this) {
            x = XY;
     }
      return x;
}

Of course, synchronous methods can also be rewritten as non-synchronous methods, but functionally identical, for example:
public synchronized int GetX () {
return x + +;
}
And
public int GetX () {
Synchronized (this) {
return x + +;
}
}
The effect is exactly the same.

Third, static method synchronization

To synchronize a static method, you need a lock for the entire class object, which is the Class (Xxx.class).
For example:
public static synchronized int setName (String name) {
Xxx.name = name;
}
Equivalent to
public static int SetName (String name) {
Synchronized (Xxx.class) {
Xxx.name = name;
}
}

Iv. What happens if a thread can't get a lock

If a thread attempts to enter the synchronization method and its lock is already occupied, the thread is blocked on the object. Essentially, a thread enters a pool of that object, where it must wait until its lock is freed and the thread becomes operational or running again.

When considering blocking, be sure to note which object is being used for locking:
1. Threads that call non-static synchronization methods on the same object will block each other. If it is a different object, each thread has its own lock on the object, and the threads do not interfere with each other.
2. Threads that call static synchronization methods in the same class will block each other, and they are all locked on identical class objects.
3. Static synchronization methods and non-static synchronization methods will never block each other, because static methods are locked on the class object, and non-static methods are locked on the object of the class.
4, for the synchronization of the code block, to see what the object has been used to lock (synchronized the contents of the parentheses behind). Threads that synchronize on the same object block each other, and threads that are locked on different objects will never block each other.

Five. When to synchronize

When multiple threads access mutually exclusive (exchangeable) data, they should be synchronized to protect the data, ensuring that two threads do not modify it at the same time.

For data that can be changed in non-static fields, it is usually accessed using a non-static method.
For data that can be changed in a static field, it is usually accessed using static methods.

If you need to use a static field in a non-static method, or if you call a non-static method in a static field, the problem becomes very complex. Has gone beyond the scope of the SJCP exam.

Vi. Thread Safety Classes

When a class is already well synchronized to protect its data, this class is called "Thread-safe."

Even thread-safe classes should be particularly cautious, because the threads of the operation are still not necessarily secure.

Vii. 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.

Java Sync Lock

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.