-4

I am trying to find element using two threads as soon as 1st thread finds the element it will interrupt other thread i have divided the array into 2 parts (0-length/2) & (length/2 -n) to search over it. But I am not able to achieve the functionality below is my code.

public class Interruptt {

    public static void main(String[] args) {

        int[] arr = { 2, 3, 4, 5, 6, 7, 8, 9 };
        int data = 3;

        MyThread mT = new MyThread(arr, 0, arr.length / 2, data);

        Thread thread1 = new Thread(mT);

        MyThread mT1 = new MyThread(arr, arr.length / 2 + 1, arr.length, data);

        Thread thread2 = new Thread(mT);

        mT.setoT(thread2);
        mT1.setoT(thread1);

        thread1.start();
        thread2.start();

        try {
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            System.out.println("enddd");
        }

        System.out.println("Out");
    }

}

class MyThread implements Runnable {
    int arr[];
    int i;
    int data;
    int end;
    Thread oT;

    public MyThread(int[] arr, int i, int end, int data) {
        this.i = i;
        this.arr = arr;
        this.end = end;
        this.data = data;

    }

    @Override
    public void run() {

        for (int j = i; j < end; j++) {
            System.out.println(Thread.currentThread());
            if (arr[j] == data) {
                System.out.println("data found by thread"
                        + Thread.currentThread());

                oT.interrupt();
                break;
            }

        }
    }

    public void setoT(Thread oT) {
        this.oT = oT;
    }

}
nlogn
  • 1,034
  • 4
  • 11
  • 17
  • The only place where an `InterruptedException` could be thrown is `Thread.sleep`, but both threads sleep for the same time, and you swallow the exception... – Colonel Thirty Two Dec 06 '15 at 18:17
  • 1
    1) why the sleep. 2) you never check the [`isInterrupted()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#isInterrupted--) flag so why do you think calling `interrupt` would do anything at all?? – Boris the Spider Dec 06 '15 at 18:18
  • 1
    Please, check as correct the answer that help you to solve your problem :) – riccardo.cardin Dec 06 '15 at 19:58

3 Answers3

2

When you call the interrupt() method on a Thread, your are sending it a graceful signal that someone outside ask it to stop. In other words, the JVM simply sets a flag called interrupted in the "willing-interrupting" thread to true. The value of flag of the can be read using the isInterrupted() method.

Then, it is up to you to check inside your task / thread that the interrupted thread is set by another thread. You can achive this behaviour sorrounding your task activities in a while loop such this:

while (!done && !this.isInterrupted()) {
    // Do some cool stuff
}

Hope this help :)

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
1

This seems like a really complicated way of running multiple threads to search an array, when Streams seem built for solving this exact problem.

Consider :

int[] arr = {2, 3, 4, 5, 6, 7, 8, 9, 0};
int data = 2;
IntStream.of(arr)
        .parallel()
        .filter(i -> i == data)
        .findAny()
        .ifPresent(i -> System.out.println("Found"));
WillShackleford
  • 6,918
  • 2
  • 17
  • 33
0

mT is thread1. mT1 is thread2 so if oT is thread to interrupt you should set:

mT.setoT(thread2); mT1.setoT(thread1);

instead of

mT.setoT(thread1); mT1.setoT(thread2);

ctomek
  • 1,696
  • 16
  • 35