Java Threading Question and Answers

17. What are the difference between extending the Thread class and implementing the Runnable interface?
 
  • Extending the Thread class will make your class unable to extend other classes, because of the single inheritance feature in Java.
  • However, this will give you a simpler code structures.
  • If you implement runnable, you can gain better object oriented design and consistency and also avoid the single inheritance problems.
 
Your Name Your Email-ID
Your Answer
18. What is Executors class?
 
  • Executors class provide utility methods for Executor, ExecutorService, ScheduleExecutorService, ThreadFactory and Collable classes.
  • Executors class can be used to easily create ThreadPool in java, also this is the only class supporting execution of callable implementations.
 
Your Name Your Email-ID
Your Answer
19. What are the uses of synchronized keyword?
 
  • The keyword synchronized is used to acquire a exclusive monitor lock on an object.
  • It can be used to mark either a method or a block of code.
  • In both cases, it means to acquire a lock for the duration of the method, or block and to release the lock at the end.
  • It also takes a parameter, which names the object, whose lock will be acquired.
 
Your Name Your Email-ID
Your Answer
20. How does multi threading take place on a computer with a single CPU?
  The operating system's task scheduler allocates execution time to multiple tasks. By quickly between executing tasks, it creates the impression that tasks execute sequentially.
 
Your Name Your Email-ID
Your Answer
1234567 Page 5 of 7