diff options
| -rw-r--r-- | test/079-phantom/src/Bitmap.java | 14 | ||||
| -rw-r--r-- | test/079-phantom/src/Main.java | 23 |
2 files changed, 29 insertions, 8 deletions
diff --git a/test/079-phantom/src/Bitmap.java b/test/079-phantom/src/Bitmap.java index ff43749e76..0d6e2d84e7 100644 --- a/test/079-phantom/src/Bitmap.java +++ b/test/079-phantom/src/Bitmap.java @@ -17,6 +17,7 @@ import java.lang.ref.ReferenceQueue; import java.lang.ref.PhantomReference; import java.util.ArrayList; +import java.util.concurrent.CountDownLatch; public class Bitmap { String mName; /* for debugging */ @@ -76,11 +77,14 @@ public class Bitmap { PhantomWrapper phan = new PhantomWrapper(wrapper, sPhantomQueue, nativeData); sPhantomList.add(phan); + wrapper.mPhantomWrapper = phan; return wrapper; } - static void freeNativeStorage(int nativeDataPtr) { + static void freeNativeStorage(int nativeDataPtr, CountDownLatch freeSignal) { System.out.println("freeNativeStorage: " + nativeDataPtr); + // Wake up the main thread that is [or will be] blocked until this native data is freed. + freeSignal.countDown(); } /* @@ -93,6 +97,9 @@ public class Bitmap { } public int mNativeData; + // The PhantomWrapper corresponding to this NativeWrapper. + public PhantomWrapper mPhantomWrapper; + /* @Override protected void finalize() throws Throwable { @@ -118,6 +125,8 @@ class PhantomWrapper extends PhantomReference { } public int mNativeData; + // This will be signaled once mNativeData has been freed. + public CountDownLatch mFreeSignal = new CountDownLatch(1); } /* @@ -137,8 +146,7 @@ class BitmapWatcher extends Thread { PhantomWrapper ref = (PhantomWrapper) mQueue.remove(); //System.out.println("dequeued ref " + ref.mNativeData + // " - " + ref); - Bitmap.freeNativeStorage(ref.mNativeData); - //ref.clear(); + Bitmap.freeNativeStorage(ref.mNativeData, ref.mFreeSignal); } catch (InterruptedException ie) { System.out.println("intr"); break; diff --git a/test/079-phantom/src/Main.java b/test/079-phantom/src/Main.java index daead2e1dd..ae2c688603 100644 --- a/test/079-phantom/src/Main.java +++ b/test/079-phantom/src/Main.java @@ -14,8 +14,11 @@ * limitations under the License. */ +import java.util.concurrent.CountDownLatch; + public class Main { Bitmap mBitmap1, mBitmap2, mBitmap3, mBitmap4; + CountDownLatch mFreeSignalA, mFreeSignalB; public static void sleep(int ms) { try { @@ -31,7 +34,6 @@ public class Main { Main main = new Main(); main.run(); - sleep(1000); System.out.println("done"); } @@ -46,22 +48,30 @@ public class Main { System.out.println("nulling 1"); mBitmap1 = null; Runtime.getRuntime().gc(); - sleep(500); + try { + mFreeSignalA.await(); // Block until dataA is definitely freed. + } catch (InterruptedException e) { + System.out.println("got unexpected InterruptedException e: " + e); + } System.out.println("nulling 2"); mBitmap2 = null; Runtime.getRuntime().gc(); - sleep(500); + sleep(200); System.out.println("nulling 3"); mBitmap3 = null; Runtime.getRuntime().gc(); - sleep(500); + sleep(200); System.out.println("nulling 4"); mBitmap4 = null; Runtime.getRuntime().gc(); - sleep(500); + try { + mFreeSignalB.await(); // Block until dataB is definitely freed. + } catch (InterruptedException e) { + System.out.println("got unexpected InterruptedException e: " + e); + } Bitmap.shutDown(); } @@ -77,7 +87,10 @@ public class Main { */ public void createBitmaps() { Bitmap.NativeWrapper dataA = Bitmap.allocNativeStorage(10, 10); + mFreeSignalA = dataA.mPhantomWrapper.mFreeSignal; Bitmap.NativeWrapper dataB = Bitmap.allocNativeStorage(20, 20); + mFreeSignalB = dataB.mPhantomWrapper.mFreeSignal; + mBitmap1 = new Bitmap("one", 10, 10, dataA); mBitmap2 = new Bitmap("two", 20, 20, dataB); mBitmap3 = mBitmap4 = new Bitmap("three/four", 20, 20, dataB); |