Java Interview Question And Answer | Java Interview Question And Answer For Fresher | Core Java Interview Question And Answare | Set 10

1.What methods java providing for Thread communications ?
 Ans: Java provides three methods that threads can use to communicate with each other: wait, notify, and notifyAll.

2.Can Java object be locked down for exclusive use by a given thread?
 Ans: Yes. You can lock an object by putting it in a "synchronized" block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.

3.Can each Java object keep track of all the threads that want to exclusively access to it?
 Ans: Yes. Use Thread.currentThread() method to track the accessing thread.

4.Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
 Ans: Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first.

5.What invokes a thread's run() method?
 Ans: After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.

6.What are three ways in which a thread can enter the waiting state?
 Ans: A thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an object's lock, or by invoking an object's wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.

7.What's new with the stop(), suspend() and resume() methods in JDK 1.2?
 Ans: The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

8.What is synchronization and why is it important?
 Ans: With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often causes dirty data and leads to significant errors.

9.What is synchronized keyword? In what situations you will Use it?


 Ans: Synchronization is the act of serializing access to critical sections of code. We will use this keyword when we expect multiple threads to access/modify the same data. To understand synchronization we need to look into thread execution manner.

          Threads may execute in a manner where their paths of execution are completely independent of each other. Neither thread depends upon the other for assistance. For example, one thread might execute a print job, while a second thread repaints a window. And then there are threads that require synchronization, the act of serializing access to critical sections of code, at various moments during their executions. For example, say that two threads need to send data packets over a single network connection. Each thread must be able to send its entire data packet before the other thread starts sending its data packet; otherwise, the data is scrambled. This scenario requires each thread to synchronize its access to the code that does the actual data-packet sending. If you feel a method is very critical for business that needs to be executed by only one thread at a time (to prevent data loss or corruption), then we need to use synchronized keyword.

10.Name Component subclasses that support painting ?
 Ans: The Canvas, Frame, Panel, and Applet classes support painting.


Previous Next


:: Click the links below for similar type Questions and answers ::

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | coupon codes