Interview Questions

How can you say Thread behaviour is unpredictable?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

6. How can you say Thread behaviour is unpredictable?

The solution to question is quite simple, Thread behaviour is unpredictable because execution of Threads depends on Thread scheduler, thread scheduler may have different implementation on different platforms like windows, unix etc. Same threading program may produce different output in subsequent executions even on same platform.

To achieve we are going to create 2 threads on same Runnable Object, create for loop in run() method and start both threads. There is no surety that which threads will complete first, both threads will enter anonymously in for loop.

(Continued on next question...)

Other Interview Questions