Java notes--about thread-related methods of object classes

Source: Internet
Author: User

About threading methods in the object class

The object class is the parent class for all Java classes, where three methods related to threading are defined, so that all Java classes support multithreading after they are created

These three methods are:notify (), Notifyall (), Wait (), which are used to control the running state of the thread.

The list of methods is as follows
Notify (): Wakes a single thread waiting on this object monitor
Notifyall (): Wakes up all the threads waiting on this object monitor
Wait (): Causes the current thread to wait until the Notify () or Notifyall () method of this object is called on another thread
Wait (long Timeout): Causes the current thread to wait before the Notify () or Notifyall () method is called or before the specified time
Wait (long Timeout,int Nanos): Before the Notify () or Notifyall () method is called or before the specified time,
or other threads before the current thread is interrupted, causing the current thread to wait.
--If you want to reprint this article please indicate the reprint address "http://www.cnblogs.com/XHJT/p/3905372.html" Thank you--
Note: In the object class, all of the above methods are final, remember not to be confused with the thread class
And these methods are to be used with the Synchronized keyword, they are all related to object monitor,
The current thread must have a monitor for this object, or a illegalmonitorstateexception exception will occur.

code example:

 PackageCom.xhj.thread;ImportJava.util.Random;/*** Application of thread-related methods in the object class * *@authorXiehejun **/ Public classObjectthreadmethod {/*** Define the highest number of items*/    Private intCount = 10; /*** Number of items recorded in warehouse during production*/    Private intsum = 0; Private classProducterImplementsRunnable {@Override Public voidrun () { for(inti = 0; I < count; i++) {                intnum =NewRandom (). Nextint (255); synchronized( This) {                    if(Sum = =count) {System.out.println ("The Warehouse is full"); Try {                             This. Wait (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    } Else{System.out.println ("Production item" + num + "No.");  This. Notify (); Sum++; System.out.println ("Warehouse and goods" + sum + "pieces"); Try{Thread.Sleep (100); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    }                }            }        }    }    Private classConsomerImplementsRunnable {@Override Public voidrun () { for(inti = 0; I < count; i++) {                synchronized( This) {                    if(sum = = 0) {System.out.println ("The warehouse is empty, please replenishment"); Try {                             This. Wait (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    } Else{System.out.println ("I bought a piece of merchandise for consumption.");  This. Notify (); Sum--; System.out.println ("Warehouse and goods" + sum + "pieces"); Try{Thread.Sleep (100); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    }                }            }        }    }    /*** Call Threading Service*/     Public voidService () {Producter productor=NewProducter (); Consomer Consomer=NewConsomer (); Thread Thread1=NewThread (Productor); Thread thread2=NewThread (Consomer);        Thread1.start ();    Thread2.start (); }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubObjectthreadmethod OB =New Objectthreadmethod ();    Ob.service (); }}

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.