20 April 2011

interview question: java threads



Topic:  JAVA Thread
Faculty Name:Viji.V
(Q)  What is threaded programming and when is it used?
Ans:  Threaded programming is normally used when a program is required to do more than one task at the same time. Threading is often used in applications with graphical user interfaces; a new thread may be created to do some processor-intensive work while the main thread keeps the interface responsive to human interaction.

The Java programming language has threaded programming facilities built in, so it is relatively easy to create threaded programs. However, multi-threaded programs introduce a degree of complexity that is not justified for most simple command line applications.

http://www.javabeginner.com/learn-java/java-threads-tutorial

http://java.sun.com/developer/Books/javaprogramming/threads/

http://www.roseindia.net/java/beginners/Threads.shtml

Faculty Name:Soji Joy

Q) What Is Synchronization In Java?

Ans:The synchronized key word is used to avoid deadlock condition. If two threads are trying to access the same method, only one thread is allowed into the method. The second thread has to wait until the first thread completes the execution of the Synchronized method.

Reference:

http://www.herongyang.com/Java/Synchronization-Support-in-Java-synchronized.html

Faculty Name:Arun Pallisseri

Q)Why are wait(), notify() and notifyall() methods defined in the Object class?

Ans:These methods are detailed on the Java Software Development Kit JavaDoc page for the Object class, they are to implement threaded programming for all subclasses of Object.

References:

http://www.indiabix.com/technical/core-java/

Faculty Name:Arun Pallisseri

Q)Why not override Thread to make a Runnable?

Ans:There is little difference in the work required to override the Thread class compared with implementing theRunnable interface, both require the body of the run() method. However, it is much simpler to make an existing classhierarchy runnable because any class can be adapted to implement the run() method. A subclass of Thread cannot extend any other type, so application-specific code would have to be added to it rather than inherited.

References:

http://www.indiabix.com/technical/core-java/

Faculty Name:Saritha G Pillai

Q)What is the difference between Thread and a Process?

Ans:Threads run inside process and they share data.One process can have multiple threads, if the process is killed all the threads inside it are killed, they dont share data.

References:

http://www.javabeginner.com/learn-java/java-threads-tutorial

http://www.roseindia.net/java/beginners/Threads.shtml

http://java.sun.com/developer/Books/javaprogramming/threads/

1 comment:

  1. from How Synchronization works in Java
    Synchronized keyword in Java is used to provide mutual exclusive access of a shared resource with multiple threads in Java. Synchronization in java guarantees that no two threads can execute a synchronized method which requires same lock simultaneously or concurrently.

    ReplyDelete