Search code examples
javamultithreadingconcurrencygarbage-collectionmark-and-sweep

Using finalize as exclusive lock


If I use stop-the-world GC and do some stuff in my finalize() method can I assume that all threads are suspended and I can do whatever I want and no thread will dare to compete for resources with me except the GC thread?


Solution

  • No, in fact there may even be multiple finalizer threads running concurrently.

    From the java language spec, "Finalizers may be called in any order, or even concurrently."