1.sleep是Thread的一个静态(static)方法。使得Runnable实现的线程也可使用sleep方法。并且避免了线程以前相互调用sleep()方法,引起死锁。java
2.sleep()执行时须要赋予一个沉睡时间。在沉睡期间(阻塞线程期间),CPU会放弃这个线程,执行其余任务。当沉睡时间到了以后,该线程会自动苏醒,不过此时线程不会马上被执行,而是要等CPU分配资源,和其余线程进行竞争。并发
3.此外若是这个线程以前获取了一个机锁,在沉睡期间,这个机锁不会释放。其余等待这个机锁的程序,必须等待这个线程醒来,且执行完后才能运行。ide
public class ThreadTest2 { public static void main(String[] args){ System.out.println("begin our test"); ThreadSleep sleep = new ThreadSleep(); try { Thread thread1 = new Thread(sleep,"路人甲"); Thread thread2 = new Thread(sleep,"路人乙"); thread1.start(); thread2.start(); }catch(Exception e){ e.printStackTrace(); } System.out.println("test is over"); } } class ThreadSleep implements Runnable{ int count = 0; @Override public void run(){ System.out.println(Thread.currentThread().getName() + " say : hello sleep !!"); count(); } public void count(){ while(count < 20) { System.out.println(Thread.currentThread().getName() + " say : count is " + count); try { count++; Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } } }
输出日志this
begin our test test is over 路人甲 say : hello sleep !! 路人甲 say : count is 0 路人乙 say : hello sleep !! 路人乙 say : count is 1 路人甲 say : count is 2 路人乙 say : count is 2 路人甲 say : count is 4 路人乙 say : count is 4 路人甲 say : count is 6 路人乙 say : count is 7 路人乙 say : count is 8 路人甲 say : count is 8 路人甲 say : count is 10 路人乙 say : count is 10 路人乙 say : count is 12 路人甲 say : count is 12 路人乙 say : count is 14 路人甲 say : count is 14 路人甲 say : count is 16 路人乙 say : count is 16 路人甲 say : count is 18 路人乙 say : count is 18
经过日志能够发现线程甲和线程乙基本是交替执行,可是并不规律,且出现了并发问题。spa
该状况是因为代码中设置了睡眠时间为100毫秒,因为count递增执行速度很快,因此线程差很少是同时睡眠,而后同时苏醒并致使了并发的出现。线程
接下来要添加synchronize块,检查sleep时机锁是否释放日志
public class ThreadTest2 { public static void main(String[] args){ System.out.println("begin our test"); ThreadSleep sleep = new ThreadSleep(); try { Thread thread1 = new Thread(sleep,"路人甲"); Thread thread2 = new Thread(sleep,"路人乙"); thread1.start(); thread2.start(); }catch(Exception e){ e.printStackTrace(); } System.out.println("test is over"); } } class ThreadSleep implements Runnable{ int count = 0; @Override public void run(){ System.out.println(Thread.currentThread().getName() + " say : hello sleep !!"); count(); } public void count(){ while(count < 20) { synchronized (this) { System.out.println(Thread.currentThread().getName() + " say : count is " + count); try { count++; Thread.sleep(100); } catch (Exception e) { e.printStackTrace(); } } } } }
输出日志code
begin our test 路人甲 say : hello sleep !! 路人甲 say : count is 0 test is over 路人乙 say : hello sleep !! 路人甲 say : count is 1 路人甲 say : count is 2 路人甲 say : count is 3 路人甲 say : count is 4 路人甲 say : count is 5 路人甲 say : count is 6 路人甲 say : count is 7 路人甲 say : count is 8 路人甲 say : count is 9 路人甲 say : count is 10 路人甲 say : count is 11 路人甲 say : count is 12 路人甲 say : count is 13 路人甲 say : count is 14 路人甲 say : count is 15 路人甲 say : count is 16 路人甲 say : count is 17 路人甲 say : count is 18 路人甲 say : count is 19 路人乙 say : count is 20
经过日志能够看出,基本是线程甲在执行,这是由于sleep时,机锁一直在线程甲上,因此线程乙只能一直等待直到线程甲释放锁。对象
1.wait()是Object类的一个方法。当调用wait()方法时,该线程会进入和该对象相关的等待池中,并释放它所拥有的机锁。资源
2.执行wait()后,必须使用notify()方法或notifyAll()方法或设置等待时间(wait(long time))唤醒在等待线程池中的线程。
3.wait()必须放在synchronized block中,不然会在运行时报“java.lang.IllegalMonitorStateException”异常
wait相关代码
public class ThreadTest2 { public static void main(String[] args) { System.out.println("begin our test"); ThreadSleep sleep = new ThreadSleep(); try { Thread thread1 = new Thread(sleep, "路人甲"); Thread thread2 = new Thread(sleep, "路人乙"); thread1.start(); thread2.start(); } catch (Exception e) { e.printStackTrace(); } System.out.println("test is over"); } } class ThreadSleep implements Runnable { int count = 0; @Override public void run() { System.out.println(Thread.currentThread().getName() + " say : hello sleep !!"); count(); } public void count() { while (count < 20) { synchronized (this) { System.out.println(Thread.currentThread().getName() + " say : count is " + count); try { count++; this.wait(100); } catch (Exception e) { e.printStackTrace(); } } } } }
输出日志
begin our test 路人甲 say : hello sleep !! 路人甲 say : count is 0 test is over 路人乙 say : hello sleep !! 路人乙 say : count is 1 路人甲 say : count is 2 路人乙 say : count is 3 路人甲 say : count is 4 路人乙 say : count is 5 路人甲 say : count is 6 路人乙 say : count is 7 路人甲 say : count is 8 路人乙 say : count is 9 路人甲 say : count is 10 路人乙 say : count is 11 路人甲 say : count is 12 路人乙 say : count is 13 路人乙 say : count is 14 路人甲 say : count is 15 路人乙 say : count is 16 路人甲 say : count is 17 路人乙 say : count is 18 路人甲 say : count is 19
经过日志能够发如今wait的状况下,机锁会被释放。