Runnable问题一个问题

由于平时开发中很少用到多线程,可是最近面试总是问多线程,我答不上来,很是尴尬,自己看了这点方面的知识,有个问题请教下大家:直接上代码了
代码:
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 0
Thread-0 for loop 1
Thread-1 for loop 0
Thread-1 for loop 1
Thread-1 for loop 2
Thread-1 for loop 3
Thread-1 for loop 4
Thread-1 for loop 5
Thread-1 for loop 6
Thread-1 for loop 7
Thread-1 for loop 8
Thread-1 for loop 9
Thread-1 for loop 10
Thread-1 for loop 11
Thread-1 for loop 12
Thread-1 for loop 13
Thread-1 for loop 14
Thread-1 for loop 15
Thread-1 for loop 16
Thread-1 for loop 17
Thread-1 for loop 18
Thread-1 for loop 19
Thread-1 for loop 20
Thread-1 for loop 21
Thread-1 for loop 22
Thread-1 for loop 23
Thread-1 for loop 24
Thread-1 for loop 25
Thread-1 for loop 26
Thread-1 for loop 27
Thread-1 for loop 28
Thread-1 for loop 29
Thread-1 for loop 30
Thread-1 for loop 31
Thread-1 for loop 32
Thread-1 for loop 33
Thread-1 for loop 34
Thread-1 for loop 35
Thread-1 for loop 36
Thread-1 for loop 37
Thread-1 for loop 38
Thread-1 for loop 39
Thread-1 for loop 40
Thread-1 for loop 41
Thread-1 for loop 42
Thread-1 for loop 43
Thread-1 for loop 44
Thread-1 for loop 45
Thread-1 for loop 46
Thread-1 for loop 47
Thread-1 for loop 48
Thread-1 for loop 49
Thread-1 for loop 50
Thread-1 for loop 51
Thread-1 for loop 52
Thread-1 for loop 53
Thread-1 for loop 54
Thread-1 for loop 55
Thread-1 for loop 56
Thread-1 for loop 57
Thread-1 for loop 58
Thread-1 for loop 59
Thread-1 for loop 60
Thread-1 for loop 61
Thread-1 for loop 62
Thread-1 for loop 63
Thread-1 for loop 64
Thread-1 for loop 65
Thread-1 for loop 66
Thread-1 for loop 67
Thread-1 for loop 68
Thread-1 for loop 69
Thread-1 for loop 70
Thread-1 for loop 71
Thread-1 for loop 72
Thread-1 for loop 73
Thread-1 for loop 74
Thread-1 for loop 75
Thread-1 for loop 76
Thread-1 for loop 77
Thread-1 for loop 78
Thread-1 for loop 79
Thread-1 for loop 80
Thread-1 for loop 81
Thread-1 for loop 82
Thread-1 for loop 83
Thread-1 for loop 84
Thread-1 for loop 85
Thread-1 for loop 86
Thread-1 for loop 87
Thread-1 for loop 88
Thread-1 for loop 89
Thread-1 for loop 90
Thread-1 for loop 91
Thread-1 for loop 92
Thread-1 for loop 93
Thread-1 for loop 94
Thread-1 for loop 95
Thread-1 for loop 96
Thread-1 for loop 97
Thread-1 for loop 98
Thread-1 for loop 99
Thread-1synchronized for loop 0
Thread-1synchronized for loop 1
Thread-1synchronized for loop 2
Thread-1synchronized for loop 3
Thread-1synchronized for loop 4
Thread-1synchronized for loop 5
Thread-1synchronized for loop 6
Thread-1synchronized for loop 7
Thread-1synchronized for loop 8
Thread-1synchronized for loop 9
Thread-1synchronized for loop 10
Thread-1synchronized for loop 11
Thread-1synchronized for loop 12
Thread-1synchronized for loop 13
Thread-1synchronized for loop 14
Thread-1synchronized for loop 15
Thread-1synchronized for loop 16
Thread-1synchronized for loop 17
Thread-1synchronized for loop 18
Thread-1synchronized for loop 19
Thread-1synchronized for loop 20
Thread-1synchronized for loop 21
Thread-1synchronized for loop 22
Thread-1synchronized for loop 23
Thread-1synchronized for loop 24
Thread-1synchronized for loop 25
Thread-1synchronized for loop 26
Thread-1synchronized for loop 27
Thread-1synchronized for loop 28
Thread-1synchronized for loop 29
Thread-1synchronized for loop 30
Thread-1synchronized for loop 31
Thread-1synchronized for loop 32
Thread-1synchronized for loop 33
Thread-1synchronized for loop 34
Thread-1synchronized for loop 35
Thread-1synchronized for loop 36
Thread-1synchronized for loop 37
Thread-1synchronized for loop 38
Thread-1synchronized for loop 39
Thread-1synchronized for loop 40
Thread-1synchronized for loop 41
Thread-1synchronized for loop 42
Thread-1synchronized for loop 43
Thread-1synchronized for loop 44
Thread-1synchronized for loop 45
Thread-1synchronized for loop 46
Thread-1synchronized for loop 47
Thread-1synchronized for loop 48
Thread-1synchronized for loop 49
当前线程被睡眠Thread-1
Thread-0 for loop 2
Thread-0 for loop 3
Thread-0 for loop 4
Thread-0 for loop 5
Thread-0 for loop 6
Thread-0 for loop 7
Thread-0 for loop 8
Thread-0 for loop 9
Thread-0 for loop 10
Thread-0 for loop 11
Thread-0 for loop 12
Thread-0 for loop 13
Thread-0 for loop 14
Thread-0 for loop 15
Thread-0 for loop 16
Thread-0 for loop 17
Thread-0 for loop 18
Thread-0 for loop 19
Thread-0 for loop 20
Thread-0 for loop 21
Thread-0 for loop 22
Thread-0 for loop 23
Thread-0 for loop 24
Thread-0 for loop 25
Thread-0 for loop 26
Thread-0 for loop 27
Thread-0 for loop 28
Thread-0 for loop 29
Thread-0 for loop 30
Thread-0 for loop 31
Thread-0 for loop 32
Thread-0 for loop 33
Thread-0 for loop 34
Thread-0 for loop 35
Thread-0 for loop 36
Thread-0 for loop 37
Thread-0 for loop 38
Thread-0 for loop 39
Thread-0 for loop 40
Thread-0 for loop 41
Thread-0 for loop 42
Thread-0 for loop 43
Thread-0 for loop 44
Thread-0 for loop 45
Thread-0 for loop 46
Thread-0 for loop 47
Thread-0 for loop 48
Thread-0 for loop 49
Thread-0 for loop 50
Thread-0 for loop 51
Thread-0 for loop 52
Thread-0 for loop 53
Thread-0 for loop 54
Thread-0 for loop 55
Thread-0 for loop 56
Thread-0 for loop 57
Thread-0 for loop 58
Thread-0 for loop 59
Thread-0 for loop 60
Thread-0 for loop 61
Thread-0 for loop 62
Thread-0 for loop 63
Thread-0 for loop 64
Thread-0 for loop 65
Thread-0 for loop 66
Thread-0 for loop 67
Thread-0 for loop 68
Thread-0 for loop 69
Thread-0 for loop 70
Thread-0 for loop 71
Thread-0 for loop 72
Thread-0 for loop 73
Thread-0 for loop 74
Thread-0 for loop 75
Thread-0 for loop 76
Thread-0 for loop 77
Thread-0 for loop 78
Thread-0 for loop 79
Thread-0 for loop 80
Thread-0 for loop 81
Thread-0 for loop 82
Thread-0 for loop 83
Thread-0 for loop 84
Thread-0 for loop 85
Thread-0 for loop 86
Thread-0 for loop 87
Thread-0 for loop 88
Thread-0 for loop 89
Thread-0 for loop 90
Thread-0 for loop 91
Thread-0 for loop 92
Thread-0 for loop 93
Thread-0 for loop 94
Thread-0 for loop 95
Thread-0 for loop 96
Thread-0 for loop 97
Thread-0 for loop 98
Thread-0 for loop 99
Thread-0synchronized for loop 0
Thread-0synchronized for loop 1
Thread-0synchronized for loop 2
Thread-0synchronized for loop 3
Thread-0synchronized for loop 4
Thread-0synchronized for loop 5
Thread-0synchronized for loop 6
Thread-0synchronized for loop 7
Thread-0synchronized for loop 8
Thread-0synchronized for loop 9
Thread-0synchronized for loop 10
Thread-0synchronized for loop 11
Thread-0synchronized for loop 12
Thread-0synchronized for loop 13
Thread-0synchronized for loop 14
Thread-0synchronized for loop 15
Thread-0synchronized for loop 16
Thread-0synchronized for loop 17
Thread-0synchronized for loop 18
Thread-0synchronized for loop 19
Thread-0synchronized for loop 20
Thread-0synchronized for loop 21
Thread-0synchronized for loop 22
Thread-0synchronized for loop 23
Thread-0synchronized for loop 24
Thread-0synchronized for loop 25
Thread-0synchronized for loop 26
Thread-0synchronized for loop 27
Thread-0synchronized for loop 28
Thread-0synchronized for loop 29
Thread-0synchronized for loop 30
Thread-0synchronized for loop 31
Thread-0synchronized for loop 32
Thread-0synchronized for loop 33
Thread-0synchronized for loop 34
Thread-0synchronized for loop 35
Thread-0synchronized for loop 36
Thread-0synchronized for loop 37
Thread-0synchronized for loop 38
Thread-0synchronized for loop 39
Thread-0synchronized for loop 40
Thread-0synchronized for loop 41
Thread-0synchronized for loop 42
Thread-0synchronized for loop 43
Thread-0synchronized for loop 44
Thread-0synchronized for loop 45
Thread-0synchronized for loop 46
Thread-0synchronized for loop 47
Thread-0synchronized for loop 48
Thread-0synchronized for loop 49
当前线程被睡眠Thread-0
Thread-1synchronized for loop 50
Thread-1synchronized for loop 51
Thread-1synchronized for loop 52
Thread-1synchronized for loop 53
Thread-1synchronized for loop 54
Thread-1synchronized for loop 55
Thread-1synchronized for loop 56
Thread-1synchronized for loop 57
Thread-1synchronized for loop 58
Thread-1synchronized for loop 59
Thread-1synchronized for loop 60
Thread-1synchronized for loop 61
Thread-1synchronized for loop 62
Thread-1synchronized for loop 63
Thread-1synchronized for loop 64
Thread-1synchronized for loop 65
Thread-1synchronized for loop 66
Thread-1synchronized for loop 67
Thread-1synchronized for loop 68
Thread-1synchronized for loop 69
Thread-1synchronized for loop 70
Thread-1synchronized for loop 71
Thread-1synchronized for loop 72
Thread-1synchronized for loop 73
Thread-1synchronized for loop 74
Thread-1synchronized for loop 75
Thread-1synchronized for loop 76
Thread-1synchronized for loop 77
Thread-1synchronized for loop 78
Thread-1synchronized for loop 79
Thread-1synchronized for loop 80
Thread-1synchronized for loop 81
Thread-1synchronized for loop 82
Thread-1synchronized for loop 83
Thread-1synchronized for loop 84
Thread-1synchronized for loop 85
Thread-1synchronized for loop 86
Thread-1synchronized for loop 87
Thread-1synchronized for loop 88
Thread-1synchronized for loop 89
Thread-1synchronized for loop 90
Thread-1synchronized for loop 91
Thread-1synchronized for loop 92
Thread-1synchronized for loop 93
Thread-1synchronized for loop 94
Thread-1synchronized for loop 95
Thread-1synchronized for loop 96
Thread-1synchronized for loop 97
Thread-1synchronized for loop 98
Thread-1synchronized for loop 99
Thread-0synchronized for loop 50
Thread-0synchronized for loop 51
Thread-0synchronized for loop 52
Thread-0synchronized for loop 53
Thread-0synchronized for loop 54
Thread-0synchronized for loop 55
Thread-0synchronized for loop 56
Thread-0synchronized for loop 57
Thread-0synchronized for loop 58
Thread-0synchronized for loop 59
Thread-0synchronized for loop 60
Thread-0synchronized for loop 61
Thread-0synchronized for loop 62
Thread-0synchronized for loop 63
Thread-0synchronized for loop 64
Thread-0synchronized for loop 65
Thread-0synchronized for loop 66
Thread-0synchronized for loop 67
Thread-0synchronized for loop 68
Thread-0synchronized for loop 69
Thread-0synchronized for loop 70
Thread-0synchronized for loop 71
Thread-0synchronized for loop 72
Thread-0synchronized for loop 73
Thread-0synchronized for loop 74
Thread-0synchronized for loop 75
Thread-0synchronized for loop 76
Thread-0synchronized for loop 77
Thread-0synchronized for loop 78
Thread-0synchronized for loop 79
Thread-0synchronized for loop 80
Thread-0synchronized for loop 81
Thread-0synchronized for loop 82
Thread-0synchronized for loop 83
Thread-0synchronized for loop 84
Thread-0synchronized for loop 85
Thread-0synchronized for loop 86
Thread-0synchronized for loop 87
Thread-0synchronized for loop 88
Thread-0synchronized for loop 89
Thread-0synchronized for loop 90
Thread-0synchronized for loop 91
Thread-0synchronized for loop 92
Thread-0synchronized for loop 93
Thread-0synchronized for loop 94
Thread-0synchronized for loop 95
Thread-0synchronized for loop 96
Thread-0synchronized for loop 97
Thread-0synchronized for loop 98
Thread-0synchronized for loop 99
我的理解是:
Thread-1先占用了同步块,跑到50的时候,睡了,让给了Thread-0,Thread-0跑完第一个循环,跑到了第二循环的50的时候也睡了,然后他们都开始等,由于Thread-0是后睡了,所以Thread-1先醒,醒了就跑,跑完了Thread-0跑。
不知道我的理解对不对。

0

2个回答

楼主,关于sleep方法,会导致线程睡眠,但是只会让出CPU的时间片,不会让出当前线程的锁的。
另外,你创建的是两个Runnable的对象,在类内部
[code="java"]
synchronized(this) {
....
}
//这其中的this即当前使用的对象,在两个不同对象执行同一个方法时,不会达到互斥效果的。
[/code]

所以,为了实现一个你要的效果可以wait/notify,在一个线程执行过程中使用wait让出拥有的锁,此时notify的执行会让另一个等待锁的线程获取到,并继续执行。

0
wang475999
Simple00001 能加下QQ 你给我讲讲么 475999598
5 年多之前 回复

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 0
Thread-1 for loop 0
Thread-0 for loop 1
Thread-1 for loop 1
Thread-0 for loop 2
Thread-1 for loop 2
Thread-0 for loop 3
Thread-1 for loop 3
Thread-0 for loop 4
Thread-1 for loop 4
Thread-1 for loop 5
Thread-1 for loop 6
Thread-1 for loop 7
Thread-1 for loop 8
Thread-0 for loop 5
Thread-0 for loop 6
Thread-1 for loop 9
Thread-0 for loop 7
Thread-1synchronized for loop 0
Thread-0 for loop 8
Thread-1synchronized for loop 1
Thread-0 for loop 9
Thread-1synchronized for loop 2
Thread-0synchronized for loop 0
Thread-1synchronized for loop 3
Thread-0synchronized for loop 1
Thread-1synchronized for loop 4
Thread-0synchronized for loop 2
Thread-1synchronized for loop 5
Thread-0synchronized for loop 3
Thread-1synchronized for loop 6
Thread-0synchronized for loop 4
Thread-1synchronized for loop 7
Thread-1synchronized for loop 8
Thread-0synchronized for loop 5
Thread-1synchronized for loop 9
Thread-0synchronized for loop 6
Thread-0synchronized for loop 7
Thread-0synchronized for loop 8
Thread-0synchronized for loop 9

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!