http://www.studytonight.com/java/thread-class-and-functions.php Q1) If a class has a synchronised method and non-synchronised method, can multiple threads execute the non-synchronised methods?
Ans) Yes. If a class has a synchronised and
non-synchronised methods, multiple threads can access the
non-synchronised methods.
|
Q2) If a thread goes to sleep does it hold the lock?
Ans) Yes when a thread goes to sleep it does not release the lock.
|
Q3)Can a thread hold multiple locks at the same time?
Ans) Yes. A thread can hold multiple locks at
the same time. Once a thread acquires a lock and enters into the
synchronized method / block, it may call another synchronized method
and acquire a lock on another object.
|
Q4) Can a thread call multiple synchronized methods on the object of which it hold the lock?
Ans) Yes. Once a thread acquires a lock in
some object, it may call any other synchronized method of that same
object using the lock that it already holds.
|
Q5) Can static methods be synchronized?
Ans) Yes. As static methods are class methods
and have only one copy of static data for the class, only one lock for
the entire class is required. Every class in java is represented by
java.lang.Class instance. The lock on this instance is used to
synchronize the static methods.
|
Q6) Can two threads call two different static synchronized methods of the same class?
Ans) No. The static synchronized methods of the same class always block each other as only one lock per class exists.
So no two static synchronized methods can execute at the same time.
|
Q7)Does a static synchronized method block a non-static synchronized method?
Ans)No As the thread executing the static
synchronized method holds a lock on the class and the thread executing
the non-satic synchronized method holds the lock on the object on which
the method has been called, these two locks are different and these
threads do not block each other.
|
Q8) Once a thread has been started can it be started again?
Ans) No. Only a thread can be started only once
in its lifetime.
If you try starting a thread which has been already started once
an IllegalThreadStateException is thrown, which is a runtime exception.
A thread in runnable state or a dead thread can not be restarted.
|
Q9) When does deadlock occur and how to avoid it?
Ans) When a locked object tries to access a locked object which is trying to access the first locked object.
When the threads are waiting for each other to release the lock on a particular object, deadlock occurs .
|
Q10) What is a better way of creating multithreaded application? Extending Thread class or implementing Runnable?
Ans) If a class is made to extend the thread
class to have a multithreaded application then this subclass of Thread
can not extend any other class and the required application will have to
be added to this class as it can not be inherited from any other class.
If a class is made to implement Runnable interface, then the class
can extend other class or implement other interface.
|
Q11) Can the start() method of the Thread class be overridden? If yes should it be overridden?
Ans) Yes the start() method can be overridden.
But it should not be overridden as it’s implementation in thread class
has the code to create a new executable thread and is specialised.
|
Q12) What are the methods of the thread class used to schedule the threads?
Ans) The methods are as follows:
|
Q13) Which thread related methods are available in Object class?
Ans) The methods are:
|
Q14) Which thread related methods are available in Thread class?
Ans) Methods which are mainly used :
|
Q15) List the methods which when called the thread does not release the locks held?
Ans) Following are the methods.
|
Q16) List the methods which when called on the object the thread releases the locks held on that object?
Ans) wait()
|
Q17) Does each thread has its own thread stack?
Ans) Yes each thread has its own call stack. For eg
Thread t1 = new Thread();
In the above example t1 and t3 will have the same stack and t2 will have its own independent stack.
Thread t2 = new Thread(); Thread t3 = t1; |
Q18) What is thread starvation?
Ans) In a multi-threaded environment thread
starvation occurs if a low priority thread is not able to run or get a
lock on the resoruce because of presence of many high priority threads.
This is mainly possible by setting thread priorities inappropriately.
|
Q19) What is threadLocal variable?
Ans) ThreadLocal is a class. If a variable is
declared as threadLocal then each thread will have a its own copy of
variable and would not interfere with the other's thread copy. Typical
scenario to use this would be giving JDBc connection to each thread so
that there is no conflict.
ThreadLocal class by JAVA API
public class ThreadLocal { public Object get(); public void set(Object newValue); public Object initialValue(); }
Implementation of ThreadLocal
public class ConnectionDispenser { private static class ThreadLocalConnection extends ThreadLocal { public Object initialValue() { return DriverManager.getConnection(ConfigurationSingleton.getDbUrl()); } } private static ThreadLocalConnection conn = new ThreadLocalConnection(); public static Connection getConnection() { return (Connection) conn.get(); } } |
Thursday, 11 July 2013
Thread Interview Question-2
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment