What is garbage collection
The runtime system keeps track of the memory that is allocated and is able to
determine whether that memory is still useable. This work is usually done in
background by a low-priority thread that is referred to as garbage collector. When the
gc finds memory that is no longer accessible from any live thread it takes steps to
release it back to the heap for reuse
What state does a thread enter when it terminates its processing?
When a thread terminates its processing, it enters the dead state.
Explain in depth Garbage collector ?
Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs. Thus making programmers more productive as they can now put more effort in coding rather than worrying about memory management.
The only disadvantage of garbage collector is it adds overheads. Because the JVM (Java virtual machine) has to keep a constant track of the objects which are not referenced and then free these unreferenced objects on fly. This whole process has a slight impact on the application performance. But garbage collector has a good algorithm and it runs in its own thread thus having a least impact on the application performance but still it has some impact.
What is the purpose of the wait(), notify(), and notifyAll() methods
The wait(), notify(), and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object’s wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object’s notify() or notifyAll() methods..
What are the states associated in the thread
Thread contains ready, running, waiting and dead states.
What is difference between String, StringBuffer and StringBuilder? When to use them?
The main difference between the three most commonly used String classes as follows.
- StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable.
- StringBuffer class implementation is synchronized while StringBuilder class is not synchronized.
- Concatenation operator “+” is internally implemented by Java using either StringBuffer or StringBuilder.
- Criteria to choose among String, StringBuffer and StringBuilder
- If the Object value will not change in a scenario use String Class because a String object is immutable.
- If the Object value can change and will only be modified from a single thread, use a StringBuilder because StringBuilder is unsynchronized(means faster).
- If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized).
What’s the main difference between ArrayList / HashMap and Vector / Hashtable?
Vector / HashTable are synchronized which means they are thread safe. Cost of thread safe is performance degradation. So if you are sure that you are not dealing with huge number of threads then you should use ArrayList / HashMap.But yes you can stillsynchronize List and Map’s using Collections provided methods :-
- List OurList = Collections.synchronizedList (OurList);
- Map OurMap = Collections.synchronizedMap (OurMap);
When a thread blocks on I/O, what state does it enter
A thread enters the waiting state when it blocks on I/O.
What is synchronization?-
Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.
What is the difference between the paint() and repaint() methods
The paint() method supports painting via a Graphics object. The repaint() method is used to causepaint() to be invoked by the AWT painting thread.