Java Sync Lock (synchronized)

Source: Internet
Author: User
Tags semaphore

Java Sync Lock (synchronized)in Java, synchronized is a lock, he can lock a method, can also lock a method, I wipe, in fact, these two things are the same. Block is not a method without a name, the method is a name of the block. This article uses blocks to test. The so-called lock, is the atomic operation, the locking block as a whole, just like you go to the toilet, pull will wipe the fart, of course, you can not wipe, if you do not care about the problems that arise. Semaphore semaphore and this synchronized actually achieve the same function, but the efficiency is different, the use of different ways. Synchronized use a little simpler.

Java in the CPU to each thread of the time slice is random and in Java many are many threads share a resource, for example, as the cock of our, bedroom four people are arranged for tonight (Christmas Eve) to 30 goddess hair Apple, a total of 30 apples, we four will send these 10 apples. These 10 apples are the resources we share.

1. Do not consider resource sharing:

public class Runnabletest implements runnable{//altogether has 10 apple private int apple = ten; @Overridepublic void Run () {try{for (int i = 0; I < 10; i++) {//synchronized (this)//{if (Apple > 0) {System.out.println (Thread.CurrentThread (). GetName () + ": Send First" + apple--+ " An apple ");}} Thread.Sleep (+);//}}catch (Exception e) {}}public static void main (string[] args) {//Four people start sending apples runnabletest RunnableTest1 = new Runnabletest ();//Runnabletest RunnableTest2 = new Runnabletest ();//Runnabletest RUNNABLETEST3 = new Runnabletest ();(new Thread (RunnableTest1, "Lee")). Start ();(new Thread (runnableTest1, "Shen er")). Start ();(new Thread ( RunnableTest1, "Wang San")). Start ();(new Thread (RunnableTest1, "Tour Four")). Start ();}}

See no, the 6th Apple should be very big, these guys all want to rob to send goddess. But this is not practical, there may only be one can be sent out of the number 6th apples. So, without considering synchronization, this is wrong


2. Consider synchronizing

Cancel the comment above:

After considering the synchronization, the Big Apple was sent to the goddess by me alone.

However, this problem due to the location of sleep and synchronized, there are a lot of different results, in fact, it is worth studying:

Scenario 1:

public void Run () {try{for (int i = 0; i <, i++) {synchronized (this) {thread.sleep (+); if (Apple > 0) {System.out. println (Thread.CurrentThread (). GetName () + ": Send First" + apple--+ "Apple");}}} catch (Exception e) {}}
Results:

I am the only one who sends apples, alas, the egg hurts ah. Think carefully, this sleep write actually and not write is the same, you sleep, other buddies are also sleeping.

Scenario 2:

public void Run () {try{synchronized (The) {for (int i = 0; i <; i++) {thread.sleep (+); if (Apple > 0) {System.out. println (Thread.CurrentThread (). GetName () + ": Send First" + apple--+ "Apple");}}} catch (Exception e) {}}

It's about the same as situation 1 and it continues to hurt. Because the lock is to let me get 10 apples out of a person.

Scenario 3

public void Run () {try{for (int i = 0; i <; i++) {thread.sleep), if (Apple > 0) {synchronized (this) {System.out. println (Thread.CurrentThread (). GetName () + ": Send First" + apple--+ "Apple");}}} catch (Exception e) {}}

Results:

You see, there are no apples. and two other sex-men have been sending apples. This is because when Lee finished the last Apple, the two goods thought there was an apple (already judged apple>0), so continue to send, the results of their own apple to the sister.

Scenario 4

public static void Main (string[] args) {//Four persons start to send Apple runnabletest runnableTest1 = new Runnabletest (); Runnabletest runnableTest2 = new Runnabletest (); Runnabletest runnableTest3 = new Runnabletest ();(new Thread (RunnableTest1, "Lee")). Start ();(new Thread (RunnableTest1, " Shen er "). Start ();(new Thread (RunnableTest1," Wang San ")). Start ();(new Thread (RunnableTest1," Tour Four ")). Start ();
Results:

Four people want to send 10 apples a person, this is really a bedroom wolf. This is the most obvious mistake, although I found a long day to find the problem.

The reason why there is lock, to understand what is the lock? The lock is a resource under an object, Case 4 is made up of three objects and cannot be locked.

Java Sync Lock (synchronized)

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.