-
November 23rd, 2016, 09:03 PM
#1
Parallel Linear Search
I wasn't sure if I should post it in the Java or Multithreading thread.
So I have to make a Parallel Linear Search program with 2 threads and then return where it was found in the array. the user can see what is in the array and the enter a number.
The problem I am having is, I'm not getting the results I should be getting. When I search for an object in the first half the array I can get bad results.
For example:
11 0 18 3 4 9 1 4 18 19
Enter the search value: 0
Target was not found
Now if I search for in the second half I get
16 17 11 0 6 18 12 4 3 0
Enter the search value: 4
Target was found at 3
Which is correct, but only partially correct. The real location is 8. I attempt to try and add 5 to the location found in the second half within the second thread.
Here is my code
Code:
package ParLin;
import java.util.Scanner;
import java.util.Random;
public class ParLinSearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i; //loop control variable
int[] ranArray, firstHalf ,secondHalf; //declare 3 arrays
//initialize array sizes
ranArray = new int[10];
firstHalf = new int[5];
secondHalf = new int[5];
//RNG
Random ranGen = new Random();
Scanner scan = new Scanner(System.in);
//Build array of random integers
for (i = 0; i < 10; i++){
int ran = ranGen.nextInt(20); //Randomly generates number (0 - 19)
ranArray[i] = ran; //add number to array
System.out.print(ran + " "); //print out array value
}
System.out.println("");
System.out.print("Enter the search value: ");//user prompt
int target = scan.nextInt();
//Split array in half for the threads to search
System.arraycopy(ranArray, 0, firstHalf, 0, 5);
System.arraycopy(ranArray, 5, secondHalf, 0, 5);
//Create 2 threads and start passing over each of the array
Thread1 t1 = new Thread1(firstHalf, target);
t1.start();
Thread1 t2 = new Thread1(secondHalf, target);
t2.start();
try {
t1.join();//bring back the values from thread 1
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
t2.join();//bring back the values from thread 2
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("");
if(t1.getCheck() == 0){//it was found
System.out.println("Target was found at " + t1.getLocation());//print where it was found
}
else if(t2.getCheck() == 0 && t1.getCheck() == 1){
int location2 = t2.getLocation();
location2 += 5;
System.out.println("Target was found at " + location2);//print where it was found
}
if (t1.getCheck() == 1 && t2.getCheck() == 1){//wasn't found
System.out.println("Target was not found");//let the user know
}
}
}
Thread1 class
Code:
package ParLin;
public class Thread1 extends Thread{
private static int check;
private static int location;
public int[] thread1Half;//half of array to search
public int target1;//target value
public Thread1(int[] firstHalf, int target) {
//constructor for firstHalf and target
this.thread1Half = firstHalf;//copy array
this.target1 = target;//copy target
}
public void run(){
check =1;//1 if not found, 0 if found
location = 0;//location of where the value was
for(int i =0; i < 5; i++){
if (thread1Half[i] == target1){//if the value at the current location is there
location = i;//location equal to i
location++;//make the location more realistic than array location
check = 0;//change check to = 0 so can report back it was found
break;//break and return back to the main
}//end if
}//end for
}//end run method
public int getCheck(){
return this.check;
}
public int getLocation(){
return this.location;
}
}//end Thread1 class
-
November 23rd, 2016, 09:15 PM
#2
Re: Parallel Linear Search
Norm
-
November 23rd, 2016, 09:46 PM
#3
Re: Parallel Linear Search
Never mind, i fixed the problem. I made the thread2 class figuring out how I was splitting up the array and then passing the halves over. Basically, it was only taking in the constructor the firstHalf, so when the 2nd time it ran the thread it was overriding the firstHalf with the secondHalf. So it was doing it correctly. Now works. Yay, cheering. Now onto Counter Sort, booo.
Here is the final code:
Code:
package ParLin;
import java.util.Scanner;
import java.util.Random;
public class ParLinSearch {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i; //loop control variable
int[] ranArray, firstHalf ,secondHalf; //declare 3 arrays
//initialize array sizes
ranArray = new int[10];
firstHalf = new int[5];
secondHalf = new int[5];
//RNG
Random ranGen = new Random();
Scanner scan = new Scanner(System.in);
//Build array of random integers
for (i = 0; i < 10; i++){
int ran = ranGen.nextInt(20); //Randomly generates number (0 - 19)
ranArray[i] = ran; //add number to array
System.out.print(ran + " "); //print out array value
}
System.out.println("");
System.out.print("Enter the search value: ");//user prompt
int target = scan.nextInt();
//Split array in half for the threads to search
System.arraycopy(ranArray, 0, firstHalf, 0, 5);
System.arraycopy(ranArray, 5, secondHalf, 0, 5);
//Create 2 threads and start passing over each of the array
Thread1 t1 = new Thread1(firstHalf, target);
t1.start();
Thread2 t2 = new Thread2(secondHalf, target);
t2.start();
try {
t1.join();//bring back the values from thread 1
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
t2.join();//bring back the values from thread 2
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("");
if(t1.getCheck() == 0){//it was found
System.out.println("Target was found at " + t1.getLocation());//print where it was found
}
else if(t2.getCheck() == 0 && t1.getCheck() == 1){
int location2 = t2.getLocation();
location2 += 5;
System.out.println("Target was found at " + location2);//print where it was found
}
if (t1.getCheck() == 1 && t2.getCheck() == 1){//wasn't found
System.out.println("Target was not found");//let the user know
}
}
}
Code:
package ParLin;
public class Thread1 extends Thread{
private static int check;
private static int location;
public int[] thread1Half;//half of array to search
public int target1;//target value
public Thread1(int[] Half, int target) {
//constructor for firstHalf and target
this.thread1Half = Half;//copy array
this.target1 = target;//copy target
}
public void run(){
check =1;//1 if not found, 0 if found
location = 0;//location of where the value was
for(int i =0; i < 5; i++){
if (thread1Half[i] == target1){//if the value at the current location is there
location = i;//location equal to i
location++;//make the location more realistic than array location
check = 0;//change check to = 0 so can report back it was found
break;//break and return back to the main
}//end if
}//end for
}//end run method
public int getCheck(){
return this.check;
}
public int getLocation(){
return this.location;
}
}//end Thread1 class
Code:
package ParLin;
public class Thread2 extends Thread{
private static int check;
private static int location;
public int[] thread2Half;//half of array to search
public int target1;//target value
public Thread2(int[] secondHalf, int target) {
//constructor for secondHalf and target
this.thread2Half = secondHalf;//copy array
this.target1 = target;//copy target
}
public void run(){
check =1;//1 if not found, 0 if found
location = 0;//location of where the value was
for(int i =0; i < 5; i++){
if (thread2Half[i] == target1){//if the value at the current location is there
location = i;//location equal to i
location++;//make the location more realistic than array location
check = 0;//change check to = 0 so can report back it was found
break;//break and return back to the main
}//end if
}//end for
}//end run method
public int getCheck(){
return this.check;
}
public int getLocation(){
return this.location;
}
}//end Thread2 class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|