Explanation: this wont throw any runtime as well as compile time errors. here the methods are private and calling those methods from main methods which resides in the same class.
So the output becomes
bookbook2
What becomes the output of below code snippet?
Output: Compilation Error!!!
Unhandled exception type InterruptedException
Explanation: If Thread.sleep() called, then the user should needs to throw the InterruptedException from the method else compilation error happens
What becomes the output of below code snippet?
Output: bookbook2
Explanation:
Here we handled the InterruptedException so the code become run on the right way
What become the output of follwing program ?
Output:
Method going to end
run
java.lang.RuntimeException: Problem
or
run.
java.lang.RuntimeException: Problem
End of method.
Explanation:
The exception happens on one thread wont affect other thread
the method wait handled with InterruptedException. else getting compilation error Unhandled exception type InterruptedException
the wait and notify writtern inside the synchronized block. else getting runtime error java.lang.IllegalMonitorStateException
What becomes the output of following program?
Output: runningrunningrunning
Explanation: Here the answer is simple while calling t.run(). then the system should act like normal method calling. t.start() only will create a new thread instance
Lets change above program bit more. Check what becomes the output of following program?
Output
running
running
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:708)
at com.ocjp.ThreadCheck.main(ThreadCheck.java:12)
Explanation: because while doing first start the thread is started. while doing the second start method the system again tries to create new thread hence exception happens.
What becomes the output of following program?
Output: Here the system throws compilation error obj.wait() at Unhandled exception type InterruptedException
What becomes the output of following program?
Output:
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at com.ocjp.ThreadCheck.waitForSignal(ThreadCheck.java:13)
at com.ocjp.ThreadCheck.run(ThreadCheck.java:5)
at java.lang.Thread.run(Thread.java:748)
at com.ocjp.ThreadCheck.main(ThreadCheck.java:23)
Explanation: an IllegalMonitorStateException can occur when a thread attempts to wait on an object’s monitor, or to notify other threads waiting for said object’s monitor, when that thread does not own the monitor in question. Put another way, if the Object.wait() method is called on an object which was not created by the current thread, an IllegalMonitorStateException will be thrown.
I have over 7+ years of experience as a technical lead and have worked at both small startups and large organizations. While I'm a proficient full-stack developer, my expertise is in building scalable backend micro-services (API, Services, Data processing, async processing, multi-threaded services and data analysing) and build the client side UI (single page angular/react applications, Multiplatform rendering using bootstrap or material).
No comments:
Post a Comment