Question-161. Can you write a Java class that could be used both as an applet as well as an application ?
Answer- Yes. Add a main() method to the applet.
Question-162. What is multithreading ?
Answer- Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:
Threads share the same address space. Thread is lightweight. Cost of communication between process is low.
Question-163. What is thread ?
Answer- A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.
Question-164. What is the difference between preemptive scheduling and time slicing ?
Answer- Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.
Question-165. What does join() method ?
Answer- The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task.
Question-166. Is it possible to start a thread twice ?
Answer- No, there is no possibility to start a thread twice. If we does, it throws an exception.
Question-167. Can we call the run() method instead of start() ?
Answer- yes, but it will not work as a thread rather it will work as a normal object so there will not be context-switching between the threads.
Question-168. What about the daemon threads ?
Answer- The daemon threads are basically the low priority threads that provides the background support to the user threads. It provides services to the user threads.
Question-169. Can we make the user thread as daemon thread if thread is started ?
Answer- No, if you do so, it will throw IllegalThreadStateException
Question-170. What is shutdown hook ?
Answer- The shutdown hook is basically a thread i.e. invoked implicitely before JVM shuts down. So we can use it perform clean up resource.