The thread is the smallest carrier that the computation program runs, because the hardware level of single single-core CPU develops to a certain bottleneck period, so there is multi-core multi-CPU, which directly leads to the complexity of multithreaded programming of programmers. This shows the importance of threading for high-performance development.
So the thread has several states in the computer, how do they switch between them? What's the difference between sleep and wait? How do
Wait-notify sceneTypical wait-notify scenarios are generally related to the following two elements:1. Status variable (state Variable)when a thread needs to wait, it is always due to some condition that is not satisfied. For example, to populate the queue with data, and when the queue element is full, the thread needs wait to stop running. When the queue element is vacant, continue with its execution.2. Con
suspend () and resume () methods: Two methods are used, suspend () causes the thread to enter a blocking state, and does not automatically recover, it must be called by its corresponding resume () to enable the thread to re-enter the executable state. Typically, suspend () and resume () are used when waiting for the result of another thread: After the test finds that the result has not yet been generated, the thread is blocked and the other thread produces the result, calling resume () to restor
1. The wait method and the Notify methodBoth methods, including the Notifyall method, are methods in the object class. In the Java api, the wait method is defined as Follows:
Public final void Wait () throws Interruptedexception
causes the current thread to wait until another thread invokes the notify() method or the method for this notifyAll() object. I N other words, This method behav
Java.lang.reflect.Method.invokeNative (Native Method)02-14 08:14:55.771:e/androidruntime (25572): at Java.lang.reflect.Method.invoke (method.java:511)02-14 08:14:55.771:e/androidruntime (25572): at Android.view.view$1.onclick (view.java:3593)02-14 08:14:55.771:e/androidruntime (25572): ... One more02-14 08:14:55.771:e/androidruntime (25572): caused By:java.lang.IllegalMonitorStateException:object not locked by Thread before notify ()02-14 08:14:55.77
Thread Communication concept: threads are independent individuals in the operating system, but these individuals cannot become a whole without special processing, and the communication between threads becomes one of the necessary ways for the whole. When a thread has a communication command, the interaction between the system is more powerful, while increasing CPU utilization will enable the developer to effectively grasp and supervise the thread task during processing. Use the Wait/
We often use the wait (), notify (), and Notifyall () methods to communicate between threads. When a thread checks a condition, it enters a wait state, for example, in the producer-consumer model, the producer thread discovers that the buffer is full and waits, and the consumer thread makes the buffer idle and notifies the producer thread by consuming a product. The Call of Notify () or Notifyall () notifie
Sleep, interrupt, wait, and notify
The sleep () method is used to stop a thread for a period of time. After the sleep interval expires, the thread may not resume execution immediately. This is because at that time, other threads may be running and not scheduled to give up execution, unless (a) "wake up" Threads have a higher priority. (B) The Running thread is blocked for other reasons.When wait () is a thread interaction, if the thread sends a wait (
Often on the go, about notify and Notifyall in Java, people often have the following statements:
Notify only notifies a waiting object, and Notifyall notifies all waiting objects, and all objects continue to run
And it seems that there are examples to prove it. The above statement can be said to be true or not. The reason is that one of the key points is that the official statement is as follows:
Wait,
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 l
Wait () notify () Notifyall ( ) These 3 methods are used to coordinate the access of multiple threads to shared data, so they must be used in the synchronized statement block. Wait () must be inside the synchronized function or code block, and wait () causes the thread that has already obtained the synchronized function or code block control to temporarily rest and lose control, Because the phenomenon loses control and enters the wait, other threads
Too lazy to write, look directly at the codeMobile Phone class/*** Mobile Phone type * Maximum number of phones produced *@authorHWUA **/ Public classshouji{ Public intMax//define container Maximum capacity Public intCurrentnum;//defining the container's current capacity PublicShouji (intmax) { This. Max =Max; Currentnum= 0; }}Production class//production of mobile phone class Public classSjphone1Implementsrunnable{ PublicShouji con; PublicSjphone1 (Shouji con) {//construct
IMP does not have an import view installed, please notify the DBA
J:/back/2>imp system/manager@cw3 fromuser=xjy Touser=xjy file=mydata2005.dmp
Import:release 9.0.1.1.1-production on Saturday May 12 09:31:30 2007
(c) Copyright 2001 Oracle Corporation. All rights reserved.
Connect to: Oracle8i Enterprise Edition Release 8.1.7.0.0-productionWith the partitioning optionJserver Release 8.1.7.0.0-production
IMP-00003: ORACLE Error 942 encounteredORA-009
The communication between multiple threads of the JVM is made up of thread locks, conditional statements, and wait (), notify ()/notifyall.The following is a two different statement that implements an output that enables multiple threads to loop.Package com.app.thread;Import Javax.swing.plaf.SliderUI;/*** To see the problem* @author Gordon**/public class Lockdemo {public static void Main (string[] args) {SYSTEM.OUT.PRINTLN ("lock");Final outturn ot =
thread with a lower priority to be executed, and of course allows a thread with the same priority and high priority to execute, and yield () can only cause the thread with the same priority to have an opportunity to execute.When wait () is called, the thread releases the "lock flag" that it occupies, making other synchronized data in the object that the thread resides in can be used by other threads.Waite () and notify () because they operate on the
Wait () and Policy () are directly affiliated to the Object class, that is, all objects have this pair of methods. It seems incredible at the beginning, but it is actually quite natural, because this pair of methods will release the occupied lock when blocking, and the lock is owned by any object, the wait () method of any object is called to cause thread blocking and the lock on the object is released. However, calling the notify () method of any obj
. Therefore, its memory lock is a mutex operation for the same object, the static member belongs to the class proprietary, and its memory space is shared by all the members of the class. This causes synchronized () to lock the static member, which is equivalent to locking the class, that is, all the members of the class are mutually exclusive. At the same time, only one thread can access the instance of the class. If you simply want to implement thread mutex in Java, it is enough to understand t
usage and differences between sleep (), wait (), and notify () and Notifyall (), Suspend and resume (), yield (), join (), interrupt () in Java threadsfrom 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 serviced may feel that the quality of the CPU service is
often on the go, about in Java notify and Notifyall, often people have the following statements:
1
And it seems like there are examples to prove it. The above statement, can be said right, can also say wrong. The reason is that one of the key points is that the official statement is as follows:
1 2 3 4 5 6 7 8
Wait,notify,notifyall: This method should be
1. Yield ()This method is similar to sleep (), except that it is not possible for the user to specify how long to pause, and the yield () method only allows the same priority thread to have an opportunity to execute.2. Wait () and notify (), Notifyall ()These three methods are used to coordinate the access of multiple threads to shared data, so they must be used within the synchronized statement block. The Synchronized keyword is used to protect share
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.