问题描述
由于平时开发中很少用到多线程,可是最近面试总是问多线程,我答不上来,很是尴尬,自己看了这点方面的知识,有个问题请教下大家:直接上代码了代码:public class RunnableTest2 implements Runnable {public void run() {for (int i = 0; i < 100; i++) {System.out.println(Thread.currentThread().getName() + " for loop "+ i);}synchronized (this) {for (int i = 0; i < 100; i++) {if (i == 50) {System.out.println("当前线程被睡眠"+Thread.currentThread().getName());try {Thread.currentThread().sleep(5000);} catch (Exception e) {e.printStackTrace();}}System.out.println(Thread.currentThread().getName()+ "synchronized for loop " + i);}}}public static void main(String[] args) {RunnableTest2 r1 = new RunnableTest2();RunnableTest2 r2 = new RunnableTest2();Thread t1 = new Thread(r1);Thread t2 = new Thread(r2);t1.start();t2.start();}}print结果:Thread-0 for loop 0Thread-0 for loop 1Thread-1 for loop 0Thread-1 for loop 1Thread-1 for loop 2Thread-1 for loop 3Thread-1 for loop 4Thread-1 for loop 5Thread-1 for loop 6Thread-1 for loop 7Thread-1 for loop 8Thread-1 for loop 9Thread-1 for loop 10Thread-1 for loop 11Thread-1 for loop 12Thread-1 for loop 13Thread-1 for loop 14Thread-1 for loop 15Thread-1 for loop 16Thread-1 for loop 17Thread-1 for loop 18Thread-1 for loop 19Thread-1 for loop 20Thread-1 for loop 21Thread-1 for loop 22Thread-1 for loop 23Thread-1 for loop 24Thread-1 for loop 25Thread-1 for loop 26Thread-1 for loop 27Thread-1 for loop 28Thread-1 for loop 29Thread-1 for loop 30Thread-1 for loop 31Thread-1 for loop 32Thread-1 for loop 33Thread-1 for loop 34Thread-1 for loop 35Thread-1 for loop 36Thread-1 for loop 37Thread-1 for loop 38Thread-1 for loop 39Thread-1 for loop 40Thread-1 for loop 41Thread-1 for loop 42Thread-1 for loop 43Thread-1 for loop 44Thread-1 for loop 45Thread-1 for loop 46Thread-1 for loop 47Thread-1 for loop 48Thread-1 for loop 49Thread-1 for loop 50Thread-1 for loop 51Thread-1 for loop 52Thread-1 for loop 53Thread-1 for loop 54Thread-1 for loop 55Thread-1 for loop 56Thread-1 for loop 57Thread-1 for loop 58Thread-1 for loop 59Thread-1 for loop 60Thread-1 for loop 61Thread-1 for loop 62Thread-1 for loop 63Thread-1 for loop 64Thread-1 for loop 65Thread-1 for loop 66Thread-1 for loop 67Thread-1 for loop 68Thread-1 for loop 69Thread-1 for loop 70Thread-1 for loop 71Thread-1 for loop 72Thread-1 for loop 73Thread-1 for loop 74Thread-1 for loop 75Thread-1 for loop 76Thread-1 for loop 77Thread-1 for loop 78Thread-1 for loop 79Thread-1 for loop 80Thread-1 for loop 81Thread-1 for loop 82Thread-1 for loop 83Thread-1 for loop 84Thread-1 for loop 85Thread-1 for loop 86Thread-1 for loop 87Thread-1 for loop 88Thread-1 for loop 89Thread-1 for loop 90Thread-1 for loop 91Thread-1 for loop 92Thread-1 for loop 93Thread-1 for loop 94Thread-1 for loop 95Thread-1 for loop 96Thread-1 for loop 97Thread-1 for loop 98Thread-1 for loop 99Thread-1synchronized for loop 0Thread-1synchronized for loop 1Thread-1synchronized for loop 2Thread-1synchronized for loop 3Thread-1synchronized for loop 4Thread-1synchronized for loop 5Thread-1synchronized for loop 6Thread-1synchronized for loop 7Thread-1synchronized for loop 8Thread-1synchronized for loop 9Thread-1synchronized for loop 10Thread-1synchronized for loop 11Thread-1synchronized for loop 12Thread-1synchronized for loop 13Thread-1synchronized for loop 14Thread-1synchronized for loop 15Thread-1synchronized for loop 16Thread-1synchronized for loop 17Thread-1synchronized for loop 18Thread-1synchronized for loop 19Thread-1synchronized for loop 20Thread-1synchronized for loop 21Thread-1synchronized for loop 22Thread-1synchronized for loop 23Thread-1synchronized for loop 24Thread-1synchronized for loop 25Thread-1synchronized for loop 26Thread-1synchronized for loop 27Thread-1synchronized for loop 28Thread-1synchronized for loop 29Thread-1synchronized for loop 30Thread-1synchronized for loop 31Thread-1synchronized for loop 32Thread-1synchronized for loop 33Thread-1synchronized for loop 34Thread-1synchronized for loop 35Thread-1synchronized for loop 36Thread-1synchronized for loop 37Thread-1synchronized for loop 38Thread-1synchronized for loop 39Thread-1synchronized for loop 40Thread-1synchronized for loop 41Thread-1synchronized for loop 42Thread-1synchronized for loop 43Thread-1synchronized for loop 44Thread-1synchronized for loop 45Thread-1synchronized for loop 46Thread-1synchronized for loop 47Thread-1synchronized for loop 48Thread-1synchronized for loop 49当前线程被睡眠Thread-1Thread-0 for loop 2Thread-0 for loop 3Thread-0 for loop 4Thread-0 for loop 5Thread-0 for loop 6Thread-0 for loop 7Thread-0 for loop 8Thread-0 for loop 9Thread-0 for loop 10Thread-0 for loop 11Thread-0 for loop 12Thread-0 for loop 13Thread-0 for loop 14Thread-0 for loop 15Thread-0 for loop 16Thread-0 for loop 17Thread-0 for loop 18Thread-0 for loop 19Thread-0 for loop 20Thread-0 for loop 21Thread-0 for loop 22Thread-0 for loop 23Thread-0 for loop 24Thread-0 for loop 25Thread-0 for loop 26Thread-0 for loop 27Thread-0 for loop 28Thread-0 for loop 29Thread-0 for loop 30Thread-0 for loop 31Thread-0 for loop 32Thread-0 for loop 33Thread-0 for loop 34Thread-0 for loop 35Thread-0 for loop 36Thread-0 for loop 37Thread-0 for loop 38Thread-0 for loop 39Thread-0 for loop 40Thread-0 for loop 41Thread-0 for loop 42Thread-0 for loop 43Thread-0 for loop 44Thread-0 for loop 45Thread-0 for loop 46Thread-0 for loop 47Thread-0 for loop 48Thread-0 for loop 49Thread-0 for loop 50Thread-0 for loop 51Thread-0 for loop 52Thread-0 for loop 53Thread-0 for loop 54Thread-0 for loop 55Thread-0 for loop 56Thread-0 for loop 57Thread-0 for loop 58Thread-0 for loop 59Thread-0 for loop 60Thread-0 for loop 61Thread-0 for loop 62Thread-0 for loop 63Thread-0 for loop 64Thread-0 for loop 65Thread-0 for loop 66Thread-0 for loop 67Thread-0 for loop 68Thread-0 for loop 69Thread-0 for loop 70Thread-0 for loop 71Thread-0 for loop 72Thread-0 for loop 73Thread-0 for loop 74Thread-0 for loop 75Thread-0 for loop 76Thread-0 for loop 77Thread-0 for loop 78Thread-0 for loop 79Thread-0 for loop 80Thread-0 for loop 81Thread-0 for loop 82Thread-0 for loop 83Thread-0 for loop 84Thread-0 for loop 85Thread-0 for loop 86Thread-0 for loop 87Thread-0 for loop 88Thread-0 for loop 89Thread-0 for loop 90Thread-0 for loop 91Thread-0 for loop 92Thread-0 for loop 93Thread-0 for loop 94Thread-0 for loop 95Thread-0 for loop 96Thread-0 for loop 97Thread-0 for loop 98Thread-0 for loop 99Thread-0synchronized for loop 0Thread-0synchronized for loop 1Thread-0synchronized for loop 2Thread-0synchronized for loop 3Thread-0synchronized for loop 4Thread-0synchronized for loop 5Thread-0synchronized for loop 6Thread-0synchronized for loop 7Thread-0synchronized for loop 8Thread-0synchronized for loop 9Thread-0synchronized for loop 10Thread-0synchronized for loop 11Thread-0synchronized for loop 12Thread-0synchronized for loop 13Thread-0synchronized for loop 14Thread-0synchronized for loop 15Thread-0synchronized for loop 16Thread-0synchronized for loop 17Thread-0synchronized for loop 18Thread-0synchronized for loop 19Thread-0synchronized for loop 20Thread-0synchronized for loop 21Thread-0synchronized for loop 22Thread-0synchronized for loop 23Thread-0synchronized for loop 24Thread-0synchronized for loop 25Thread-0synchronized for loop 26Thread-0synchronized for loop 27Thread-0synchronized for loop 28Thread-0synchronized for loop 29Thread-0synchronized for loop 30Thread-0synchronized for loop 31Thread-0synchronized for loop 32Thread-0synchronized for loop 33Thread-0synchronized for loop 34Thread-0synchronized for loop 35Thread-0synchronized for loop 36Thread-0synchronized for loop 37Thread-0synchronized for loop 38Thread-0synchronized for loop 39Thread-0synchronized for loop 40Thread-0synchronized for loop 41Thread-0synchronized for loop 42Thread-0synchronized for loop 43Thread-0synchronized for loop 44Thread-0synchronized for loop 45Thread-0synchronized for loop 46Thread-0synchronized for loop 47Thread-0synchronized for loop 48Thread-0synchronized for loop 49当前线程被睡眠Thread-0Thread-1synchronized for loop 50Thread-1synchronized for loop 51Thread-1synchronized for loop 52Thread-1synchronized for loop 53Thread-1synchronized for loop 54Thread-1synchronized for loop 55Thread-1synchronized for loop 56Thread-1synchronized for loop 57Thread-1synchronized for loop 58Thread-1synchronized for loop 59Thread-1synchronized for loop 60Thread-1synchronized for loop 61Thread-1synchronized for loop 62Thread-1synchronized for loop 63Thread-1synchronized for loop 64Thread-1synchronized for loop 65Thread-1synchronized for loop 66Thread-1synchronized for loop 67Thread-1synchronized for loop 68Thread-1synchronized for loop 69Thread-1synchronized for loop 70Thread-1synchronized for loop 71Thread-1synchronized for loop 72Thread-1synchronized for loop 73Thread-1synchronized for loop 74Thread-1synchronized for loop 75Thread-1synchronized for loop 76Thread-1synchronized for loop 77Thread-1synchronized for loop 78Thread-1synchronized for loop 79Thread-1synchronized for loop 80Thread-1synchronized for loop 81Thread-1synchronized for loop 82Thread-1synchronized for loop 83Thread-1synchronized for loop 84Thread-1synchronized for loop 85Thread-1synchronized for loop 86Thread-1synchronized for loop 87Thread-1synchronized for loop 88Thread-1synchronized for loop 89Thread-1synchronized for loop 90Thread-1synchronized for loop 91Thread-1synchronized for loop 92Thread-1synchronized for loop 93Thread-1synchronized for loop 94Thread-1synchronized for loop 95Thread-1synchronized for loop 96Thread-1synchronized for loop 97Thread-1synchronized for loop 98Thread-1synchronized for loop 99Thread-0synchronized for loop 50Thread-0synchronized for loop 51Thread-0synchronized for loop 52Thread-0synchronized for loop 53Thread-0synchronized for loop 54Thread-0synchronized for loop 55Thread-0synchronized for loop 56Thread-0synchronized for loop 57Thread-0synchronized for loop 58Thread-0synchronized for loop 59Thread-0synchronized for loop 60Thread-0synchronized for loop 61Thread-0synchronized for loop 62Thread-0synchronized for loop 63Thread-0synchronized for loop 64Thread-0synchronized for loop 65Thread-0synchronized for loop 66Thread-0synchronized for loop 67Thread-0synchronized for loop 68Thread-0synchronized for loop 69Thread-0synchronized for loop 70Thread-0synchronized for loop 71Thread-0synchronized for loop 72Thread-0synchronized for loop 73Thread-0synchronized for loop 74Thread-0synchronized for loop 75Thread-0synchronized for loop 76Thread-0synchronized for loop 77Thread-0synchronized for loop 78Thread-0synchronized for loop 79Thread-0synchronized for loop 80Thread-0synchronized for loop 81Thread-0synchronized for loop 82Thread-0synchronized for loop 83Thread-0synchronized for loop 84Thread-0synchronized for loop 85Thread-0synchronized for loop 86Thread-0synchronized for loop 87Thread-0synchronized for loop 88Thread-0synchronized for loop 89Thread-0synchronized for loop 90Thread-0synchronized for loop 91Thread-0synchronized for loop 92Thread-0synchronized for loop 93Thread-0synchronized for loop 94Thread-0synchronized for loop 95Thread-0synchronized for loop 96Thread-0synchronized for loop 97Thread-0synchronized for loop 98Thread-0synchronized for loop 99我的理解是:Thread-1先占用了同步块,跑到50的时候,睡了,让给了Thread-0,Thread-0跑完第一个循环,跑到了第二循环的50的时候也睡了,然后他们都开始等,由于Thread-0是后睡了,所以Thread-1先醒,醒了就跑,跑完了Thread-0跑。不知道我的理解对不对。
解决方案
楼主,关于sleep方法,会导致线程睡眠,但是只会让出CPU的时间片,不会让出当前线程的锁的。另外,你创建的是两个Runnable的对象,在类内部synchronized(this) { ....}//这其中的this即当前使用的对象,在两个不同对象执行同一个方法时,不会达到互斥效果的。所以,为了实现一个你要的效果可以wait/notify,在一个线程执行过程中使用wait让出拥有的锁,此时notify的执行会让另一个等待锁的线程获取到,并继续执行。
解决方案二:
1.首先sleep()方法会让当前线程睡眠(停止执行)mills毫秒,线程由运行中的状态进入不可运行状态,睡眠时间过后线程会再进入可运行状态Thread-1 与Thread-0 他们是片段执行而已 没有说那个一定要先执行完 再执行那个我把你的循环修改成10了package cn.com.chinagril.test;public class Test implements Runnable { public void run() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " for loop " + i); } synchronized (this) { for (int i = 0; i < 10; i++) { if (i == 50) { System.out.println("当前线程被睡眠"+Thread.currentThread().getName()); try { Thread.currentThread().sleep(5000); } catch (Exception e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "synchronized for loop " + i); } } } public static void main(String[] args) { Test r1 = new Test(); Test r2 = new Test(); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); } }Thread-0 for loop 0Thread-1 for loop 0Thread-0 for loop 1Thread-1 for loop 1Thread-0 for loop 2Thread-1 for loop 2Thread-0 for loop 3Thread-1 for loop 3Thread-0 for loop 4Thread-1 for loop 4Thread-1 for loop 5Thread-1 for loop 6Thread-1 for loop 7Thread-1 for loop 8Thread-0 for loop 5Thread-0 for loop 6Thread-1 for loop 9Thread-0 for loop 7Thread-1synchronized for loop 0Thread-0 for loop 8Thread-1synchronized for loop 1Thread-0 for loop 9Thread-1synchronized for loop 2Thread-0synchronized for loop 0Thread-1synchronized for loop 3Thread-0synchronized for loop 1Thread-1synchronized for loop 4Thread-0synchronized for loop 2Thread-1synchronized for loop 5Thread-0synchronized for loop 3Thread-1synchronized for loop 6Thread-0synchronized for loop 4Thread-1synchronized for loop 7Thread-1synchronized for loop 8Thread-0synchronized for loop 5Thread-1synchronized for loop 9Thread-0synchronized for loop 6Thread-0synchronized for loop 7Thread-0synchronized for loop 8Thread-0synchronized for loop 9