Java Threading Questions and Answers

1. 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
2. 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
3. What are daemon threads?
  Daemon threads are designed run in the background. One example of a daemon thread is the garbage collector thread. You can use setDaemon () to mark a thread as daemon.
 
Your Name Your Email-ID
Your Answer
4. What is semaphore?
  Semaphore is an object which helps two threads to communicate with one another in order to synchronize there operation.
 
Your Name Your Email-ID
Your Answer
1234567 Page 1 of 7