Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hfdst4;
- import java.util.*;
- class ZoekGetal extends Thread {
- protected int startpositie;
- protected int eindpositie;
- protected int getal;
- protected int[] rij;
- protected ArrayList<Integer> resultaten;
- public ZoekGetal(int startpositie, int eindpositie, int getal, int[] rij, ArrayList<Integer> resultaten) {
- this.startpositie = startpositie;
- this.eindpositie = eindpositie;
- this.getal = getal;
- this.rij = rij;
- this.resultaten = resultaten;
- }
- public void run() {
- for(int i = this.startpositie; i < this.eindpositie; i++) {
- if(rij[i] == this.getal) {
- this.resultaten.add(i);
- }
- try {
- Thread.sleep(10);
- } catch(InterruptedException e) {}
- }
- }
- }
- class TestZoekGetal {
- public static void main(String[] args) {
- int aantalThreads = 8;
- int N = 1000;
- int m = 10;
- ZoekGetal[] z = new ZoekGetal[aantalThreads];
- ArrayList[] r = new ArrayList[aantalThreads];
- int[] a = maakRij(N, m);
- // added
- int interval = N/aantalThreads;
- //
- for(int i=0;i<z.length;i++) {
- r[i] = new ArrayList();
- // z[i] = new ZoekGetal(0, N, 3, a, r[i]);
- z[i] = new ZoekGetal(i*interval, (i+1)*interval, 3, a, r[i]); // added
- }
- for(int i=0;i<z.length;i++) {
- z[i].start();
- }
- // added
- /*
- boolean done = false;
- while(!done) {
- done = true;
- for (int i = 0; i < z.length; i++) {
- if (z[i].isAlive()) {
- done = false;
- break;
- }
- }
- }
- */
- // or
- for (ZoekGetal zoek : z) {
- try {
- zoek.join();
- } catch(InterruptedException e) {
- e.printStackTrace();
- }
- }
- //
- for(int i=0;i<z.length;i++){
- System.out.println(""+i+":"+r[i]);
- }
- }
- public static int[] maakRij(int n, int m){
- int[] r = new int[n];
- for(int i=0;i<r.length;i++){
- r[i] = (int)(m*Math.random());
- }
- return r;
- }
- }
Add Comment
Please, Sign In to add comment