04 | 09 | 2010
Main Menu
Affiliates
Login Form



Alexa
JoomlaWatch Stats 1.2.6 by Matej Koval
Java Threading and Synchronization Interview Questions
Written by Java King   
Friday, 16 May 2008 16:25

Rating 3.3/5 (12 votes)

 

Q: How do you create threads?

A: There are two ways to create threads. Extend the thread class or Implement the Runnable interface.

Q: How to create and start a Thread by extending the Thread Class?

When we extend the thread class, we have to override the run() method and in that ,write the code to be executed by the thread. To start the thread we have to just call the start() method in the Thread class.

Q: How to create and start a Thread by implementing the Runnable Interface?

When we implement the Runnable interface, we have to provide the implementation for the run() method and ,in that, write the code to be executed by the thread. To start the thread, we have to create an instance of the Thread class, pass the instance of the class implementing Runnable as the argument in the Thread class’s constructor and then call start() in the instance of the thread class.

Q: When do you implement Runnable interface and when do you extend Thread class?

A: When your class is already extending some other class, it is not possible to extend Thread class because in java a class can have only one parent class. In that case we can implement the Runnable interface because in java a class can implement any number of interfaces in addition to extending another class.

Q: Can you create a Thread without using the Thread Class at all?

A: No, because even when we implement Runnable, to create a Thread, we have to create an instance of the Thread class, pass the instance of the class implementing Runnable as the argument in the Thread class’s constructor.

 

Q: What problems can arise when you use multiple threads?

A: When multiple threads are used, a same piece of code can get parallel executed by multiple threads which can result in concurrent modification of shared variables which may give un expected output.

Q: So, how do you solve this problem?

A: Any method or block of code that modifies shared variables has to be made synchronized.

Q: what are the different ways of synchronization?

A: Synchronized method and synchronized block.

Q: How do you make a method synchronized?

A: A method can be made synchronized by using the synchronized keyword in the method declaration.

Q: What happens when multiple threads call a synchronized method?

When a thread enters a synchronized method, the object on which the method is called gets locked by that thread, so no other thread cannot call that or any other synchronized method on the same object.

Q: Do you mean to say that when a thread enters a synchronized method, no other thread can enter the same method or other synchronized methods on that class?

A: No, when a thread enters a synchronized method, only the object on which the method is called gets locked by that thread, but other objects instantiated from the same class are still not locked and methods on those instances can be called.

Q: What happens when I make a static method as synchronized?

A: Static methods are invoked directly on the class and not on object instances of the class, so when a thread enters a synchronized static method, the class itself gets locked by the thread and no other thread can enter any static synchronized methods on that class.

Q: When do you use synchronized blocks?

A: When synchronized method is used, the object on which the method is called gets locked by the thread. But, if you do not want that object to get locked, but you want some instance variable in that object to be locked, you go for synchronized blocks. Moreover, code in synchronized methods and block execute much slower that in non synchronized blocks and methods. Using synchronized blocks we can synchronize only the required portion of a method and thus improve performance.

Comments
Search
Anonymous   |122.181.1.xxx |2009-08-04 05:54:15
please provide with any example for
Q: What happens when multiple threads call
a synchronized method?
Q: What happens when I make a static method as
synchronized?
Only registered users can write comments!

3.22 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 11 April 2009 11:08 )