package vtk;import java.lang.ref.WeakReference;import java.lang.reflect.Constructor;import java.util.HashMap;import java.util.TreeSet;import java.util.concurrent.locks.ReentrantLock;/*** Provide a Java thread safe implementation of vtkJavaMemoryManager. This does* not make VTK thread safe. This only insure that the change of reference count* will never happen in two concurrent thread in the Java world.** @see vtkJavaMemoryManager* @author sebastien jourdain - sebastien.jourdain@kitware.com*/public class vtkJavaMemoryManagerImpl implements vtkJavaMemoryManager {private vtkJavaGarbageCollector garbageCollector;private ReentrantLock lock;private vtkReferenceInformation lastGcResult;private HashMap<Long, WeakReference<vtkObjectBase>> objectMap;private HashMap<Long, String> objectMapClassName;public vtkJavaMemoryManagerImpl() {this.lock = new ReentrantLock();this.objectMap = new HashMap<Long, WeakReference<vtkObjectBase>>();this.objectMapClassName = new HashMap<Long, String>();this.garbageCollector = new vtkJavaGarbageCollector();}// Thread safepublic vtkObjectBase getJavaObject(Long vtkId) {// Check pre-conditionif (vtkId == null || vtkId.longValue() == 0) {throw new RuntimeException("Invalid ID, can not be null or equal to 0.");}// Check inside the map if the object is already thereWeakReference<vtkObjectBase> value = objectMap.get(vtkId);vtkObjectBase resultObject = (value == null) ? null : value.get();// If not, we have to do somethingif (value == null || resultObject == null) {try {// Make sure no concurrency could happen inside thatthis.lock.lock();// Now that we have the lock make sure someone else didn't// create the object in between, if so just return the created// instancevalue = objectMap.get(vtkId);resultObject = (value == null) ? null : value.get();if (resultObject != null) {return resultObject;}// We need to do the work of the gcif (value != null && resultObject == null) {this.unRegisterJavaObject(vtkId);}// No-one did create it, so let's do itif (resultObject == null) {try {String className = vtkObjectBase.VTKGetClassNameFromReference(vtkId.longValue());Class<?> c = Class.forName("vtk." + className);Constructor<?> cons = c.getConstructor(new Class<?>[] { long.class });resultObject = (vtkObjectBase) cons.newInstance(new Object[] { vtkId });} catch (Exception e) {e.printStackTrace();}}} finally {this.lock.unlock();}}return resultObject;}// Thread safepublic void registerJavaObject(Long id, vtkObjectBase obj) {try {this.lock.lock();this.objectMap.put(id, new WeakReference<vtkObjectBase>(obj));this.objectMapClassName.put(id, obj.GetClassName());} finally {this.lock.unlock();}}// Thread safepublic void unRegisterJavaObject(Long id) {try {this.lock.lock();this.objectMapClassName.remove(id);WeakReference<vtkObjectBase> value = this.objectMap.remove(id);// Prevent double deletion...if (value != null) {vtkObjectBase.VTKDeleteReference(id.longValue());} else {throw new RuntimeException("You try to delete a vtkObject that is not referenced in the Java object Map. You may have call Delete() twice.");}} finally {this.lock.unlock();}}// Thread safepublic vtkReferenceInformation gc(boolean debug) {System.gc();try {this.lock.lock();final vtkReferenceInformation infos = new vtkReferenceInformation(debug);for (Long id : new TreeSet<Long>(this.objectMap.keySet())) {vtkObjectBase obj = this.objectMap.get(id).get();if (obj == null) {infos.addFreeObject(this.objectMapClassName.get(id));this.unRegisterJavaObject(id);} else {infos.addKeptObject(this.objectMapClassName.get(id));}}this.lastGcResult = infos;return infos;} finally {this.lock.unlock();}}public vtkJavaGarbageCollector getAutoGarbageCollector() {return this.garbageCollector;}// Thread safepublic int deleteAll() {int size = this.objectMap.size();try {this.lock.lock();for (Long id : new TreeSet<Long>(this.objectMap.keySet())) {this.unRegisterJavaObject(id);}} finally {this.lock.unlock();}return size;}public int getSize() {return objectMap.size();}public vtkReferenceInformation getLastReferenceInformation() {return this.lastGcResult;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。