Java Thread Interview Questions
1. Difference between Thread and Process in Java?
Thread is subset of Process, in other words one processcan contain multiple threads. Two process runs on different memory space, butall threads share same memory space. Don't confuse this with stack memory,which is different for different thread and used to store local data to thatthread.
2. When to use runnable vs. Thread
Thisis follow-up of previous multi-threading interview question. As we know we canimplement thread either by extending Thread class or implementing Runnableinterface, question arise, which one is better and when to use one? Thisquestion will be easy to answer, if you know that Java programming languagedoesn't support multiple inheritance of class, but it allows you to implementmultiple interface. Which means, its better to implement Runnable than extendsThread, if you also want to extend another class.
3. What is race condition inJava?
Racecondition are cause of some subtle programming bugs when Java programs areexposed to concurrent execution environment. As name suggests, race conditionoccurs due to race between multiple threads, if a thread which is supposed toexecute first lost the race and executed second, behaviour of code changes,which surface as non-deterministic bugs. This is one of the hardest bugs tofind and re-produce because of random nature of racing between threads.
4. Share data between threads: You can share data betweenthreads by using shared object, or concurrent data-structure likeBlockingQueue.
5. ThreadLocal variable: ThreadLocal variables are specialkind of variable available to Java programmer. Just like instance variable isper instance, ThreadLocal variable is per thread.
6. Difference between synchronized and concurrent: Thoughboth synchronized and concurrent collection provides thread-safe collectionsuitable for multi-threaded and concurrent access, later is more scalable thanformer.
To be continued...