Skip to content

Instantly share code, notes, and snippets.

@beoliver
Created April 26, 2015 14:40
Show Gist options
  • Save beoliver/0d090b4df27a81669a7f to your computer and use it in GitHub Desktop.
Save beoliver/0d090b4df27a81669a7f to your computer and use it in GitHub Desktop.
multithreaded searching of an ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String[] words = {"c", "b", "a", "a", "a", "r", "b", "c", "a", "b", "a", "c"};
SynchronisedInteger sharedValue = new SynchronisedInteger();
ArrayList<Thread> threads = new ArrayList<>();
for (int i = 0; i <= 9; i += 3) {
Thread t = new Thread(new Worker(words, i, i + 2, "a", sharedValue));
threads.add(t);
t.start();
}
// note that by doing this, all threads begin to execute in parallel
// we then call join on each thread to make sure that the MAIN THREAD waits
// for all of the threads to finish.
for (Thread t : threads) {
try { t.join(); } catch (InterruptedException e) {}
}
System.out.println("total matches: " + sharedValue.getContents());
System.exit(0);
}
}
class Worker implements Runnable {
static int idCounter = 0;
private int id;
private int count = 0;
private int i;
private int j;
private String[] values;
private String key;
private SynchronisedInteger sharedValue;
public Worker(String[] values, int start, int stop, String key, SynchronisedInteger sharedValue) {
this.i = start;
this.j = stop;
this.values = values;
this.key = key;
this.sharedValue = sharedValue;
this.id = idCounter++;
}
@Override
public void run() {
System.out.println("thread #" + id + " now running");
for (; i <= j; i++) {
if (values[i].equals(key)) {
System.out.println("match found at index " + i + " by thread #" + id);
count++;
}
}
System.out.println("thread #"+ id + " has matched " + count + " items");
sharedValue.add(count);
}
}
class SynchronisedInteger {
private Integer number = 0;
public SynchronisedInteger() {}
public synchronized Integer getContents() {
return number;
}
public synchronized void add(Integer count) {
number += count;
}
}
class AltSynchronisedInteger {
private Integer number = 0;
public AltSynchronisedInteger() {}
public Integer getContents() {
synchronized (this) {
return number;
}
}
public void add(Integer count) {
synchronized (this) {
number += count;
}
}
}
/*
EXAMPLE OUTPUT:
thread #0 now running
thread #2 now running
thread #1 now running
match found at index 3 by thread #1
match found at index 4 by thread #1
thread #1 has matched 2 items
match found at index 8 by thread #2
thread #3 now running
match found at index 10 by thread #3
match found at index 2 by thread #0
thread #3 has matched 1 items
thread #2 has matched 1 items
thread #0 has matched 1 items
total matches: 5
-- when run again...
thread #0 now running
match found at index 2 by thread #0
thread #0 has matched 1 items
thread #1 now running
match found at index 3 by thread #1
match found at index 4 by thread #1
thread #1 has matched 2 items
thread #2 now running
match found at index 8 by thread #2
thread #2 has matched 1 items
thread #3 now running
match found at index 10 by thread #3
thread #3 has matched 1 items
total matches: 5
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment