简体   繁体   中英

How can I make a thread that seaches the first half of the arryalist and the second thread searches the second half?

I made a arraylist of items I dont know how to search split the search procedure. I want to assign thread one to search the first half and thread two to search the second half. And for the output if the name exist in the first half I want the computer to return the boolean of the name if it was found and the location where it was found. Like for example thread 1 found a name. Ivy exist true

 import java.util.ArrayList;

/* a class for storing a set of strings */
class Set {
/* items are stored in an ArrayList */
private ArrayList<String> items;

public Set() {
    items = new ArrayList<String>();
}

/* add an item in */
public void add(String element) {
    items.add(element);
}

/* check if an item exists TODO parallelize this process */
boolean exists(String element) {
    for(int i = 0; i < items.size(); i++) {
        /* if this is the item */
        if(items.get(i).equals(element)) {
            /* we found it */
            return true;
        }
    }

    /* didn't find it */
    return false;
   }
 }


public class Search {
  public static void main(String args[]) {
    /* create a test set */
    Set names = new Set();

    /* add 100 random names */
    names.add("Santiago");
    names.add("Darcel");
    names.add("Illa");
    names.add("Myrta");
    names.add("Greg");
    names.add("Annabell");
    names.add("Lonnie");
    names.add("Ramona");
    names.add("Pearl");
    names.add("Jaqueline");
    names.add("Winnifred");
    names.add("Roland");
    names.add("Alysa");
    names.add("Hilda");
    names.add("Jeanett");
    names.add("Kimberly");
    names.add("Annalee");
    names.add("Deane");
    names.add("Brittani");
    names.add("Natacha");
    names.add("Herta");
    names.add("Giovanna");
    names.add("Tressa");
    names.add("Morton");
    names.add("Ela");
    names.add("Chante");
    names.add("Melani");
    names.add("Omega");
    names.add("Roman");
    names.add("Rashida");
    names.add("Myles");
    names.add("Devorah");
    names.add("Luther");
    names.add("Annette");
    names.add("Tessa");
    names.add("Darryl");
    names.add("Thad");
    names.add("Freda");
    names.add("Laurence");
    names.add("Asa");
    names.add("Burma");
    names.add("Lila");
    names.add("Tierra");
    names.add("Idell");
    names.add("Ninfa");
    names.add("Denae");
    names.add("Randy");
    names.add("Milan");
    names.add("Karey");
    names.add("Carter");
    names.add("Arlette");
    names.add("Estela");
    names.add("Dacia");
    names.add("Cory");
    names.add("Leatrice");
    names.add("Maura");
    names.add("Tiana");
    names.add("Billy");
    names.add("Brittany");
    names.add("Kendall");
    names.add("Merri");
    names.add("Liane");
    names.add("Simone");
    names.add("Hilaria");
    names.add("Neely");
    names.add("Jeromy");
    names.add("Kiyoko");
    names.add("Alta");
    names.add("Lucien");
    names.add("Patria");
    names.add("Alphonso");
    names.add("Jenae");
    names.add("Sanda");
    names.add("Suk");
    names.add("Berry");
    names.add("Terry");
    names.add("Wei");
    names.add("Milagros");
    names.add("Adrianne");
    names.add("Dusti");
    names.add("Ivy");
    names.add("Cyndi");
    names.add("Quiana");
    names.add("Ellyn");
    names.add("Garnet");
    names.add("Hipolito");
    names.add("Eugena");
    names.add("Laveta");
    names.add("Eunice");
    names.add("Arnulfo");
    names.add("Luz");
    names.add("Ranee");
    names.add("Adolfo");
    names.add("Leola");
    names.add("Miguel");
    names.add("Jacque");
    names.add("Aja");
    names.add("Kelsie");
    names.add("Pamula");
    names.add("Marcus");

    /* test */
    System.out.printf("%s exists: %b.\n", "Ivy", names.exists("Ivy"));
    System.out.printf("%s exists: %b.\n", "Mark", names.exists("Mark"));
    System.out.printf("%s exists: %b.\n", "Pearl", names.exists("Pearl"));
    System.out.printf("%s exists: %b.\n", "Georgette", names.exists("Georgette"));
    System.out.printf("%s exists: %b.\n", "Simone", names.exists("Simone"));
    System.out.printf("%s exists: %b.\n", "Bobby", names.exists("Bobby"));
    System.out.printf("%s exists: %b.\n", "Roland", names.exists("Roland"));
    System.out.printf("%s exists: %b.\n", "Agatha", names.exists("Agatha"));
    System.out.printf("%s exists: %b.\n", "Luz", names.exists("Luz"));
    System.out.printf("%s exists: %b.\n", "Herman", names.exists("Herman"));
}

}

Create a new class which implements Runnable; its parameters (so, fields, set in the constructor) are [1] the list, and [2] a 'range', so, a start point and an end point. Your one starter thread can make as many of these as it likes. In your case, it would make 2; they both get the same list, but one gets the range 0-list.length()/2, the other gets the range list.length()/2-list.length().

Then, make 2 threads, each initialized with one of the instances you made. This is okay, as your class implements Runnable.

Then start them both.

For the answer, you need to know when the threads are done, AND you need the threads to convey the answer back to you. You can use the yield() method to freeze your own thread and wait for those to finish, guaranteeing that they all finished. You can then use a plain old field with a getter to query each of the created objects for the answer they got, and then you write some code to join these answers together.

Once you're done experimenting, there are various frameworks and takes on how to do this in a more streamlined fashion, where you really just reduce it all to a job, as described by a closure/lambda, and a reducing function which combines answers into a single answer; the framework then takes care of firing off the proper number of threads suitable for your CPU and such. But, this is a good beginning exercise to learn how those frameworks work, roughly. These frameworks can be found in the java.util.concurrent package, such as the fork/join framework, as well as executor pools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM