Subscribe to:
Post Comments (Atom)
Output: bookbook2
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
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
Output: bookbook2
Explanation: Here we handled the InterruptedException so the code become run on the right way
Output:
Method going to end run java.lang.RuntimeException: Problemor
run. java.lang.RuntimeException: Problem End of method.
Explanation: The exception happens on one thread wont affect other thread
Output:
0 Completed 1 Completed 2 Completed 3 Completed 4 Completed 5 Completed 6 Completed 7 Completed 8 Completed 9 Completed
Explanation:
Also here please note the following thing
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
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.
Output: Here the system throws compilation error obj.wait() at Unhandled exception type InterruptedException
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.
No comments:
Post a Comment