Difference between Java Thread sleep and wait

Source: Internet
Author: User

When writing Java thread programs, we often ignore the difference between sleep and wait methods, leading to some very difficult problems, therefore, understanding the differences between the two methods will help us compile better programs. Difference: sleep () wait () sleep is the Thread class method. When wait is the object class method sleep (), when the object lock wait () is sleep, releasing the object lock cannot access the synchronization code block. The code block can be accessed: [java] package com. jony. test; public class ThreadTest implements Runnable {int number = 10; public void firstMethod () throws Exception {System. out. println ("first"); System. out. println (Thread. currentThread (); System. out. println (this); synchronized (this) {number + = 100; System. out. println (n Umber); y () ;}} public void secondMethod () throws Exception {System. out. println ("second"); System. out. println (Thread. currentThread (); System. out. println (this); When synchronized (this) {/** sleep () is sleeping, the object lock remains occupied. When wait () is sleeping, the object lock is released. Therefore: * (1) sleep won't access other synchronous code blocks * (2) wait will access other synchronous code blocks * (rest for 2 S and block the thread) * To verify whether other synchronization code blocks can be accessed when the lock of the current Thread object is occupied * // Thread. sleep (2000); // this. wait (2000); // you can only call the wait method in the synchronous code block this. wait (); System. out. println ("Before:" + number); number * = 200; System. out. println ("After:" + number) ;}@ Override public void run () {try {firstMethod () ;}catch (Exception e) {e. printStackTrace ();} www.2cto.com} public static void main (String [] args) throws Exception {ThreadTest threadTest = new ThreadTest ();, Thread thread = new Thread (threadTest); System. out. println (Thread. currentThread (); // thread. run (); // no new thread will be created, and the run method thread will be called directly. start (); // start to execute this Thread (create a new Thread). the Java Virtual Machine calls this Thread's run method // Thread. sleep (1000); threadTest. secondMethod ();}}

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.