Threads that are not executing critical code in another process can be terminated immediately. However, if a thread is executing critical code in another process, the system must wait until the thread has left the critical section before terminating the thread. Marking which sections of code are critical and which sections are not can be done in Java using using the synchronized keyword.
Because critical sections are typically as small as possible, the short delay in process termination is acceptable. However, if the server gets stuck in one of its critical sections (eg. due to a bug), a request to kill the client process would also hang. In this case, the user could be prompted to force-kill the client, and by implication the server along with it. (another option is to give the server a chance to recover)
There is no reason why the finalize methods and the garbage collector need to be run at the same time. It is possible to handle resource reclamation in two stages. First, all objects of the process are invalidated and finalized. Second, the objects are garbage collected when the garbage collector finds that there are no more references to them. All objects that were only referenced within the process are immediately considered garbage. Objects that are referred to by other processes are considered garbage once all references are released.
Once the process is terminated, other processes that hold references to objects of the dead process will be holding references to invalid objects. These are objects that are still in memory, but access to fields or methods of that object is denied by throwing an InvalidReferenceError.
This two stage process of garbage collection is shown in the following
A single stage garbage collector can either be run whenever the system decides, or explicitly by invoking System.gc(). Similarly in the two-stage garbage collector described above, each stage may be run either explicitly or when the system decides. When a process is terminated, the system will invoke the first stage of the garbage collector explicitly. The actual objects can then be reclaimed at any later stage.