11package com .thealgorithms .searches ;
22
3- import java .util .Scanner ;
4-
3+ /**
4+ * LinearSearchThread is a multithreaded implementation of the linear search algorithm.
5+ * It creates multiple threads to search for a specific number in an array.
6+ *
7+ * <p>
8+ * The class generates an array of random integers, prompts the user to enter a number to search for,
9+ * and divides the array into four segments, each handled by a separate thread.
10+ * The threads run concurrently and search for the specified number within their designated segments.
11+ * Finally, it consolidates the results to determine if the number was found.
12+ * </p>
13+ *
14+ * <p>
15+ * Example usage:
16+ * 1. The program will output the generated array.
17+ * 2. The user will be prompted to input a number to search for.
18+ * 3. The program will display whether the number was found in the array.
19+ * </p>
20+ */
521public final class LinearSearchThread {
622 private LinearSearchThread () {
723 }
8-
9- public static void main (String [] args ) {
10- int [] list = new int [200 ];
11- for (int j = 0 ; j < list .length ; j ++) {
12- list [j ] = (int ) (Math .random () * 100 );
13- }
14- for (int y : list ) {
15- System .out .print (y + " " );
16- }
17- System .out .println ();
18- System .out .print ("Enter number to search for: " );
19- Scanner in = new Scanner (System .in );
20- int x = in .nextInt ();
21- Searcher t = new Searcher (list , 0 , 50 , x );
22- Searcher t1 = new Searcher (list , 50 , 100 , x );
23- Searcher t2 = new Searcher (list , 100 , 150 , x );
24- Searcher t3 = new Searcher (list , 150 , 200 , x );
25- t .start ();
26- t1 .start ();
27- t2 .start ();
28- t3 .start ();
29- try {
30- t .join ();
31- t1 .join ();
32- t2 .join ();
33- t3 .join ();
34- } catch (InterruptedException e ) {
35- }
36- boolean found = t .getResult () || t1 .getResult () || t2 .getResult () || t3 .getResult ();
37- System .out .println ("Found = " + found );
38- in .close ();
39- }
4024}
4125
26+ /**
27+ * The Searcher class extends Thread and is responsible for searching for a specific
28+ * number in a segment of an array.
29+ */
4230class Searcher extends Thread {
31+ private final int [] arr ; // The array to search in
32+ private final int left ; // Starting index of the segment
33+ private final int right ; // Ending index of the segment
34+ private final int x ; // The number to search for
35+ private boolean found ; // Result flag
4336
44- private final int [] arr ;
45- private final int left ;
46- private final int right ;
47- private final int x ;
48- private boolean found ;
49-
37+ /**
38+ * Constructor to initialize the Searcher.
39+ *
40+ * @param arr The array to search in
41+ * @param left The starting index of the segment
42+ * @param right The ending index of the segment
43+ * @param x The number to search for
44+ */
5045 Searcher (int [] arr , int left , int right , int x ) {
5146 this .arr = arr ;
5247 this .left = left ;
5348 this .right = right ;
5449 this .x = x ;
5550 }
5651
52+ /**
53+ * The run method for the thread, performing the linear search in its segment.
54+ */
5755 @ Override
5856 public void run () {
5957 int k = left ;
@@ -65,6 +63,11 @@ public void run() {
6563 }
6664 }
6765
66+ /**
67+ * Returns whether the number was found in the segment.
68+ *
69+ * @return true if the number was found, false otherwise
70+ */
6871 boolean getResult () {
6972 return found ;
7073 }
0 commit comments