skip to Main Content

I am making artificial intelligence bots for my private server. Their names have to be random and unique, so I made a function that retrieves a random string from a website. it takes a second to generate 3 names and adds them to an arraylist. That means in 10 minutes I get 1800 randomly generated strings.

That is a little bit bad since I need a large amount of names in the shortest time possible, so I made multiple threads that generate those 1800 names in less than a minute. But I am not sure whether this is a good practice or not.

public static void init() {
    for (int i = 0; i < 600; i++) {
        new Thread("" + i) {
            public void run() {
                for (int j = 0; j < 3; j++) {
                    names.add(NameGenerator.generateRandomName(Utils.random(3, 12)));
                }
            }
        }.start();
    }
}

I don’t think that this is efficient nor the best practice.

Edit: (Added functions)

public static ArrayList<String> names = new ArrayList<String>();

public static String generateRandomName(int length) {
    String result;
    try {
        result = postURL("https://jimpix.co.uk/generators/word-generator.asp#result",
                "go=yes&ul1=0&chars=" + length + "&match=1&numwords=1&alpha=0&lk=", 30000);
    } catch (IOException e) {
        Logger.logObject("Failed generating a new name");
        return null;
    }
    result = result.substring(result.indexOf("check-username.asp?u=") + "check-username.asp?u=".length(),
            result.indexOf(""><span style="color:white;"));
    return capitalizeFirstLetter(result);
}

3

Answers


  1. I think that this is not a proper way to use threads. Threads are used to handle such things like running GUI and app logic separately so the GUI will not freeze. They are also used to run separated server’s and client’s thread. You will not actualy benefit from threads in this example. Also, without using executors you are going to run out of memory because of large number of threads (600 is ok but it should be avoided and limited). Use just one thread to not block your main program or do not use threads at all.

    In theory, performance would actualy boost on multicore processor but it depends on multiple factors.

    Change your code to something like this:

    public static void init() {
       new Thread(new Runnable() {
          public void run() {
             for (int i = 0; i < 600 * 3; i++) {
                  names.add(NameGenerator.generateName());  
             }
         }
       }).start();
    }
    

    Also, using this example, remember, that the thread is async so also provide some kind of callback function to get your program noticed if it matters.

    Use synchronized collection too.

    Login or Signup to reply.
  2. You will have to use a synchronized structure for your List as you modify it concurrently.
    As more efficient alternative, you could create a class implementing Runnable and declare in it a List field that contains the generated words. Instantiating 3 instances of the class, start them as Thread and merge their List result.
    You could use CountDownLatch from the client side that the started threads will “countDown”. It will ease the synchronization step to perform the merge.

    Login or Signup to reply.
  3. If you want to generate unique, random Strings, you could just use UUIDs. They are randomly-generated unique String in this format: aeb1f910-1340-4f9d-829b-c9e179b21b4e.

    You can generate them in Java using the UUID.randomUUID() method.
    Granted, they are not readable names, but they should fit your usecase quite well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search