Why Wait () and notify () must use sychronized
If not, will error illegalmonitorstateexception. first, it is clear that the implementation basis for Wait () and notify () is based on the existence of objects. So why is it based on object existence?
1.1 Public classLISTADD1 {2 Private volatile StaticList List =NewArrayList ();3 4 Public voidAdd () {5List.add ("Hello");6 }7 8 Public intsize () {9 returnlist.size ();Ten } One A Public Static
It programmer development must-all kinds of resources download list, the most complete IT resources in history, personal collection summary.
From the operating system point of view, the OS maintains a ready queue (the Ready thread queues). And
The realization of the classic consumer and producer:Precautions:1: Use Wait () in the loop, because when the thread obtains the lock, it is possible that other conditions are not met:2: The common buffer pool is to be used with the lock mechanism:1
Warehouse Class Public classStore {Private intsize = 0;//Current Capacity Private Final intMAX = 10;//Maximum Capacity//adding goods to the warehouse Public synchronized voidAdd () { while(Size >= MAX)//each execution is checked for full
Java provides 3 very important ways to skillfully solve communication problems between threads. These 3 methods are: Wait (), notify (), and Notifyall (). They are all the final methods of the object class, so each class has them by default.Although
Packagethreadtest;Importjava.util.LinkedList;ImportJava.util.concurrent.TimeUnit;ImportJava.util.concurrent.atomic.AtomicInteger; Public classQueueTest01 {Privatelinkedlist list =NewLinkedlist(); PrivateAtomicinteger count =NewAtomicinteger (0);
package Threadt; public class ThreadMain { public static void main (String args[]) { /span>final Object obj = new Object (); // new
Thread (new thread1to2 (1, obj), "xiaoming" new Thread (new thread1to2 (2, obj), "Xiao
After the thread has entered the running state, it can be changed to "wait for blocking" based on conditional triggering:
The running thread executes the wait () method, which releases all the resources it occupies, and the JVM puts the thread into
1. synchronized the role ofsynchronizedSimilar to the critical section in Win32, the role of the critical section: for shared global variables, in the case of multiple threads concurrency, read-write to such a global variable will be read-write
From the operating system's point of view, the OS maintains a ready queue (the queued thread of readiness). And at some point the CPU only serves the thread that is in the queue header in the ready queue.However, the thread that is currently being
Ondition can implement multiple Condition,notify can only notify a randomSuch as:ABCthree threads to deal with one thing, the rule isAfinish processing the notificationB,Bfinish processing the notificationC,cNoticeA; If you useNotrfycan't practice
From: http://zhangjunhd.blog.51cto.com/113473/71387
Using wait () and notify ()/notifyAll () allows multiple tasks to collaborate with each other.
1. wait () and Policy ()/policyall ()When sleep () and yield () are called, the lock is not released,
First, prefaceFor concurrent programming, in addition to the thread, the Wati and notify objects for object objects should also have a deep understanding of their usage, although there are not many knowledge points.II. basic knowledge of thread
Wait and sleep differences:
The same point : Calling the Wait,sleep method can be a thread that goes into a blocking state, yielding the CPU's execution rights.
different points : 1.sleep You must specify a time, but the wait method can
Wait and policy must perform operations on the same object.
Code 1:
Package wait1_y1;
Public class threadinstance implements runnable {
Public void run (){
Double D = 0;
Synchronized (this ){
Try {
Thread. Sleep (5000 );
}
Let's take a look at the test code. For details, see the notes.
Package COM. jadyer. thread. wait;/*** Java multithreading: Wait () and Policy () * @ see ================================================= =============================================
An example of understanding wait () and Policy ()
The following is a post I saw on the csdn forum, which involves the understanding of synchronization, wait (), notify (), and other concepts, I tried to analyze the wait () and Policy () methods
(1) Wait:1. Wait is a method of the object class, and sleep is a method of the thread class.2. The thread that is currently accessing my object, wait.3. Current this thread, locked in the current object of this thread stopped.4. Wait only after
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.