Tuesday, November 6, 2007



CORE JAVA faqs2

  1. Why stop and suspend method are deprecated?
    Stop() terminates the thread abruptly while suspend() method blocks a thread until another thread calls resume () on it.
    Stop () method will leave an object in inconsistent state because it immediately gives up locks on all other object that it has locked. For example, during transfer of money from one account to other account if the transfer thread is stopped, the bank account object is damaged. When a thread wants to stop another thread, it doesn’t know when it is safe to stop that thread. So it has been deprecated.
    Suspend () method frequently leads to deadlocks .It can be invoked on a thread externally at any point of time without the threads consent which is not desired. For this reason it has been deprecated.

  2. What is difference between sleep() ,wait()and suspend() of a Thread ?
    Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has acquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt () it will wake up the sleeping thread.
    Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.
    t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptible it is deadlock prone.
    object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is called on a object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.
  3. What is "starvation" when used in the context of the Java threading model?
    · Starvation is when the Java runtime (JVM) doesn't allocate time to a thread to execute. This may be due to a poor scheduling algorithm (like green Threads under Solaris, where a for loop from 1 to 1 million doing something CPU intensive wouldn't yield the CPU under Solaris but would under Windows), poor programming practice (not returning from the paint() method in an applet), or a hostile attack (like hitting a host with a denial of service attack where the CPU is busy outside the Java process).

  4. What is a daemon thread?
    · Daemon is a thread that has only purpose in life is to serve others.
    · When only daemon threads remain, the program exits.

  5. What is the difference between a “Green thread” and “Native thread”

    · Green Threads" are the default threads that are provided in the JDK, while the "Native Threads" are the one provided by the native Operating System.Normally "Native Threads" provide better performance because they are controlled by the kernel of the system, allowing the JVM to better use the resources offered by the system itself.For example, in a multiprocessor system "Green Threads" will never be able to use Solaris or Linux or Windows specified kernel calls to optimize the use of the processors.

  6. What is an object graph?

    · When used in connection with serialization, an object graph is a tree structure rooted at the object you have written to the ObjectOutputStream. The branches and leaves of the tree are all the objects, which are reachable by following references from the root object. This structure is maintained by the ObjectOutputStream in order to ensure that the complete reachable state of the given object is serialized and to handle cyclic dependencies. The graph is computed on the fly, during the serialization process.
    A side-effect of maintaining this graph is that an object which has already been written to the stream will not be written again - it will be replaced by an object stream identifier, simply a number used to point at the previously object. To force a changed object to be written in its entirety, you need to reset() the stream, which clears the object graph
  7. Can a Vector or a Hashtable be serialized and deserialized?

    · Both these classes implement Serializable, and have been designed for serialization. So yes, they can be serialized.
    · In order to serialize a collection like Vector or Hashtable, you must also be able to serialize all of the objects contained in these collections. Otherwise, the collection would not be able to be completely restored. Your program will throw a NotSerializableException unless all objects stored in the Vector or Hashtable are also serializable.

  8. Is writing an object to an ObjectOutputStream a thread-safe operation?

    · Absolutely not. Serialization, i.e. passing an object reference to the writeObject () method of ObjectOutputStream, is not an atomic operation. Serialization involves traversing a graph of objects, each of which contributes to the state of the object being serialized, saving the state of each object in turn. This is a time-consuming procedure. If other threads are allowed to concurrently access or modify any one of the objects in the graph you could get undefined and unwanted results.

  9. What is the advantage of using Externalizable over Serializable?

    · Externalization allows a class to specify the methods to be used to write the object's contents to a stream and to read them back. By implementing Externalizable you can win performance at the cost of flexibility and extra code to maintain. If you implement Externalizable you stream the data directly without the need for reflection, which is used in the case of Serializable.

  10. What is JNDI and what is its use?

    · JNDI is Java Naming and Directory Services provides a standard interface to locate users, machines, networks, objects and services.

  11. Define a naming service?

    · Associates names with objects, which is otherwise called binding.
    · It provides a facility to find objects based on names, which is otherwise known as look-up.
    · When it is required to locate a machine on the network, DNS is used to translate the machine name to IP Address.

  12. What is a directory object? Is it different from naming object?
    · A directory object can store attributes with it. A directory metadata contains how the directory is structured.
  13. What are the standard naming and directory services available?
    · LDAP, NIS (Network Information services) and NDS (Novell Directory Services).
    61. What are the advantages of using JNDI?
    · It is unified to system to access all sorts of directory service information.
    · It is single API to access different directories with different protocols.
    · It insulates application from protocol and implementation details.
    · It is extensible. Future provider of directories can plug in their particular directory services to JNDI without affecting client code.

No comments: