1 package vtk.test; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.util.concurrent.TimeUnit; 5 6 import vtk.vtkJavaGarbageCollector; 7 import vtk.vtkJavaTesting; 8 import vtk.vtkPoints; 9 import vtk.vtkUnstructuredGrid; 10 11 /** 12 * This test run concurrently thread that create Java view of VTK objects and 13 * the EDT that collect those objects as well as another thread. 14 * 15 * Based on the changes done to prevent concurrency during creation/deletion of 16 * VTK object this application shouldn't crash. 17 * 18 * @author sebastien jourdain - sebastien.jourdain@kitware.com 19 */ 20 public class ConcurrencyGC { 21 main(String[] args)22 public static void main(String[] args) throws InterruptedException, InvocationTargetException { 23 try { 24 vtkJavaTesting.Initialize(args, true); 25 26 // Setup working runnable 27 Runnable workingJob = new Runnable() { 28 public void run() { 29 try { 30 vtkUnstructuredGrid grid = new vtkUnstructuredGrid(); 31 grid.SetPoints(new vtkPoints()); 32 vtkPoints p; 33 long timeout = System.currentTimeMillis() + 60000; // +1 minute 34 while (System.currentTimeMillis() < timeout) { 35 p = grid.GetPoints(); 36 if (p == null) { 37 throw new RuntimeException("Invalid pointer null"); 38 } 39 if (p.GetReferenceCount() != 2) { 40 throw new RuntimeException("Invalid reference count of " + p.GetReferenceCount()); 41 } 42 } 43 } catch (Throwable e) { 44 e.printStackTrace(); 45 vtkJavaTesting.Exit(vtkJavaTesting.FAILED); 46 } 47 } 48 }; 49 50 // Start threads for concurrency (2xwork + 1xGC + 1xGCinEDT) 51 Thread worker1 = new Thread(workingJob); 52 Thread worker2 = new Thread(workingJob); 53 54 // Start working thread 55 worker1.start(); 56 worker2.start(); 57 58 // Setup GC 59 vtkJavaGarbageCollector gc = vtkJavaTesting.StartGCInEDT(10, TimeUnit.MILLISECONDS); // Start periodic GC in EDT 60 new Thread(gc.GetDeleteRunnable()).start(); // Start GC in tight loop 61 62 // If worker finished close everything 63 worker1.join(); 64 worker2.join(); 65 gc.Stop(); 66 vtkJavaTesting.Exit(vtkJavaTesting.PASSED); 67 68 } catch (Throwable e) { 69 e.printStackTrace(); 70 vtkJavaTesting.Exit(vtkJavaTesting.FAILED); 71 } 72 } 73 } 74