简体   繁体   English

scjp:与线程相关的问题

[英]scjp: threads related issue

below is the question... 以下是问题...

void waitForSignal() {
     Object obj = new Object();
     synchronized (Thread.currentThread()) {
         obj.wait();
         obj.notify();
     }
 }

Which statement is true? 哪个论述是对的?

A. This code can throw an InterruptedException . 答:此代码可以引发InterruptedException

B. This code can throw an IllegalMonitorStateException . B.此代码可以引发IllegalMonitorStateException

C. This code can throw a TimeoutException after ten minutes. C.此代码可以在十分钟后引发TimeoutException

D. Reversing the order of obj.wait() and obj.notify() might cause this method to complete normally. D.颠倒obj.wait()obj.notify()的顺序可能会导致此方法正常完成。

E. A call to notify() or notifyAll() from another thread might cause this method to complete normally. E.从另一个线程调用notify()notifyAll()可能导致此方法正常完成。

F. This code does NOT compile unless obj.wait() is replaced with ((Thread) obj).wait() . F.除非将obj.wait()替换为((Thread) obj).wait()否则不会编译此代码。

Answer is B . 答案是B。 But when i have executed in below manner getting A. Please help. 但是,当我以以下方式执行A时,请帮助。

public class ThreadStateProblem extends Thread {  
    public void run() {  

    }  

    void waitForSignal() {
        Object obj = new Object();
        synchronized (Thread.currentThread()) {
            obj.wait();
            obj.notify();
        }
    }

    public static void main(String []s) {  
        new ThreadStateProblem().start();  
        new ThreadStateProblem().waitForSignal();
    }    
}   

And also tried below : 并且还尝试了以下方法:

public class ThreadStateProblem extends Thread {  
    public void run() {  

    }  

    void waitForSignal() {
        Object obj = new Object();
        synchronized (Thread.currentThread()) {
            obj.wait();
            obj.notify();
        }
    }

    public static void main(String []s) {  
        ThreadStateProblem sd =new ThreadStateProblem();
        sd.start();
        sd.waitForSignal();
    }    
}  

This code doesn't compile. 此代码无法编译。 Answer A means the code would throw InterruptedException while running. 答案A表示代码在运行时将抛出InterruptedException。 Your code has a compile error where obj.wait can throw InterruptedException and the exception isn't handled and the method is not declared as throwing it, so the class can't compile. 您的代码有一个编译错误,其中obj.wait可以抛出InterruptedException,并且不处理该异常,并且该方法未声明为抛出该异常,因此该类无法编译。 So Answer A is not applicable here. 因此答案A在这里不适用。

Of course the code from the question has the same problem. 当然,问题中的代码也有同样的问题。 So the question seems flawed. 所以这个问题似乎有缺陷。

Once you fix the compile error, and move the waitForSignal call into the Thread's run method so that it gets called by the sd thread instead of the main thread, you should have something like: 一旦解决了编译错误,并将waitForSignal调用移至Thread的run方法中,以便它被sd线程而不是主线程调用,则应具有以下内容:

public class ThreadStateProblem extends Thread {  
    public void run() {  
        waitForSignal();
    }  

    void waitForSignal() {
        Object obj = new Object();
        synchronized (Thread.currentThread()) {
            try {
                obj.wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                e.printStackTrace();
            }
            obj.notify();
        }
    }

    public static void main(String []s) {  
        ThreadStateProblem sd =new ThreadStateProblem();
        sd.start();
    }    
} 

then you should get the expected result: 那么您应该得到预期的结果:

Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at java.lang.Object.wait(Object.java:503)
    at ThreadStateProblem.waitForSignal(ThreadStateProblem.java:10)
    at ThreadStateProblem.run(ThreadStateProblem.java:3)

This will compile only if its surrounded with try and catch now coming on the explanation 仅当其周围有try and catch时才进行编译

-If the thread calling wait() does not own the lock on the object, a IllegalMonitorStateException will be thrown. -如果调用wait()的线程不拥有该对象的锁,则将引发IllegalMonitorStateException

-Here we haven't take lock for obj we are taking lock for current thread object. -这里我们没有为obj锁定,而是为当前thread对象锁定。

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

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