Search in sources :

Example 21 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project jdk8u_jdk by JetBrains.

the class ComodifiedRemove method main.

public static void main(String[] args) {
    List list = new LinkedList();
    Object o1 = new Integer(1);
    list.add(o1);
    ListIterator e = list.listIterator();
    e.next();
    Object o2 = new Integer(2);
    list.add(o2);
    try {
        e.remove();
    } catch (ConcurrentModificationException cme) {
        return;
    }
    throw new RuntimeException("LinkedList ListIterator.remove() comodification check failed.");
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) ListIterator(java.util.ListIterator)

Example 22 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project kdeconnect-android by KDE.

the class DeviceFragment method refreshUI.

void refreshUI() {
    if (device == null || rootView == null) {
        return;
    }
    //Once in-app, there is no point in keep displaying the notification if any
    device.hidePairingNotification();
    mActivity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (device.isPairRequestedByPeer()) {
                ((TextView) rootView.findViewById(R.id.pair_message)).setText(R.string.pair_requested);
                rootView.findViewById(R.id.pair_progress).setVisibility(View.GONE);
                rootView.findViewById(R.id.pair_button).setVisibility(View.GONE);
                rootView.findViewById(R.id.pair_request).setVisibility(View.VISIBLE);
            } else {
                boolean paired = device.isPaired();
                boolean reachable = device.isReachable();
                boolean onData = NetworkHelper.isOnMobileNetwork(getContext());
                rootView.findViewById(R.id.pairing_buttons).setVisibility(paired ? View.GONE : View.VISIBLE);
                rootView.findViewById(R.id.not_reachable_message).setVisibility((paired && !reachable && !onData) ? View.VISIBLE : View.GONE);
                rootView.findViewById(R.id.on_data_message).setVisibility((paired && !reachable && onData) ? View.VISIBLE : View.GONE);
                try {
                    ArrayList<ListAdapter.Item> items = new ArrayList<>();
                    //Plugins button list
                    final Collection<Plugin> plugins = device.getLoadedPlugins().values();
                    for (final Plugin p : plugins) {
                        if (!p.hasMainActivity())
                            continue;
                        if (p.displayInContextMenu())
                            continue;
                        items.add(new PluginItem(p, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                p.startMainActivity(mActivity);
                            }
                        }));
                    }
                    //Failed plugins List
                    final ConcurrentHashMap<String, Plugin> failed = device.getFailedPlugins();
                    if (!failed.isEmpty()) {
                        if (errorHeader == null) {
                            errorHeader = new TextView(mActivity);
                            errorHeader.setPadding(0, ((int) (28 * getResources().getDisplayMetrics().density)), 0, ((int) (8 * getResources().getDisplayMetrics().density)));
                            errorHeader.setOnClickListener(null);
                            errorHeader.setOnLongClickListener(null);
                            errorHeader.setText(getResources().getString(R.string.plugins_failed_to_load));
                        }
                        items.add(new CustomItem(errorHeader));
                        for (Map.Entry<String, Plugin> entry : failed.entrySet()) {
                            String pluginKey = entry.getKey();
                            final Plugin plugin = entry.getValue();
                            if (plugin == null) {
                                items.add(new SmallEntryItem(pluginKey));
                            } else {
                                items.add(new SmallEntryItem(plugin.getDisplayName(), new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        plugin.getErrorDialog(mActivity).show();
                                    }
                                }));
                            }
                        }
                    }
                    //Plugins without permissions List
                    final ConcurrentHashMap<String, Plugin> permissionsNeeded = device.getPluginsWithoutPermissions();
                    if (!permissionsNeeded.isEmpty()) {
                        if (noPermissionsHeader == null) {
                            noPermissionsHeader = new TextView(mActivity);
                            noPermissionsHeader.setPadding(0, ((int) (28 * getResources().getDisplayMetrics().density)), 0, ((int) (8 * getResources().getDisplayMetrics().density)));
                            noPermissionsHeader.setOnClickListener(null);
                            noPermissionsHeader.setOnLongClickListener(null);
                            noPermissionsHeader.setText(getResources().getString(R.string.plugins_need_permission));
                        }
                        items.add(new CustomItem(noPermissionsHeader));
                        for (Map.Entry<String, Plugin> entry : permissionsNeeded.entrySet()) {
                            String pluginKey = entry.getKey();
                            final Plugin plugin = entry.getValue();
                            if (plugin == null) {
                                items.add(new SmallEntryItem(pluginKey));
                            } else {
                                items.add(new SmallEntryItem(plugin.getDisplayName(), new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {
                                        plugin.getPermissionExplanationDialog(mActivity).show();
                                    }
                                }));
                            }
                        }
                    }
                    ListView buttonsList = (ListView) rootView.findViewById(R.id.buttons_list);
                    ListAdapter adapter = new ListAdapter(mActivity, items);
                    buttonsList.setAdapter(adapter);
                    mActivity.invalidateOptionsMenu();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                //Ignore: The activity was closed while we were trying to update it
                } catch (ConcurrentModificationException e) {
                    Log.e("DeviceActivity", "ConcurrentModificationException");
                    //Try again
                    this.run();
                }
            }
        }
    });
}
Also used : CustomItem(org.kde.kdeconnect.UserInterface.List.CustomItem) ConcurrentModificationException(java.util.ConcurrentModificationException) ArrayList(java.util.ArrayList) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) SmallEntryItem(org.kde.kdeconnect.UserInterface.List.SmallEntryItem) ListView(android.widget.ListView) Collection(java.util.Collection) TextView(android.widget.TextView) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PluginItem(org.kde.kdeconnect.UserInterface.List.PluginItem) ListAdapter(org.kde.kdeconnect.UserInterface.List.ListAdapter) Plugin(org.kde.kdeconnect.Plugins.Plugin)

Example 23 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project opennms by OpenNMS.

the class TopoFRLayout method calcRepulsion.

protected void calcRepulsion(V v1) {
    FRVertexData fvd1 = getFRData(v1);
    if (fvd1 == null)
        return;
    fvd1.setLocation(0, 0);
    try {
        for (V v2 : getGraph().getVertices()) {
            if (v1 != v2) {
                Point2D p1 = transform(v1);
                Point2D p2 = transform(v2);
                if (p1 == null || p2 == null)
                    continue;
                double xDelta = p1.getX() - p2.getX();
                double yDelta = p1.getY() - p2.getY();
                xDelta = Math.abs(xDelta) > EPSILON ? xDelta : xDelta == 0 ? epsilon() : Math.signum(xDelta) * EPSILON;
                yDelta = Math.abs(yDelta) > EPSILON ? yDelta : yDelta == 0 ? epsilon() : Math.signum(yDelta) * EPSILON;
                double deltaLength = Math.sqrt((xDelta * xDelta) + (yDelta * yDelta));
                double force = (repulsion_constant * repulsion_constant) / deltaLength;
                if (Double.isNaN(force)) {
                    throw new RuntimeException("Unexpected mathematical result in FRLayout:calcPositions [repulsion]");
                }
                fvd1.offset((xDelta / deltaLength) * force, (yDelta / deltaLength) * force);
            }
        }
    } catch (ConcurrentModificationException cme) {
        calcRepulsion(v1);
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) Point2D(java.awt.geom.Point2D)

Example 24 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project pcgen by PCGen.

the class CharacterFacadeImpl method export.

/**
	 * @see pcgen.core.facade.CharacterFacade#export(pcgen.io.ExportHandler, java.io.BufferedWriter)
	 */
@Override
public void export(ExportHandler theHandler, BufferedWriter buf) throws ExportException {
    final int maxRetries = 3;
    for (int i = 0; i < maxRetries; i++) {
        try {
            Logging.log(Logging.DEBUG, "Starting export at serial " + theCharacter.getSerial() + " to " + theHandler.getTemplateFile());
            PlayerCharacter exportPc = getExportCharacter();
            //PlayerCharacter exportPc =  theCharacter;
            theHandler.write(exportPc, buf);
            Logging.log(Logging.DEBUG, "Finished export at serial " + theCharacter.getSerial() + " to " + theHandler.getTemplateFile());
            return;
        } catch (ConcurrentModificationException e) {
            Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
            for (Entry<Thread, StackTraceElement[]> threadEntry : allStackTraces.entrySet()) {
                if (threadEntry.getValue().length > 1) {
                    StringBuilder sb = new StringBuilder("Thread: " + threadEntry.getKey() + "\n");
                    for (StackTraceElement elem : threadEntry.getValue()) {
                        sb.append("  ");
                        sb.append(elem.toString());
                        sb.append("\n");
                    }
                    Logging.log(Logging.INFO, sb.toString());
                }
            }
            Logging.log(Logging.WARNING, "Retrying export after ConcurrentModificationException", e);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                Logging.errorPrint("Interrupted sleep - probably closing.");
                return;
            }
        }
    }
    Logging.errorPrint("Unable to export using " + theHandler.getTemplateFile() + " due to concurrent modifications.");
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) Entry(java.util.Map.Entry) PlayerCharacter(pcgen.core.PlayerCharacter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with ConcurrentModificationException

use of java.util.ConcurrentModificationException in project ACS by ACS-Community.

the class AcsCorba method shutdownORB.

/**
	 * Calls <code>m_orb.shutdown(wait_for_completion)</code>.
	 * <p>
	 * If this method is invoked from an ORB thread (as it happens in the Java container in case of shutdown being
	 * commanded by the manager), it returns immediately, independently of the <code>wait_for_completion</code>
	 * parameter. The <code>Orb#shutdown</code> call will then run asynchronously. Note that shutting down the ORB from
	 * within an ORB thread would result in a deadlock (or BAD_INV_ORDER exception) if <code>wait_for_completion</code>
	 * is true. The Java container will nonetheless wait for the ORB shutdown, because its main thread blocks on ORB#run
	 * and only continues when the ORB is shut down.
	 * 
	 * @Todo: As a workaround for a JacORB bug (http://www.jacorb.org/cgi-bin/bugzilla/show_bug.cgi?id=537), we
	 *        currently skim off the <code>ConcurrentModificationException</code> that the unsynchronized HashMap of
	 *        <code>ClientConnectionManager.shutdown</code> may throw. This should be removed when we upgrade JacORB.
	 * @param wait_for_completion
	 *            blocks this call until ORB has shut down. Will be effectively <code>false</code> if
	 *            <code>isOrbThread == true</code>.
	 * @param isOrbThread
	 *            must be <code>true</code> if the calling thread services a Corba invocation, e.g. to
	 *            {@link AcsContainer#shutdown}. 
	 *            Note that JacORB has the proprietary method <code>isInInvocationContext</code>, but we need the explicit flag
	 *            in order to stay ORB-independent.
	 */
public void shutdownORB(final boolean wait_for_completion, boolean isOrbThread) {
    if (m_orb != null) {
        if (isOrbThread) {
            Runnable cmd = new Runnable() {

                public void run() {
                    try {
                        // since m_orb.shutdown runs asynchronously anyway, there is no advantage in passing
                        // wait_for_completion even if that flag is set to true.
                        m_orb.shutdown(false);
                    } catch (ConcurrentModificationException ex) {
                    // ignore, see javadoc
                    }
                }
            };
            (new DaemonThreadFactory("ShutdownORB")).newThread(cmd).start();
        } else {
            // not an ORB thread, thus we can call shutdown directly
            try {
                if (wait_for_completion) {
                    // There appears to be a bug in JacORB that makes "orb.shutdown(true)" return too early (before shutdown completed),
                    // which then depending on timing can make a subsequent call to orb#destroy hang.
                    // Here we try out some additional synch'ing through Orb#run, and use a timeout to avoid blocking forever.
                    // However the premature return could be caused by the occasional ConcurrentModificationException (see comment on JacORB bug)
                    // in which case the additional orb shutdown synchronization on return from orb.run is useless.
                    // For ACS 8.0 we use both strategies though, the additional synch'ing and a sleep delay when catching ConcurrentModificationException.
                    final CountDownLatch synch1 = new CountDownLatch(1);
                    final CountDownLatch synch2 = new CountDownLatch(1);
                    Runnable cmd = new Runnable() {

                        public void run() {
                            synch1.countDown();
                            m_orb.run();
                            synch2.countDown();
                        }
                    };
                    // Start this ORB-blocking thread, and wait until it actually runs.
                    (new DaemonThreadFactory("WorkaroundBlockOnOrbTillShutdownCompletes")).newThread(cmd).start();
                    try {
                        synch1.await();
                        // sleep one second to minimize the risk that orb.run() has not made it to the point where it blocks
                        // when we call orb.shutdown next
                        Thread.sleep(1000);
                    } catch (InterruptedException ex1) {
                        // loggers probably don't work any more even though we have not called orb.shutdown yet,
                        // but the log manager and handlers has likely been flushed and shut down already
                        System.out.println("Failed to wait for the thread that should call orb.run to synch with ORB shutdown. " + ex1.toString());
                    }
                    // this call should only return when the ORB has shut down, but it may return too early
                    m_orb.shutdown(true);
                    // orb.shutdown(true) returned too soon.
                    try {
                        boolean orbRunUnblocked = synch2.await(30, TimeUnit.SECONDS);
                        if (!orbRunUnblocked) {
                            // Loggers cannot be expected to work at this point, thus printing to stdout
                            System.out.println("Failed to return within 30 s from orb.run() after ORB shutdown.");
                        }
                    } catch (InterruptedException ex) {
                        // Loggers cannot be expected to work at this point, thus printing to stdout
                        System.out.println("InterruptedException waiting for orb.run() to return after ORB shutdown. " + ex.toString());
                    }
                } else {
                    // don't wait for orb shutdown to complete...
                    m_orb.shutdown(false);
                }
            } catch (ConcurrentModificationException ex) {
                System.out.println("Caught a ConcurrentModificationException from ORB shutdown. " + "This should be harmless, see known JacORB bug http://www.jacorb.org/cgi-bin/bugzilla/show_bug.cgi?id=537");
                if (wait_for_completion) {
                    // which the client could interpret to not call orb.destroy
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex1) {
                        System.out.println("InterruptedException while sleeping after the ConcurrentModificationException in orb.shutdown");
                    }
                }
            }
        }
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) DaemonThreadFactory(alma.acs.concurrent.DaemonThreadFactory) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ConcurrentModificationException (java.util.ConcurrentModificationException)89 Iterator (java.util.Iterator)24 HashSet (java.util.HashSet)19 Set (java.util.Set)18 ResultSet (java.sql.ResultSet)16 ArrayList (java.util.ArrayList)12 IOException (java.io.IOException)11 HashMap (java.util.HashMap)9 GameLocal (org.apache.openejb.test.entity.cmr.manytomany.GameLocal)8 PlatformLocal (org.apache.openejb.test.entity.cmr.manytomany.PlatformLocal)8 ArtistLocal (org.apache.openejb.test.entity.cmr.onetomany.ArtistLocal)8 SongLocal (org.apache.openejb.test.entity.cmr.onetomany.SongLocal)8 List (java.util.List)7 Test (org.junit.Test)7 Map (java.util.Map)6 AbstractList (java.util.AbstractList)5 NoSuchElementException (java.util.NoSuchElementException)5 Collection (java.util.Collection)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 MultiMapRecord (com.hazelcast.multimap.impl.MultiMapRecord)3