@@ -61,9 +61,17 @@ static ReferenceQueue<Object> getQueue() {
6161 return queue ;
6262 }
6363
64+ static final boolean ENABLED = isFinalizationEnabled ();
65+
66+ private static native boolean isFinalizationEnabled ();
67+
6468 /* Invoked by VM */
6569 static void register (Object finalizee ) {
66- new Finalizer (finalizee );
70+ if (ENABLED ) {
71+ new Finalizer (finalizee );
72+ } else {
73+ throw new InternalError ("unexpected call to Finalizer::register when finalization is disabled" );
74+ }
6775 }
6876
6977 private void runFinalizer (JavaLangAccess jla ) {
@@ -130,7 +138,7 @@ public Void run() {
130138
131139 /* Called by Runtime.runFinalization() */
132140 static void runFinalization () {
133- if (VM .initLevel () == 0 ) {
141+ if (VM .initLevel () == 0 || ! ENABLED ) {
134142 return ;
135143 }
136144
@@ -182,14 +190,16 @@ public void run() {
182190 }
183191
184192 static {
185- ThreadGroup tg = Thread .currentThread ().getThreadGroup ();
186- for (ThreadGroup tgn = tg ;
187- tgn != null ;
188- tg = tgn , tgn = tg .getParent ());
189- Thread finalizer = new FinalizerThread (tg );
190- finalizer .setPriority (Thread .MAX_PRIORITY - 2 );
191- finalizer .setDaemon (true );
192- finalizer .start ();
193+ if (ENABLED ) {
194+ ThreadGroup tg = Thread .currentThread ().getThreadGroup ();
195+ for (ThreadGroup tgn = tg ;
196+ tgn != null ;
197+ tg = tgn , tgn = tg .getParent ());
198+ Thread finalizer = new FinalizerThread (tg );
199+ finalizer .setPriority (Thread .MAX_PRIORITY - 2 );
200+ finalizer .setDaemon (true );
201+ finalizer .start ();
202+ }
193203 }
194204
195205}
0 commit comments