Skip to content

Package: GCPhantomThread

GCPhantomThread

nameinstructionbranchcomplexitylinemethod
GCPhantomThread(ReferenceQueue, PhantomReference)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
didCollectReference()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
run()
M: 4 C: 37
90%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 1 C: 12
92%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Edgar Mueller - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.test.common.spi;
15:
16: import java.lang.ref.PhantomReference;
17: import java.lang.ref.Reference;
18: import java.lang.ref.ReferenceQueue;
19:
20: class GCPhantomThread extends Thread {
21:
22:         private static final int DEFAULT_TIMEOUT = 1000;
23:
24:         private boolean hasReference;
25:         private final ReferenceQueue<?> queue;
26:         private final PhantomReference<?> expectedReference;
27:         private int retry;
28:
29:         GCPhantomThread(ReferenceQueue<?> queue,
30:                 PhantomReference<?> expectedReference) {
31:                 this.queue = queue;
32:                 this.expectedReference = expectedReference;
33:         }
34:
35:         public boolean didCollectReference() {
36:                 return hasReference;
37:         }
38:
39:         @Override
40:         public void run() {
41:•                while (!hasReference && retry < 100) {
42:                         Reference<?> ref = null;
43:                         System.gc();
44:                         System.runFinalization();
45:                         try {
46:                                 Thread.sleep(50);
47:                         } catch (final InterruptedException e) {
48:                                 // ignore
49:                         }
50:                         try {
51:                                 ref = queue.remove(DEFAULT_TIMEOUT);
52:                         } catch (final IllegalArgumentException e) {
53:                                 // ignore
54:                         } catch (final InterruptedException e) {
55:                                 // ignore
56:                         }
57:•                        if (ref != null && ref == expectedReference) {
58:                                 hasReference = true;
59:                         }
60:                         retry++;
61:                 }
62:         }
63: }