Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • zConcurrentModificationException


    Scorpius

    Recommended Posts

    Here's a snippet demonstrating how I worked around the ConcurrentModificationException that gets thrown due to multi-threaded scripts attempting to add/remove something to the same list at the same time causing the entire script to freeze.

     

    This should work with any other data type that produces the same behaviour

    // Our dummy hot list that throws ConcurrentModificationException when we try to add/remove something to/from it
    List<String> myHotList = new ArrayList<String>();
    
    // Assume this gets called wherever
    public not-so-static void zFunction()
    {
        int attempts = AddToList(myHotList, "zHotString");
        System.out.println("Added new hot string in " + attempts + " attempts");
        
        attempts = RemoveFromList(myHotList, "zHotString");
        System.out.println("Removed old but still hot string in " + attempts + " attempts");
    }
    
    // Our magic functions
    private int AddToList(List<String> zList, String new_string)
    {
        int attempts = 0;
        while(true)
        {
            try
            {
                attempts++;
                zList.add(new_string);
                break;
            }
            catch(Exception | Error e)
            {
            }
            try
            {
                Thread.sleep(3333);
            }
            catch(Exception | Error e)
            {
            }
        }
        return attempts;
    }
    
    private int RemoveFromList(List<String> zList, String old_but_hot_string)
    {
        int attempts = 0;
        while(true)
        {
            try
            {
                attempts++;
                zList.remove(old_but_hot_string);
                break;
            }
            catch(Exception | Error e)
            {
            }
            try
            {
                Thread.sleep(3333);
            }
            catch(Exception | Error e)
            {
            }
        }
        return attempts;
    }
    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.