Talk about Java threading, synchronized,wait,notify problems

Source: Internet
Author: User

<★CNMM22 Dynamic Original:http://blog.csdn.net/cnmm22/article/details/44758269>


In my last blog


Now that I'm not clear, I'll talk about it.


First, synchronized has only one function, which is to ensure that the blocks of code are not interrupted.


That said, there are synchronized on the line, why to add a small tail in the back? Synchronized (object Lock)? What about this object lock?


To explain this problem can not rely on theory alone, and all the online on this issue will focus on the theory. I'm going to put your focus on the actual application, to stand up to the designer's point of view to consider this problem,


Suppose a program, without multiple threads, is naturally executed from top to bottom.


And all of the places that use synchronized are bound to have multiple threads, and the program is no longer as simple as a single thread.


This leads to a practical problem: synchronized reduces program execution efficiency because, in order to ensure that blocks of code are fully executed, other threads may have to wait.


The designer naturally want to think: in order to ensure that the synchronized code block complete execution, is everyone waiting, or just some related procedures?


The answer is already included in the question.


So, how do you tell which programs are relevant? Object lock.


As for this question: why must there be synchronized in the Wait,notify place?


Because it must be wired to have the same thread as these wait threads Cheng wake them up, this sentence may be difficult to understand, but you will find that: there is a wait program, a thread will often be created multiple instances, such as my last talk of code 1, they obviously can have the same object lock , or there must be multiple threads with the same object lock, like the code in my last lecture, 2.


Hey, you said strange, in fact, it is not surprising, very well understood, I can clearly tell you, there is no way to have a single-threaded program wait such code appears, because if so, thread one but wait, no longer wake him up, there is no possibility of such a procedure appears.


For example, suppose you want to write a pause game in a game, which must be used for wait, and you must wake it up with a thread that holds the same object lock as him. That thread is usually a button listener thread, you have to prove one thing to believe me, that is, the main thread of the game and the key listener thread is not the same thread, but it can be a set of related threads (this is very simple, as long as there is the same object lock can be).


You can try it, in Java, the key is a dedicated thread, usually a background thread like awt-eventqueue-0, and the code you write is running in another thread.


So, actually when your game is paused, not all of it is paused, which is tantamount to explaining a thread wait, there must be other threads waking up to wake up the wait thread at the right time.




Talk about Java threading, synchronized,wait,notify problems

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.