Example usage for java.util.concurrent ArrayBlockingQueue offer

List of usage examples for java.util.concurrent ArrayBlockingQueue offer

Introduction

In this page you can find the example usage for java.util.concurrent ArrayBlockingQueue offer.

Prototype

public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException 

Source Link

Document

Inserts the specified element at the tail of this queue, waiting up to the specified wait time for space to become available if the queue is full.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    int capacity = 10;
    ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity);

    for (int i = 0; i < 10; i++) {
        queue.add(i);//from  ww w .j a v  a  2s.co  m
    }
    queue.offer(9999, 10, TimeUnit.MINUTES);
    System.out.println(queue);
}

From source file:net.sf.xfd.provider.PublicProvider.java

final boolean checkAccess(Uri uri, String grantMode, String necessaryMode) {
    try {/*  ww  w. ja  v  a  2s.  com*/
        verifyMac(uri, grantMode, necessaryMode);

        return true;
    } catch (FileNotFoundException fnfe) {
        final Context context = getContext();

        assert context != null;

        ObjectIntMap<String> decisions = null;

        final String caller = getCallingPackage();

        if (!TextUtils.isEmpty(caller)) {
            decisions = accessCache.get(uri.getPath());
            if (decisions == null) {
                decisions = new ObjectIntHashMap<>();
            } else {
                //noinspection SynchronizationOnLocalVariableOrMethodParameter
                synchronized (decisions) {
                    final int decision = decisions.get(caller);

                    switch (decision) {
                    case RESPONSE_ALLOW:
                        return true;
                    }
                }
            }
        }

        final ArrayBlockingQueue<Bundle> queue = new ArrayBlockingQueue<>(1);

        //noinspection RestrictedApi
        final ResultReceiver receiver = new ResultReceiver(new Handler(Looper.getMainLooper())) {
            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                try {
                    queue.offer(resultData, 4, TimeUnit.SECONDS);
                } catch (InterruptedException ignored) {
                }
            }
        };

        try {
            final Intent intent = authActivityIntent(context);

            if (intent == null)
                return false;

            final Bundle result;

            final CharSequence resolved = base.resolve(uri.getPath());

            // try to ensure, that no more than one dialog can appear at once
            uxLock.lockInterruptibly();
            try {
                context.startActivity(intent.putExtra(EXTRA_MODE, necessaryMode).putExtra(EXTRA_CALLER, caller)
                        .putExtra(EXTRA_UID, Binder.getCallingUid()).putExtra(EXTRA_CALLBACK, receiver)
                        .putExtra(EXTRA_PATH, resolved));

                result = queue.poll(10, TimeUnit.SECONDS);
            } finally {
                uxLock.unlock();
            }

            int decision = RESPONSE_DENY;

            if (result != null) {
                decision = result.getInt(EXTRA_RESPONSE, -1);
            }

            if (decision == RESPONSE_ALLOW) {
                if (decisions != null) {
                    //noinspection SynchronizationOnLocalVariableOrMethodParameter
                    synchronized (decisions) {
                        decisions.put(caller, RESPONSE_ALLOW);

                        accessCache.put(uri.getPath(), decisions);
                    }
                }

                return true;
            }
        } catch (InterruptedException ignored) {
        }
    }

    return false;
}