0 0

Runnable问题一个问题5

由于平时开发中很少用到多线程,可是最近面试总是问多线程,我答不上来,很是尴尬,自己看了这点方面的知识,有个问题请教下大家:直接上代码了
代码:
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跑。
不知道我的理解对不对。
 
2014年5月24日 22:42

2个答案 按时间排序 按投票排序

0 0

采纳的答案

楼主,关于sleep方法,会导致线程睡眠,但是只会让出CPU的时间片,不会让出当前线程的锁的。
另外,你创建的是两个Runnable的对象,在类内部

synchronized(this) {
  ....
}
//这其中的this即当前使用的对象,在两个不同对象执行同一个方法时,不会达到互斥效果的。


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

2014年5月25日 09:59
0 0

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

2014年5月25日 00:41

相关推荐

    java多线程Runnable实例

    `Runnable`接口是Java提供的一个核心机制,用于创建并运行线程。本实例将深入讲解如何使用`Runnable`接口来实现多线程,并通过具体的`TestRunnable.java`源代码进行演示。 一、`Runnable`接口简介 在Java中,`...

    android handler runnable使用实例(关键是内部run中停止)

    `Runnable`是一个接口,它定义了一个单个方法`run()`. 任何实现了Runnable接口的类都可以创建一个Runnable对象,然后将这个对象传递给Handler的`post()`或`sendMessage()`方法,使得`run()`方法能在Handler的关联...

    implements Runnable

    "implements Runnable"是Java编程语言中的一个重要概念,它与多线程编程紧密相关。在Java中,线程是程序执行的最小单位,而创建线程主要有两种方式:继承Thread类和实现Runnable接口。本项目是一个Java小游戏,核心...

    android demo,使用Runnable和Handler的特性实现每个3s的定时器

    本示例的`android demo`旨在演示如何利用`Runnable`和`Handler`来实现一个每3秒执行一次的任务,这在需要定期更新UI或执行周期性操作的场景下非常有用。现在我们将详细探讨这两个组件以及它们如何协同工作。 首先,...

    用Runnable创建线程比较--java

    `Runnable`是Java提供的一个接口,其中只有一个抽象方法`run()`. 当你创建一个类实现这个接口时,你需要覆盖`run()`方法,这个方法包含你希望在线程中执行的代码。然后,你可以将这个`Runnable`对象传递给`Thread`类...

    实现Runnable接口的小程序

    在Java的`java.lang`包中,Runnable接口定义了一个方法:`void run()`。任何类实现这个接口并重写`run()`方法,都可以成为一个可执行的任务。当这个任务被放入Thread对象中并启动时,`run()`方法会被执行。 创建一...

    实现Runnable接口创建多线程.docx

    由于Java的单继承特性,当一个类需要继承其他类时,就不能再直接继承`Thread`,这时实现`Runnable`接口就成为创建线程的优选方案。本篇将深入探讨如何通过实现`Runnable`接口来创建多线程,以及如何使用`Thread`类的...

    Java中的Runnable,Callable,Future,FutureTask的比较

    例如,如果一个任务需要计算一个复杂的数学问题,Callable可以返回计算结果,而Runnable则无法做到这一点。`call()`方法的返回类型由泛型V指定,可以是任何Java类型。 3. **Future**: Future接口是用于异步计算的...

    线程Runnable的实现

    在一个线程中求100以内的偶数,求出一个偶数后休眠一个随机时间在(1-300毫秒之间).在另一个线程中求奇数,求出一个奇数后也休眠一个随机时间(1-300毫秒之间).输出数据时应有提示,指明是哪个线程输出的数据 用实例...

    线程类实现Runnable接口

    声明自定义线程类实现Runnable接口实现奇数/偶数序列线程并发执行。

    多线程的Runnable_和thread有什么区别

    - 例如,在售票系统中,多个线程可以共享同一个`Runnable`对象,从而共享相同的票务数据。 #### 四、总结 通过对比两种实现方式,我们可以发现它们各有优势。选择哪种方式取决于具体的应用场景和个人喜好。通常来...

    Android 多线程下载简单demo之Runnable

    本篇将基于`Runnable`接口提供一个简单的多线程下载示例,帮助开发者理解如何在Android中实现这一功能。 首先,我们要明白在Android中,主线程(UI线程)负责处理用户交互和更新界面,因此,长时间的网络请求或计算...

    Java中Runnable和Thread的区别

    - **缺点**:Java的类继承是单继承的,这意味着如果一个类已经继承了另一个类,它就不能再继承`Thread`类。此外,创建过多的`Thread`子类可能会导致类结构复杂化。 ### 实现Runnable接口 #### 实现方式 实现`...

    使用Runnable模拟Callable接口实现线程有返回值

    在此我给出一个Demo,看看通过Runnable接口如何实现Callable接口的有返回值。这个例子并不摘自JDK中Callable接口源码,只是提供一种可行方案,仅供参考!该Demo解压、导入到开发环境即可,测试入口为test包下的...

    thread 线程类 实现runnable接口

    isEquals() 方法可以比较两个线程是否相同。 8. 线程的实现 Thread 类的实现使用了 pthread 库,它提供了线程的基本操作和管理。Thread 类的构造函数会初始化线程的状态、线程 ID、线程属性等信息。 Thread 线程...

    autosar swc runnable

    Autosar SWC在Simulink中设置多个runnable的测试模型及生成的代码及Arxml文件

    JAVA中使用Thread类和Runnable接口创建多线程

    在Java中创建线程有两种方法:使用Thread类和使用Runnable接口。在使用Runnable接口时需要建立一个Thread实例。因此,无论是通过Thread类还是Runnable接口建立线程,都必须建立Thread类或它的子类的实例。

    synchronized 实现多线程 implements Runnable

    一个简单的多线程代码示例,Java实现,用于实现同一时刻,只允许一个线程调用执行的代码块或类,即synchronized的如何使用(多线程实现),实现 Runnable

    Eclipse编写的Android数字时钟应用实例(用新建Runnable的方法)

    `Runnable`是Java多线程编程的核心组件,它允许我们定义一个可执行的任务,然后在不同的线程上运行。 首先,我们需要创建一个新的Android项目,并在`main.xml`布局文件中添加一个TextView,用于显示数字时钟的时间...

    java Runnable接口创建线程

    1. 创建一个实现Runnable接口的类。 2. 在该类中重写run()方法,以便在线程中执行的操作。 3. 在main方法中创建一个线程对象,并将Runnable对象传递给它。 4. 调用start()方法以启动线程。 Runnable接口的优点 ...

Global site tag (gtag.js) - Google Analytics