JAVA Threading Basics Summary

Source: Internet
Author: User

    1. At any one time, the object's control (monitor) can only be owned by one thread.

    2. Whether you are executing an object's wait, notify, or Notifyall method, you must ensure that the currently running thread has control over the object (monitor)

    3. If the above three methods of the object are executed in a thread without control, the java.lang.IllegalMonitorStateException exception is reported.

    4. The JVM is multi-threaded and does not guarantee the timing of runtime threads by default

Illegalmonitorstateexception wait or notify have to make sure they get the lock on the object.


If you do not use wait&notify, the thread will always try to occupy the lock, wasting performance.


Wait will release the lock but notify will release it? What happens if it is not released.

Will not be released if you release the thread to run halfway there will be a problem until the entire synchronization statement is self-completed before it is released.


package org.famous.unyielding.current;import java.util.vector;public class client { Public static void main (String[] args)  {int goods = 0;Object  Plock = new object (); Vector<string> message = new vector<string> (); Thread customer = new thread (New customer (message,  "Customer",  pLock)); Thread producter = new thread (New producter (message,  "Producter",  pLock)); Customer.start ();p Roducter.start ();}} package org.famous.unyielding.current;import java.util.vector;/** *  *  @author  patzheng * */public class Customer implements Runnable {private  Vector<string> messages = new vector ();p rivate string threadname;private  object plock;public customer (VECTOR&LT;STRING&GT;&NBSP;MESSAGES,&NBSP;STRING&NBsp;threadname, object plock)  {this.messages = messages;this.threadName =  Threadname;this.plock = plock;} @Overridepublic  void run ()  {thread.currentthread (). SetName (ThreadName); System.err.println (Thread.CurrentThread (). GetName ()  +  "Goods ' S size"  + messages.size ( ));synchronized  (Plock)  {while  (True)  {if  (Messages.size ()  <= 0)  { Try {plock.wait (); System.err.println ("customer wait");}  catch  (interruptedexception e)  {e.printstacktrace ();}}  else {system.err.println (Thread.CurrentThread (). GetName ()  +  "Goods ' S size"  +  messages.size ()); System.err.println ("Customer notify producer"); messages.remove (0); try {thread.sleep (1000);}  catch  (interruptedexception e)  {e.printstacktrace ();} Plock.notify ();}}}} Package org.famous.unyielding.current;import java.util.date;import java.util.vector;public class producter implements runnable {private  Vector<string> messages = new vector ();p rivate string threadname;private  object plock;public producter (Vector<string> messages, string threadname,  object plock)  {this.messages = messages;this.threadName = threadName; This.plock = plock;} @Overridepublic  void run ()  {thread.currentthread (). SetName (ThreadName); System.err.println (Thread.CurrentThread (). GetName ()  +  "Goods ' S size"  + messages.size ( ));synchronized  (Plock)  {while  (True)  {if  (Messages.size ()  >= 1)  { Try {system.err.println ("producer wait");p lock.wait ();}  catch  (interruptedexception e)  {e.printstacktrace ();}}  else {messages.add (New date (). toString ()); System.err.println (Thread.currentthreaD (). GetName ()  +  "Goods ' S size"  + messages.size ()); System.err.println ("Producer notify customer"); Try {thread.sleep (1000);}  catch  (interruptedexception e)  {// TODO Auto-generated catch  Blocke.printstacktrace ();} Plock.notify ();}}}}








JAVA Threading Basics Summary

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.