第二章,第一个小案例,顺序混乱

来源:2-5 Java线程-隋唐演义实战开发---关键先生

wangxinyuan

2016-04-10 16:05

java代码:

package com.test.thread;

public class Actor extends Thread {
    volatile boolean keepRunning = true;
    public void run(){
        System.out.println(getName()+"是一个演员!");
        int count = 0;
    
        while(keepRunning){
            System.out.println(getName()+"登台演出:"+ (++count));
            
            if(count == 100){
                keepRunning = false;
            }
            
            if(count%10== 0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(getName()+"的演出结束了!");
    }
    
    
    public static void main(String[] args){
        Thread actor = new Actor();
        actor.setName("Mr. Thread");
        
        actor.start();
        
        Thread actressThread = new Thread(new Actress(),"Ms. Runnable");
        actressThread.start();
    }

}


class Actress implements Runnable{
    volatile boolean keepRunning = true;
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"是一个演员!");
        int count = 0;
        
    
        while(keepRunning){
            System.out.println(Thread.currentThread().getName()+"登台演出:"+ (++count));
            
            if(count == 100){
                keepRunning = false;
            }
            
            if(count%10== 0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(Thread.currentThread().getName()+"的演出结束了!");
        
    }
    
}

执行效果:

Mr. Thread是一个演员!
Ms. Runnable是一个演员!
Mr. Thread登台演出:1
Mr. Thread登台演出:2
Mr. Thread登台演出:3
Mr. Thread登台演出:4
Ms. Runnable登台演出:1
Mr. Thread登台演出:5
Mr. Thread登台演出:6
Ms. Runnable登台演出:2
Mr. Thread登台演出:7
Mr. Thread登台演出:8
Ms. Runnable登台演出:3
Mr. Thread登台演出:9
Mr. Thread登台演出:10
Ms. Runnable登台演出:4
Ms. Runnable登台演出:5
Ms. Runnable登台演出:6
Ms. Runnable登台演出:7
Ms. Runnable登台演出:8
Ms. Runnable登台演出:9
Ms. Runnable登台演出:10
Ms. Runnable登台演出:11
Ms. Runnable登台演出:12
Ms. Runnable登台演出:13
Ms. Runnable登台演出:14
Ms. Runnable登台演出:15
Mr. Thread登台演出:11
Ms. Runnable登台演出:16
Mr. Thread登台演出:12
Ms. Runnable登台演出:17
Mr. Thread登台演出:13
Mr. Thread登台演出:14
Ms. Runnable登台演出:18
Mr. Thread登台演出:15
Mr. Thread登台演出:16
Ms. Runnable登台演出:19
Ms. Runnable登台演出:20
Mr. Thread登台演出:17
Mr. Thread登台演出:18
Mr. Thread登台演出:19
Mr. Thread登台演出:20
Mr. Thread登台演出:21
Ms. Runnable登台演出:21
Mr. Thread登台演出:22
Ms. Runnable登台演出:22
Mr. Thread登台演出:23
Ms. Runnable登台演出:23
Ms. Runnable登台演出:24
Ms. Runnable登台演出:25
Mr. Thread登台演出:24
Ms. Runnable登台演出:26
Mr. Thread登台演出:25
Mr. Thread登台演出:26
Mr. Thread登台演出:27
Mr. Thread登台演出:28
Mr. Thread登台演出:29
Mr. Thread登台演出:30
Ms. Runnable登台演出:27
Ms. Runnable登台演出:28
Ms. Runnable登台演出:29
Ms. Runnable登台演出:30
Ms. Runnable登台演出:31
Mr. Thread登台演出:31
Ms. Runnable登台演出:32
Mr. Thread登台演出:32
Ms. Runnable登台演出:33
Mr. Thread登台演出:33
Ms. Runnable登台演出:34
Mr. Thread登台演出:34
Ms. Runnable登台演出:35
Mr. Thread登台演出:35
Mr. Thread登台演出:36
Mr. Thread登台演出:37
Mr. Thread登台演出:38
Mr. Thread登台演出:39
Mr. Thread登台演出:40
Ms. Runnable登台演出:36
Ms. Runnable登台演出:37
Ms. Runnable登台演出:38
Ms. Runnable登台演出:39
Ms. Runnable登台演出:40
Ms. Runnable登台演出:41
Mr. Thread登台演出:41
Ms. Runnable登台演出:42
Mr. Thread登台演出:42
Ms. Runnable登台演出:43
Mr. Thread登台演出:43
Ms. Runnable登台演出:44
Ms. Runnable登台演出:45
Mr. Thread登台演出:44
Mr. Thread登台演出:45
Mr. Thread登台演出:46
Mr. Thread登台演出:47
Ms. Runnable登台演出:46
Mr. Thread登台演出:48
Ms. Runnable登台演出:47
Mr. Thread登台演出:49
Ms. Runnable登台演出:48
Ms. Runnable登台演出:49
Ms. Runnable登台演出:50
Mr. Thread登台演出:50
Ms. Runnable登台演出:51
Mr. Thread登台演出:51
Ms. Runnable登台演出:52
Mr. Thread登台演出:52
Ms. Runnable登台演出:53
Mr. Thread登台演出:53
Mr. Thread登台演出:54
Mr. Thread登台演出:55
Ms. Runnable登台演出:54
Mr. Thread登台演出:56
Mr. Thread登台演出:57
Ms. Runnable登台演出:55
Ms. Runnable登台演出:56
Ms. Runnable登台演出:57
Ms. Runnable登台演出:58
Ms. Runnable登台演出:59
Ms. Runnable登台演出:60
Mr. Thread登台演出:58
Mr. Thread登台演出:59
Mr. Thread登台演出:60
Mr. Thread登台演出:61
Mr. Thread登台演出:62
Ms. Runnable登台演出:61
Mr. Thread登台演出:63
Mr. Thread登台演出:64
Mr. Thread登台演出:65
Mr. Thread登台演出:66
Mr. Thread登台演出:67
Mr. Thread登台演出:68
Mr. Thread登台演出:69
Mr. Thread登台演出:70
Ms. Runnable登台演出:62
Ms. Runnable登台演出:63
Ms. Runnable登台演出:64
Ms. Runnable登台演出:65
Ms. Runnable登台演出:66
Ms. Runnable登台演出:67
Ms. Runnable登台演出:68
Ms. Runnable登台演出:69
Ms. Runnable登台演出:70
Mr. Thread登台演出:71
Mr. Thread登台演出:72
Ms. Runnable登台演出:71
Mr. Thread登台演出:73
Ms. Runnable登台演出:72
Mr. Thread登台演出:74
Ms. Runnable登台演出:73
Ms. Runnable登台演出:74
Ms. Runnable登台演出:75
Ms. Runnable登台演出:76
Mr. Thread登台演出:75
Mr. Thread登台演出:76
Mr. Thread登台演出:77
Ms. Runnable登台演出:77
Ms. Runnable登台演出:78
Ms. Runnable登台演出:79
Ms. Runnable登台演出:80
Mr. Thread登台演出:78
Mr. Thread登台演出:79
Mr. Thread登台演出:80
Ms. Runnable登台演出:81
Mr. Thread登台演出:81
Ms. Runnable登台演出:82
Mr. Thread登台演出:82
Ms. Runnable登台演出:83
Mr. Thread登台演出:83
Ms. Runnable登台演出:84
Mr. Thread登台演出:84
Ms. Runnable登台演出:85
Mr. Thread登台演出:85
Mr. Thread登台演出:86
Ms. Runnable登台演出:86
Ms. Runnable登台演出:87
Ms. Runnable登台演出:88
Ms. Runnable登台演出:89
Ms. Runnable登台演出:90
Mr. Thread登台演出:87
Mr. Thread登台演出:88
Mr. Thread登台演出:89
Mr. Thread登台演出:90
Ms. Runnable登台演出:91
Ms. Runnable登台演出:92
Mr. Thread登台演出:91
Ms. Runnable登台演出:93
Mr. Thread登台演出:92
Ms. Runnable登台演出:94
Ms. Runnable登台演出:95
Mr. Thread登台演出:93
Ms. Runnable登台演出:96
Mr. Thread登台演出:94
Mr. Thread登台演出:95
Ms. Runnable登台演出:97
Mr. Thread登台演出:96
Ms. Runnable登台演出:98
Mr. Thread登台演出:97
Mr. Thread登台演出:98
Ms. Runnable登台演出:99
Mr. Thread登台演出:99
Mr. Thread登台演出:100
Ms. Runnable登台演出:100
Ms. Runnable的演出结束了!
Mr. Thread的演出结束了!

写回答 关注

2回答

  • 蒲公英shine
    2016-04-10 17:19:22
    已采纳

    是由于volatile的原因。。但是具体的我也不懂

    wangxi...

    volatile我用了还是不好使。

    2016-04-10 21:25:42

    共 1 条回复 >

  • 蒲公英shine
    2016-04-12 15:23:18

    我并不是特别懂volatile,我也不懂为什么加上它顺序就很混乱。

深入浅出Java多线程

带你一起深入浅出多线程,掌握基础,展望进阶路线

186088 学习 · 464 问题

查看课程

相似问题