Java Threading Question and Answers

13. What is the relationship between a synchronized and volatile keyword?
 
  • The JVM is guaranteed to treat reads and writes of data of 32 bits or less as atomic.
  • For long or double variable, programmers should take care in multithreading environment.
  • Either put these variables in a synchronized method or block, or declare them volatile.
 
Your Name Your Email-ID
Your Answer
14. What are the ways in which a thread can enter the waiting state?
 
  • A thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an object's lock or by involving an object's wait() method.
  • It can also enter the waiting state by invoking its (deprecated) suspend() method.
 
Your Name Your Email-ID
Your Answer
15. What invokes a thread’s run() method?
  After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.
 
Your Name Your Email-ID
Your Answer
16. What are the different ways of creating thread?
  Implementing Runnable: The Runnable interface defines a single method, run, meant to contain the code executed in the thread.
Subclass Thread: The Thread class itself implements Runnable interface, though it runs method does nothing.
 
Your Name Your Email-ID
Your Answer
1234567 Page 4 of 7