Java Multithreading Question and Answers

33. What are monitors?
 
  • Monitor is a body of code which can be executed by only one thread at a time.
  • If any other thread tries to get access at the same time, it will be suspended until the current thread releases the Monitor.
 
Your Name Your Email-ID
Your Answer
34. What is thread safety and synchronization?
 
  • Thread safe is used to describe that can run safely in multithreaded environments allowing accessing to data in a safe and efficient way.
  • Thread safety is achieved by synchronization.
  • Synchronization assures that the object data is not accessed by multiple threads at the same time.
 
Your Name Your Email-ID
Your Answer
35. What is the difference between thread and process?
 
  • A thread is a path of execution that run on CPU, a process is a collection of threads that share the same virtual memory. A process has at least one thread of execution and a thread always run in a process context.
  • A process is a collection of virtual memory space, code, data and system resources.
  • A thread is code that is to be serially executed within a process.
  • A processor executes threads, not processes.
  • One more significant difference between process and thread is every process has its own data memory location but all related threads can share same data memory and have their own individual stacks. Thread is a light weighted process, collection of threads become process.
 
Your Name Your Email-ID
Your Answer
36. How do we create threads?
  There are two ways to create a thread.
  • extend the java.lang.Thread clas: Create a class which extends the thread class. When the class is instantiated, the thread and object are created together and the object is automatically bound to the thread. By calling the object's start() method, the thread is started and immediately calls the object's run() method.
  • Implement the java.lang.Runnable interface: Create a thread and supply it an object with a run() method. This object will be permanently associated with the thread. The object's run method will be invoked when the thread is started. This method of thread creation is useful if you want many threads sharing an object.
 
Your Name Your Email-ID
Your Answer
12345678910 Page 9 of 10