简体   繁体   English

SCJP线程章节问题

[英]SCJP Thread Chapter issue

I am having problem to understand the following program in Chapter 9(threads) of SCJP book K&B 我在理解SCJP书K&B的第9章(线程)中的以下程序时遇到问题

QUESTION: 题:

class Dudes{  
    static long flag = 0;
    // insert code here  
    void chat(long id){  
        if(flag == 0)  
            flag = id;  
        for( int x = 1; x < 3; x++){  
            if( flag == id)  
                System.out.print("yo ");  
            else  
                System.out.print("dude ");  
        }  
    }  
}  
public class DudesChat implements Runnable{  
    static Dudes d;  
    public static void main( String[] args){  
        new DudesChat().go();     
    }  
    void go(){  
        d = new Dudes();  
        new Thread( new DudesChat()).start();  
        new Thread( new DudesChat()).start();  

    }  
    public void run(){  
        d.chat(Thread.currentThread().getId());  
    }  
} 

And given these two fragments: 并给出以下两个片段:

I. synchronized void chat (long id){  
             II. void chat(long id){  

OPTIONS: 选项:

When fragment I or fragment II is inserted at line 5, which are true? (Choose all that apply.) 
A. An exception is thrown at runtime 
B. With fragment I, compilation fails 
C. With fragment II, compilation fails 
D. With fragment I, the ouput could be yo dude dude yo 
E. With fragment I, the output could be dude dude yo yo 
F. With fragment II, the output could be yo dude dude yo 

The Official Answer is F(But I cannot understand why, It would be really grateful if someone could explain me the concept) 官方的答案是F(但我不明白为什么,如果有人能向我解释这个概念,将不胜感激)

Consider the following scenario: 请考虑以下情形:

Thread 1 ID : 1 线程1 ID:1
Thread 2 ID : 2 线程2 ID:2
Following steps would take place: 将执行以下步骤:
Thread 1 gets CPU cycle and executes chat(1) 线程1获得CPU周期并执行chat(1)
flag=1 标志= 1
x = 1 : since flag == 1 so yo is printed x = 1:由于标志== 1,所以会打印yo
Thread 1 is preempted by Thread 2 线程1被线程2抢占
Thread 2 gets CPU cycle and executes chat(2) 线程2获得CPU周期并执行chat(2)
flag = 1 (NOT 2 because flag==0 condition fails) 标志= 1(不是2,因为flag==0条件失败)
x = 1 : since flag!=2 so dude will be printed x = 1:由于标志!= 2,所以将打印dude
x = 2 : since flag!=2 so dude will be printed x = 2:由于标志!= 2,所以将打印dude
Thread 1 gets the CPU cycle 线程1获得CPU周期
flag = 1 标志= 1
x = 2 : since flag == 1 so yo will be printed. x = 2:由于标志== 1,所以将打印yo


Hence the output is `yo dude dude yo`

if chat will be synchronized, output will be 如果聊天将同步,则输出为

yo yo dude dude 

There is one Object of Dudes, it is declared as static; 有一个Dudes对象,被声明为静态对象; We have one object, 2 threads and 1 synchronized method; 我们有一个对象,2个线程和1个同步方法; if chat method will be synchronized, two threads can't access class synchronized method together. 如果chat方法将被同步,则两个线程无法一起访问类同步方法。

if chat will not be synchronized, you will not detect answer (there will not be same answers, because two threads are invoked together so state is changing during the processes of each thread; 如果聊天不同步,您将不会检测到答案(不会有相同的答案,因为两个线程被一起调用,所以每个线程的处理过程中状态都在变化;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM