Java Synchronous lock (synchronized)

Source: Internet
Author: User

Java Synchronous lock (synchronized)
Java synchronization locks (synchronized) in java, Synchronized is a lock, which can lock a method or lock a method. In fact, these two things are the same. Is a block a method without a name, and a method is a block with a name. This article uses blocks for testing. The so-called lock is an atomic operation. It takes the locked block as a whole, just like when you go to the toilet and pull it, you have to wipe your ass. Of course, you can also not wipe it, if you don't care about the problem. The semaphores Semaphore and Synchronized actually implement similar functions, but they are different in efficiency and use different methods. Synchronized is easier to use.

In java, the time slice allocated by the cpu to each thread is random and many threads in java share one resource, four people in the dormitory are arranged to send apples to 30 goddesses tonight (Christmas Eve), with a total of 30 apples. The four of us will send these 10 apples. These 10 apples are the resources we share.

1. Do not consider resource sharing:

Public class RunnableTest implements Runnable {// a total of 10 apple private int apple = 10; @ Overridepublic void run () {try {for (int I = 0; I <10; I ++) {// synchronized (this) // {if (apple> 0) {System. out. println (Thread. currentThread (). getName () + ": sent to" + apple -- + "apple") ;}} Thread. sleep (1000); //} catch (Exception e) {}} public static void main (String [] args) {// four people started to send Apple RunnableTest runnableTest1 = new RunnableTest (); // RunnableTest runnableTest2 = new RunnableTest (); // RunnableTest runnableTest3 = new RunnableTest (); (new Thread (runnableTest1, "Li Yi ")). start (); (new Thread (runnableTest1, "Shen 2 ")). start (); (new Thread (runnableTest1, "Wang San ")). start (); (new Thread (runnableTest1, "Game 4 ")). start ();}}

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Memory + + 2vM/memory/J0tTLzbXEs/memory + PGJyPgo8L3A + CjxwPiAgMi6/memory + ICAgIL2ryc/memory "http://www.2cto.com/uploadfile/Collfiles/20141225/20141225085457277.jpg" alt = "\">

After consideration, I sent the Big Apple to the goddess alone.

However, due to the different locations of sleep and Synchronized, there are many different results, which are worth studying:

Scenario 1:

Public void run () {try {for (int I = 0; I <10; I ++) {synchronized (this) {Thread. sleep (1000); if (apple> 0) {System. out. println (Thread. currentThread (). getName () + ":" + apple -- + "apple") ;}}} catch (Exception e ){}}
Result:

I am sending apples all by myself. Alas, it hurts. Think about it carefully. This sleep writing is actually the same as not writing. You are asleep, and other buddies are sleeping.

Case 2:

Public void run () {try {synchronized (this) {for (int I = 0; I <10; I ++) {Thread. sleep (1000); if (apple> 0) {System. out. println (Thread. currentThread (). getName () + ":" + apple -- + "apple") ;}}} catch (Exception e ){}}

This is similar to the case 1 and continues to hurt. Because the lock is to let me finish sending 10 apples by myself.

Case 3

Public void run () {try {for (int I = 0; I <10; I ++) {Thread. sleep (1000); if (apple> 0) {synchronized (this) {System. out. println (Thread. currentThread (). getName () + ":" + apple -- + "apple") ;}}} catch (Exception e ){}}

Result:

You see, there are no more apples. Two other color wolves keep sending apples. This is because when Li Yi sent the last apple, the two products thought there was an apple (which has already been determined to be an apple> 0), So he continued to send the product, as a result, I gave my apple to my sister.

Scenario 4

Public static void main (String [] args) {// four people start sending Apple RunnableTest runnableTest1 = new RunnableTest (); RunnableTest runnableTest2 = new RunnableTest (); runnableTest runnableTest3 = new RunnableTest (); (new Thread (runnableTest1, "Li Yi ")). start (); (new Thread (runnableTest1, "Shen 2 ")). start (); (new Thread (runnableTest1, "Wang San ")). start (); (new Thread (runnableTest1, "Game 4 ")). start ();}
Result:

Four people want to send 10 apples by themselves. This is really a wolf in the dormitory. This is the most obvious mistake, although I found the problem for a long time.

What is the lock? Locks resources under an object. Case 4 consists of three objects and cannot be locked.

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.