Created
April 25, 2015 23:33
-
-
Save beoliver/0076789ae02cc6824682 to your computer and use it in GitHub Desktop.
Simple Java thread example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.atomic.AtomicInteger; | |
public class Main { | |
public static void main(String[] args) { | |
String[] words = {"a", "b", "c", "a", "a", "r", "b", "c", "a"}; | |
SharedValue sharedValue = new SharedValue(); | |
for (int i = 0; i <= 6; i += 3) { | |
Thread t = new Thread(new Worker(words,i,i+2,"a",sharedValue)); | |
t.start(); | |
try { t.join(); } catch (InterruptedException e) {} | |
// we use t.join() to make sure that all threads complete before | |
// printing the result | |
} | |
System.out.println(sharedValue.getContents()); | |
/* | |
match found at index 0 | |
match found at index 3 | |
match found at index 4 | |
match found at index 8 | |
4 | |
*/ | |
System.exit(0); | |
} | |
} | |
class Worker implements Runnable { | |
private int i; | |
private int j; | |
private String[] values; | |
private String key; | |
private SharedValue sharedValue; | |
public Worker(String[] values, int start, int stop, String key, SharedValue sharedValue) { | |
this.i = start; | |
this.j = stop; | |
this.values = values; | |
this.key = key; | |
this.sharedValue = sharedValue; | |
} | |
@Override | |
public void run() { | |
for (; i <= j; i++) { | |
if (values[i].equals(key)) { | |
System.out.println("match found at index " + i); | |
sharedValue.increment(); | |
} | |
} | |
} | |
} | |
class SharedValue { | |
public static AtomicInteger number = new AtomicInteger(0); | |
public AtomicInteger getContents() { return number; } | |
public void increment() { number.incrementAndGet(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment