Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. public class DeathNotificationObject {
  2. private static ReferenceQueue<DeathNotificationObject>
  3. refQueue = new ReferenceQueue<DeathNotificationObject>();
  4.  
  5. static {
  6. Thread deathThread = new Thread("Death notification") {
  7. @Override
  8. public void run() {
  9. try {
  10. while (true) {
  11. refQueue.remove();
  12. System.out.println("I'm dying!");
  13. }
  14. } catch (Throwable t) {
  15. t.printStackTrace();
  16. }
  17. }
  18. };
  19. deathThread.setDaemon(true);
  20. deathThread.start();
  21. }
  22.  
  23. public DeathNotificationObject() {
  24. System.out.println("I'm born.");
  25. new PhantomReference<DeathNotificationObject>(this, refQueue);
  26. }
  27.  
  28. public static void main(String[] args) {
  29. for (int i = 0 ; i < 10 ; i++) {
  30. new DeathNotificationObject();
  31. }
  32. try {
  33. System.gc();
  34. Thread.sleep(3000);
  35. } catch (InterruptedException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40.  
  41. I'm born.
  42. I'm born.
  43. I'm born.
  44. I'm born.
  45. I'm born.
  46. I'm born.
  47. I'm born.
  48. I'm born.
  49. I'm born.
  50. I'm born.
  51.  
  52. public class ElementCachedImage {
  53. private static Map<PhantomReference<ElementCachedImage>, File>
  54. refMap = new HashMap<PhantomReference<ElementCachedImage>, File>();
  55. private static ReferenceQueue<ElementCachedImage>
  56. refQue = new ReferenceQueue<ElementCachedImage>();
  57.  
  58. static {
  59. Thread cleanUpThread = new Thread("Image Temporary Files cleanup") {
  60. @Override
  61. public void run() {
  62. try {
  63. while (true) {
  64. Reference<? extends ElementCachedImage> phanRef =
  65. refQue.remove();
  66. File f = refMap.remove(phanRef);
  67. Calendar c = Calendar.getInstance();
  68. c.setTimeInMillis(f.lastModified());
  69. _log.debug("Deleting unused file: " + f + " created at " + c.getTime());
  70. f.delete();
  71. }
  72. } catch (Throwable t) {
  73. _log.error(t);
  74. }
  75. }
  76. };
  77. cleanUpThread.setDaemon(true);
  78. cleanUpThread.start();
  79. }
  80.  
  81. ImageWrapper img = null;
  82.  
  83. private static Logger _log = Logger.getLogger(ElementCachedImage.class);
  84.  
  85. public boolean copyToFile(File dest) {
  86. try {
  87. FileUtils.copyFile(img.getFile(), dest);
  88. } catch (IOException e) {
  89. _log.error(e);
  90. return false;
  91. }
  92. return true;
  93. }
  94.  
  95. public ElementCachedImage(BufferedImage bi) {
  96. if (bi == null) throw new NullPointerException();
  97. img = new ImageWrapper(bi);
  98. PhantomReference<ElementCachedImage> pref =
  99. new PhantomReference<ElementCachedImage>(this, refQue);
  100. refMap.put(pref, img.getFile());
  101.  
  102. new Thread("Save image to file") {
  103. @Override
  104. public void run() {
  105. synchronized(ElementCachedImage.this) {
  106. if (img != null) {
  107. img.saveToFile();
  108. img.getFile().deleteOnExit();
  109. }
  110. }
  111. }
  112. }.start();
  113. }
  114. }
  115.  
  116. import java.lang.ref.PhantomReference;
  117. import java.lang.ref.Reference;
  118. import java.lang.ref.ReferenceQueue;
  119. import java.util.HashSet;
  120. import java.util.Set;
  121.  
  122. public class DeathNotificationObject {
  123. private static ReferenceQueue<DeathNotificationObject> refQueue = new ReferenceQueue<DeathNotificationObject>();
  124. private static Set<Reference<DeathNotificationObject>> refs = new HashSet<>();
  125.  
  126. static {
  127. Thread deathThread = new Thread("Death notification") {
  128. @Override
  129. public void run() {
  130. try {
  131. while (true) {
  132. Reference<? extends DeathNotificationObject> ref = refQueue.remove();
  133. refs.remove(ref);
  134. System.out.println("I'm dying!");
  135. }
  136. } catch (Throwable t) {
  137. t.printStackTrace();
  138. }
  139. }
  140. };
  141. deathThread.setDaemon(true);
  142. deathThread.start();
  143. }
  144.  
  145. public DeathNotificationObject() {
  146. System.out.println("I'm born.");
  147. PhantomReference<DeathNotificationObject> ref = new PhantomReference<DeathNotificationObject>(this, refQueue);
  148. refs.add(ref);
  149. }
  150.  
  151. public static void main(String[] args) {
  152. for (int i = 0 ; i < 10 ; i++) {
  153. new DeathNotificationObject();
  154. }
  155. try {
  156. System.gc();
  157. Thread.sleep(3000);
  158. } catch (InterruptedException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. }
  163.  
  164. public DeathNotificationObject() {
  165. System.out.println("I'm born.");
  166. PhantomReference<DeathNotificationObject> ref = new PhantomReference<DeathNotificationObject>(this, refQueue);
  167. ref.enqueue();
  168. }
  169.  
  170. public static void main(String[] args) throws InterruptedException {
  171.  
  172. for (int i = 0 ; i < 5 ; i++) {
  173. DeathNotificationObject item = new DeathNotificationObject();
  174.  
  175. System.out.println("working with item "+item);
  176. Thread.sleep(1000);
  177. System.out.println("stopped working with item "+item);
  178. // simulate release item
  179. item = null;
  180. }
  181.  
  182. try {
  183. System.gc();
  184. Thread.sleep(3000);
  185. } catch (InterruptedException e) {
  186. e.printStackTrace();
  187. }
  188. }
  189.  
  190. I'm born.
  191. I'm dying!
  192. working with item DeathNotificationObject@6908b095
  193. stopped working with item DeathNotificationObject@6908b095
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement