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 ();}}