Java Code Examples for java.lang.ref.WeakReference.get()

The following are Jave code examples for showing how to use get() of the java.lang.ref.WeakReference class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: ResourceTable.java   View Source Code Vote up 6 votes
/**
 * Remove a PropertyChangeListener from the listener list. This removes a 
 * PropertyChangeListener that was registered for all properties. If 
 * listener was added more than once to the same event source, it will be 
 * notified one less time after being removed. If listener is null, or was 
 * never added, no exception is thrown and no action is taken.
 * 
 * @param listener The PropertyChangeListener to be rem ove
 */
public void removePropertyChangeListener(PropertyChangeListener listener)
{
    
    for(int index = listeners.size() - 1; index >= 0; index--)
    {
        WeakReference<PropertyChangeListener> ref = listeners.get(index);
        PropertyChangeListener refListener = ref.get();
        if(refListener != null)
        {
            if(refListener.equals(listener) == true)
            {
                listeners.remove(index);
            }
        }
        else
        {
            listeners.remove(index);
        }
    }
}
 
Example 2
Project: DroidPlugin   File: PluginManager.java   View Source Code Vote up 6 votes
@Override
public void onServiceDisconnected(ComponentName componentName) {
    Log.i(TAG, "onServiceDisconnected disconnected!");
    mPluginManager = null;

    Iterator<WeakReference<ServiceConnection>> iterator = sServiceConnection.iterator();
    while (iterator.hasNext()) {
        WeakReference<ServiceConnection> wsc = iterator.next();
        ServiceConnection sc = wsc != null ? wsc.get() : null;
        if (sc != null) {
            sc.onServiceDisconnected(componentName);
        } else {
            iterator.remove();
        }
    }
    //服务连接断开,需要重新连接。
    connectToService();
}
 
Example 3
Project: OpenJSharp   File: ThreadReferenceImpl.java   View Source Code Vote up 6 votes
/**
 * Propagate the the thread state change information
 * to registered listeners.
 * Must be entered while synchronized on vm.state()
 */
private void processThreadAction(ThreadAction action) {
    synchronized (vm.state()) {
        Iterator<WeakReference<ThreadListener>> iter = listeners.iterator();
        while (iter.hasNext()) {
            WeakReference<ThreadListener> ref = iter.next();
            ThreadListener listener = ref.get();
            if (listener != null) {
                switch (action.id()) {
                    case ThreadAction.THREAD_RESUMABLE:
                        if (!listener.threadResumable(action)) {
                            iter.remove();
                        }
                        break;
                }
            } else {
                // Listener is unreachable; clean up
                iter.remove();
            }
        }

        // Discard our local cache
        resetLocalCache();
    }
}
 
Example 4
Project: openjdk-jdk10   File: TimedAcquireLeak.java   View Source Code Vote up 6 votes
/** No guarantees, but effective in practice. */
private static void forceFullGc() {
    CountDownLatch finalizeDone = new CountDownLatch(1);
    WeakReference<?> ref = new WeakReference<Object>(new Object() {
        protected void finalize() { finalizeDone.countDown(); }});
    try {
        for (int i = 0; i < 10; i++) {
            System.gc();
            if (finalizeDone.await(1L, SECONDS) && ref.get() == null) {
                System.runFinalization(); // try to pick up stragglers
                return;
            }
        }
    } catch (InterruptedException unexpected) {
        throw new AssertionError("unexpected InterruptedException");
    }
    throw new AssertionError("failed to do a \"full\" gc");
}
 
Example 5
Project: coordination_oru   File: GeometrySmoother.java   View Source Code Vote up 6 votes
/**
 * Gets the interpolation parameters for a Bezier curve approximated
 * by the given number of vertices. 
 * 
 * @param npoints number of vertices
 * 
 * @return array of {@code InterpPoint} objects holding the parameter values
 */
private InterpPoint[] getInterpPoints(int npoints) {
    WeakReference<InterpPoint[]> ref = lookup.get(npoints);
    InterpPoint[] ip = null;
    if (ref != null) ip = ref.get();
    
    if (ip == null) {
        ip = new InterpPoint[npoints];
    
        for (int i = 0; i < npoints; i++) {
            double t = (double) i / (npoints - 1);
            double tc = 1.0 - t;

            ip[i] = new InterpPoint();
            ip[i].t[0] = tc*tc*tc;
            ip[i].t[1] = 3.0*tc*tc*t;
            ip[i].t[2] = 3.0*tc*t*t;
            ip[i].t[3] = t*t*t;
            ip[i].tsum = ip[i].t[0] + ip[i].t[1] + ip[i].t[2] + ip[i].t[3];
        }
        
        lookup.put(npoints, new WeakReference<InterpPoint[]>(ip));
    }
    
    return ip;
}
 
Example 6
Project: alvisnlp   File: Tuple.java   View Source Code Vote up 5 votes
/**
 * Returns all arguments.
 */
public Collection<Element> getAllArguments() {
	List<Element> result = new ArrayList<Element>(arguments.size());
	for (WeakReference<Element> ref : arguments.values()) {
		Element elt = ref.get();
		if (elt != null)
			result.add(elt);
	}
	Collections.sort(result, ElementComparator.INSTANCE);
    return result;
}
 
Example 7
Project: RLibrary   File: RBaseViewHolder.java   View Source Code Vote up 5 votes
public <T extends View> T v(@IdRes int resId) {
    WeakReference<View> viewWeakReference = sparseArray.get(resId);
    View view;
    if (viewWeakReference == null) {
        view = itemView.findViewById(resId);
        sparseArray.put(resId, new WeakReference<>(view));
    } else {
        view = viewWeakReference.get();
        if (view == null) {
            view = itemView.findViewById(resId);
            sparseArray.put(resId, new WeakReference<>(view));
        }
    }
    return (T) view;
}
 
Example 8
Project: Krine   File: KrineClassPath.java   View Source Code Vote up 5 votes
/**
 */
void notifyListeners() {
    for (Enumeration e = listeners.elements(); e.hasMoreElements(); ) {
        WeakReference wr = (WeakReference) e.nextElement();
        ClassPathListener l = (ClassPathListener) wr.get();
        if (l == null)  // garbage collected
            listeners.removeElement(wr);
        else
            l.classPathChanged();
    }
}
 
Example 9
Project: PlayerBase   File: BaseBindPlayerEventReceiver.java   View Source Code Vote up 5 votes
private void callBackErrorEventListener(int eventCode, Bundle bundle) {
    if(mErrorEventListenerList==null)
        return;
    Iterator<WeakReference<OnErrorListener>> iterator = mErrorEventListenerList.iterator();
    while (iterator.hasNext()){
        WeakReference<OnErrorListener> onErrorListener = iterator.next();
        if(onErrorListener!=null){
            OnErrorListener errorListener = onErrorListener.get();
            if(errorListener!=null){
                errorListener.onError(eventCode,bundle);
            }
        }
    }
}
 
Example 10
Project: holon-vaadin7   File: ItemCacheMap.java   View Source Code Vote up 5 votes
/**
 * Get a cached Item by index
 * @param index Index
 * @return Cached Item, or <code>null</code> if Item was not cached or was garbage collected according to
 *         {@link WeakReference} behaviour
 */
public ITEM getItem(int index) {
	WeakReference<ITEM> itemReference = get(index);
	if (itemReference != null) {
		return itemReference.get();
	}
	return null;
}
 
Example 11
Project: permissions4m   File: NormalApplyPermissions.java   View Source Code Vote up 5 votes
private static void requestNextPermissionWithListener(PermissionWrapper wrapper) {
    int nextCode = wrapper.getRequestCodes()[0];
    if (wrapper.getRequestCode() != nextCode) {
        WeakReference<PermissionWrapper> reference = AbstractWrapper.getWrapperMap().get(new
                AbstractWrapper.Key(wrapper.getContext(), nextCode));
        if (reference != null) {
            AbstractWrapper permissionWrapper = (AbstractWrapper) reference.get();
            if (permissionWrapper != null) {
                permissionWrapper.requestPermissionWithListener();
            }
        }
    }
}
 
Example 12
Project: QSVideoPlayer   File: ConfigManage.java   View Source Code Vote up 5 votes
public static void releaseOther(QSVideoView qs) {
    for (WeakReference<QSVideoView> w : videos) {
        QSVideoView q = w.get();
        if (q != null & q != qs)
            q.release();
    }
}
 
Example 13
Project: OpenJSharp   File: StackMachine.java   View Source Code Vote up 5 votes
@SuppressWarnings("unused")
private static StackEntry[] fetchStack() {
    WeakReference<StackEntry[]> ref = stacks.get();
    StackEntry[] stack = ref.get();
    if (stack == null) {
        ref = new WeakReference<StackEntry[]>(stack = allocateStack());
        stacks.set(ref);
    }
    return stack;
}
 
Example 14
Project: openjdk-jdk10   File: InputEdge.java   View Source Code Vote up 5 votes
public static synchronized InputEdge createImmutable(char fromIndex, char toIndex, int from, int to, String label, String type) {
    InputEdge edge = new InputEdge(fromIndex, toIndex, from, to, label, type, State.IMMUTABLE);
    WeakReference<InputEdge> result = immutableCache.get(edge);
    if (result != null) {
        InputEdge edge2 = result.get();
        if (edge2 != null) {
            return edge2;
        }
    }
    immutableCache.put(edge, new WeakReference<>(edge));
    return edge;
}
 
Example 15
Project: QSVideoPlayer   File: ConfigManage.java   View Source Code Vote up 5 votes
private void addVideoView(QSVideoView q) {
    WeakReference<QSVideoView> w = new WeakReference<>(q);
    videos.add(w);
    Iterator<WeakReference<QSVideoView>> iterList = videos.iterator();//List接口实现了Iterable接口
    while (iterList.hasNext()) {
        WeakReference<QSVideoView> ww = iterList.next();
        if (ww.get() == null)
            iterList.remove();
    }
}
 
Example 16
Project: FirefoxData-android   File: BaseResource.java   View Source Code Vote up 5 votes
public static boolean isHttpResponseObserver(HttpResponseObserver httpResponseObserver) {
  for (WeakReference<HttpResponseObserver> weakReference : httpResponseObservers) {
    HttpResponseObserver innerHttpResponseObserver = weakReference.get();
    if (innerHttpResponseObserver == httpResponseObserver) {
      return true;
    }
  }
  return false;
}
 
Example 17
Project: gemini.blueprint   File: PublishingServiceFactory.java   View Source Code Vote up 5 votes
public Object getService(Bundle bundle, ServiceRegistration serviceRegistration) {
	if (log.isTraceEnabled()) {
		log.trace("Get service called by bundle " + OsgiStringUtils.nullSafeName(bundle) + " on registration "
				+ OsgiStringUtils.nullSafeToString(serviceRegistration.getReference()));
	}

	targetResolver.activate();
	Object bn = targetResolver.getBean();

	// handle SF beans
	if (bn instanceof ServiceFactory) {
		bn = ((ServiceFactory) bn).getService(bundle, serviceRegistration);
	}

	if (createTCCLProxy) {
		// check proxy cache
		synchronized (proxyCache) {
			WeakReference<Object> value = proxyCache.get(bn);
			Object proxy = null;
			if (value != null) {
				proxy = value.get();
			}
			if (proxy == null) {
				proxy = createCLLProxy(bn);
				proxyCache.put(bn, new WeakReference<Object>(proxy));
			}
			bn = proxy;
		}
	}

	return bn;
}
 
Example 18
Project: jdk8u-jdk   File: Test7064279.java   View Source Code Vote up 5 votes
public static void main(String[] args) throws Exception {
    WeakReference ref = new WeakReference(test("test.jar", "test.Test"));
    try {
        int[] array = new int[1024];
        while (true) {
            array = new int[array.length << 1];
        }
    }
    catch (OutOfMemoryError error) {
        System.gc();
    }
    if (null != ref.get()) {
        throw new Error("ClassLoader is not released");
    }
}
 
Example 19
Project: android-project-gallery   File: ImageGridActivity.java   View Source Code Vote up 4 votes
public RecyclerViewListAdapter(WeakReference<ImageGridActivity> weakReference, List<ITEM> items)
{
	super(weakReference.get(), items);
	this.mWeakReference = weakReference;
}
 
Example 20
Project: rongyunDemo   File: SyncHttpClient.java   View Source Code Vote up 3 votes
/**
 * Cancels any pending (or potentially active) requests associated with the
 * passed Context.
 * <p>
 * &nbsp;
 * </p>
 * <b>Note:</b> This will only affect requests which were created with a
 * non-null android Context. This method is intended to be used in the
 * onDestroy method of your android activities to destroy all requests which
 * are no longer required.
 *
 * @param context               the android Context instance associated to the request.
 * @param mayInterruptIfRunning specifies if active requests should be cancelled along with
 *                              pending requests.
 */
public void cancelRequests(Context context, boolean mayInterruptIfRunning) {
    List<WeakReference<Future<?>>> requestList = requestMap.get(context);
    if (requestList != null) {
        for (WeakReference<Future<?>> requestRef : requestList) {
            Future<?> request = requestRef.get();
            if (request != null) {
                request.cancel(mayInterruptIfRunning);
            }
        }
    }
    requestMap.remove(context);
}