Java-Preliminary Understanding-13th Chapter-deadlock Example

Source: Internet
Author: User

One.

Next is also a synchronization of a small knowledge point, when we use synchronization, there will be a phenomenon, deadlock.

Under what circumstances is the lock dead? One of the most common forms of expression in our program is the nesting of synchronizations. The image of the saying is: Two people each have a chopsticks, all want to eat, the result is disputed, who can not eat. Harmonious situation is, chopsticks to each other, each other after eating, and then even their own chopsticks back together, we eat again.

In the case of harmony, there are also stalemate. This stalemate is the deadlock, now through a program to verify.

One of the manifestations of deadlocks is the synchronous nesting.

Now the synchronization function contains the synchronization code block, the synchronization code block contains the synchronization function, the synchronization function of the lock is this, the synchronization code block of the lock is obj.

( my question here is two synchronized blocks of code are the same ?) The sync function is definitely the same one)

The process is like this, a thread comes in, first executes the synchronization code block, first takes the obj lock, takes out the lock to come in. One comes in and one more synchronization, this synchronization is the synchronization function, the lock is this. This is two locks, and obj has a this. Then look down, a thread in the Reading synchronization function, first take this,this inside who, obj bar. This is where two locks are nested.

And then execute it,

The DOS results show that the card is directly there. Compile and run again, there are all alternating output, that is harmonious.

There's even a direct hang-out.

The main thread sleeps 10 milliseconds, the 0 thread gets the CPU execution right, when the main thread wakes up, continues executes the t.flag=false; T2.start ();

When the 0 thread gets execution, rushed in to get the synchronization code block obj lock, followed by the show method to take this lock, and then read the show function, and then into the synchronization code block, read inside the content, if the statement read three sentences, 100~98. Immediately following the T2 this thread is also ready to be read, (it is entered as false) and is read else. This 0 thread is likely to be out of object, not yet waiting for this show, T2 thread will get the this lock with show. As soon as it gets this lock, it goes into the show function, and the 0 thread is holding this, (that is, the 0 thread is holding the declaration of the synchronous code block, and holding obj wants to call the This of the show function, and the 1 thread is in the show function declaration that, holding this, want to get into the synchronous code block for obj) Don't put each other, and all holding their own locks, who do not put, who are not in, this is the deadlock. → I think the fight and the executive right in who has the need to contact? I mean, can you hold a lock without CPU execution ?

If a thread gets a lock and goes out again, it's all released. The second thread comes in, which is more harmonious.

This is to tell you, try not to set a deadlock, so that the problem is not resolved.

Two.

Now to introduce a deadlock program, interview multi-threaded when you will encounter, please write me a deadlock program.

Write a simple deadlock program. (Follow the principle on a nested)

Is the mark the meaning of the lock?

In this case, the two nested are synchronous blocks of code ( why not use synchronous functions ?). ) and prepare two locks for this two synchronous code block.

In the Mylock class, what does the object Locka the previous modifier final mean?

It feels like the definition in the Mylock class above is very strange, is it a function or a variable ? Because it's a lock, it's definitely a function, but why do you write it?

Suddenly think of lock, or sync, feel bad logic, video in the time of interpretation, many times is the environment of their own construction features. is there a more precise description of synchronization ?

When I used to say that a thread task is an object (multiple threads operate the same task), why is the new two thread task here?

Two thread execution path, only two tasks, but this does not affect me to demonstrate, why? We have two threads running the Run method, and this flag, although both of my objects have their own flag, but their values are fixed, one is true and the other is false.

Only this situation occurs when multiple tasks do not matter, because you perform either True or FALSE, only in both cases.

The resource encapsulated in my thread task is a Boolean variable, although this variable has a separate copy of the two task objects, but the obtained value can only be two, either true or false. If this is not flag, but Num,int num=100, then it will be discarded, which results in two thread tasks performing two 100.

We can also not call the constructor, it is necessary to give the flag a value, first satisfy a value, and then switch.

Is this program that is now envisaged to be locked?

from the DOS results, it is locked, a thread to get the Locka lock, Output "if Locka ... "But I didn't get the lockb lock, so I couldn't enter the next step. The other thread gets the LOCKB lock and outputs "if lockb ...", but there is no locka lock. The end result is a different output statement.

You can also export the name of a thread,

The DOS result also shows the deadlock. But the same program for the CPU to execute, perhaps a harmony. So for the sake of greater probability we need to show the deadlock thread, we set the loop statement, the number of executions increases, the probability of a natural deadlock becomes larger.

The while (true), which is written earlier, may be looping all the time, forcing a stop when printing in the background.

The DOS results show that there is a lock-up and harmony, and the while (true) we just saidmight mean multiple loops.

Java-Preliminary Understanding-13th Chapter-deadlock Example

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.