]> gerrit.simantics Code Review - simantics/3d.git/blob - vtk/src/vtk/vtkJavaGarbageCollector.java
Mesh API to use Tuple3d instead of Vector3d
[simantics/3d.git] / vtk / src / vtk / vtkJavaGarbageCollector.java
1 package vtk;
2
3 import java.util.concurrent.Executors;
4 import java.util.concurrent.ScheduledExecutorService;
5 import java.util.concurrent.TimeUnit;
6
7 import javax.swing.SwingUtilities;
8
9 /**
10  * This class helps to provide a solution in concurrent garbage collection issue
11  * with VTK. This class allow automatic garbage collection done in a specific
12  * thread such as the EDT.
13  *
14  * @author sebastien jourdain - sebastien.jourdain@kitware.com
15  */
16 public class vtkJavaGarbageCollector {
17
18     private ScheduledExecutorService executor;
19     private Runnable deleteRunnable;
20     private Runnable deferredEdtRunnable;
21     private long periodTime;
22     private TimeUnit timeUnit;
23     private boolean autoCollectionRunning;
24     private boolean debug;
25
26     /**
27      * Build a garbage collector which is configured to garbage collect every
28      * seconds but has not been started yet. The user has to call
29      * SetAutoGarbageCollection(true) to make it start.
30      */
31     public vtkJavaGarbageCollector() {
32         // Default settings
33         debug = false;
34         periodTime = 1;
35         timeUnit = TimeUnit.SECONDS;
36         autoCollectionRunning = false;
37         //
38         executor = Executors.newSingleThreadScheduledExecutor();
39         deleteRunnable = new Runnable() {
40
41             public void run() {
42                 // Do the delete here
43                 vtkReferenceInformation info = vtkObject.JAVA_OBJECT_MANAGER.gc(debug);
44                 if (debug) {
45                     System.out.println(info);
46                     System.out.println(info.listKeptReferenceToString());
47                     System.out.println(info.listRemovedReferenceToString());
48                 }
49             }
50         };
51         deferredEdtRunnable = new Runnable() {
52
53             public void run() {
54                 SwingUtilities.invokeLater(deleteRunnable);
55             }
56         };
57     }
58
59     /**
60      * Set the schedule time that should be used to send a garbage collection
61      * request to the EDT.
62      *
63      * @param period
64      * @param timeUnit
65      */
66     public void SetScheduleTime(long period, TimeUnit timeUnit) {
67         this.periodTime = period;
68         this.timeUnit = timeUnit;
69         SetAutoGarbageCollection(autoCollectionRunning);
70     }
71
72     /**
73      * Whether to print out when garbage collection is run.
74      *
75      * @param debug
76      */
77     public void SetDebug(boolean debug) {
78         this.debug = debug;
79     }
80
81     /**
82      * Start or stop the automatic garbage collection in the EDT.
83      *
84      * @param doGarbageCollectionInEDT
85      */
86     public void SetAutoGarbageCollection(boolean doGarbageCollectionInEDT) {
87         autoCollectionRunning = doGarbageCollectionInEDT;
88         executor.shutdown();
89         if (doGarbageCollectionInEDT) {
90             executor = Executors.newSingleThreadScheduledExecutor();
91             executor.scheduleAtFixedRate(deferredEdtRunnable, periodTime, periodTime, timeUnit);
92         }
93     }
94
95     /**
96      * Shortcut for SetAutoGarbageCollection(true)
97      * @see SetAutoGarbageCollection
98      */
99     public void Start() {
100         this.SetAutoGarbageCollection(true);
101     }
102
103     /**
104      * Shortcut for SetAutoGarbageCollection(false)
105      * @see SetAutoGarbageCollection
106      */
107     public void Stop() {
108         this.SetAutoGarbageCollection(false);
109     }
110
111     /**
112      * @return the runnable that do the garbage collection. This could be used
113      *         if you want to execute the garbage collection in another thread
114      *         than the EDT.
115      */
116     public Runnable GetDeleteRunnable() {
117         return deleteRunnable;
118     }
119 }