Given a large file of integers, search for a particular element in it using multi-threading.
Examples:
Input : 1, 5, 7, 10, 12, 14, 15, 18, 20,
22, 25, 27, 30, 64, 110, 220
Output :if key = 20
Key element found
Input :1, 5, 7, 10, 12, 14, 15, 18, 20,
22, 25, 27, 30, 64, 110, 220
Output :if key = 202
Key not present
Prerequisite : Multi-threading
Approach : First create n threads. Then, divide array in to four parts one section for each thread and apply linear search on individual section using multithreading and check whether the key element is present or not.
Command : g++ -pthread linear_thread.cpp
C++
#include <iostream>
#include <pthread.h>
using namespace std;
#define max 16
#define thread_max 4
int a[max] = { 1, 5, 7, 10, 12, 14, 15, 18,
20, 22, 25, 27, 30, 64, 110, 220 };
int key = 202;
int f = 0;
int current_thread = 0;
void * ThreadSearch( void * args)
{
int num = current_thread++;
for ( int i = num * (max / 4);
i < ((num + 1) * (max / 4)); i++) {
if (a[i] == key)
f = 1;
}
}
int main()
{
pthread_t thread [thread_max];
for ( int i = 0; i < thread_max; i++) {
pthread_create(& thread [i], NULL, ThreadSearch,
( void *)NULL);
}
for ( int i = 0; i < thread_max; i++) {
pthread_join( thread [i], NULL);
}
if (f == 1)
cout << "Key element found" << endl;
else
cout << "Key not present" << endl;
return 0;
}
|
Java
import java.util.concurrent.*;
public class Main {
static final int max = 16 ;
static final int thread_max = 4 ;
static int [] a = { 1 , 5 , 7 , 10 , 12 , 14 , 15 , 18 ,
20 , 22 , 25 , 27 , 30 , 64 , 110 , 220 };
static int key = 202 ;
static int f = 0 ;
static int current_thread = 0 ;
static void ThreadSearch( int num)
{
for ( int i = num * (max / 4 );
i < ((num + 1 ) * (max / 4 )); i++) {
if (a[i] == key)
f = 1 ;
}
}
public static void main(String[] args)
{
ExecutorService executor
= Executors.newFixedThreadPool(thread_max);
for ( int i = 0 ; i < thread_max; i++) {
executor.execute( new Runnable() {
public void run()
{
ThreadSearch(current_thread++);
}
});
}
executor.shutdown();
while (!executor.isTerminated()) {
}
if (f == 1 )
System.out.println( "Key element found" );
else
System.out.println( "Key not present" );
}
}
|
Python3
import concurrent.futures
max_val = 16
thread_max = 4
a = [ 1 , 5 , 7 , 10 , 12 , 14 , 15 , 18 , 20 , 22 , 25 , 27 , 30 , 64 , 110 , 220 ]
key = 202
f = 0
def thread_search(num):
global f
for i in range (num * (max_val / / 4 ), (num + 1 ) * (max_val / / 4 )):
if a[i] = = key:
f = 1
break
if __name__ = = '__main__' :
with concurrent.futures.ThreadPoolExecutor(max_workers = thread_max) as executor:
for i in range (thread_max):
executor.submit(thread_search, i)
if f = = 1 :
print ( "Key element found" )
else :
print ( "Key not present" )
|
C#
using System;
using System.Threading;
namespace ThreadSearchDemo
{
class Program
{
const int max = 16;
const int thread_max = 4;
static int [] a = { 1, 5, 7, 10, 12, 14, 15, 18,
20, 22, 25, 27, 30, 64, 110, 220 };
static int key = 202;
static int f = 0;
static int current_thread = 0;
static void ThreadSearch()
{
int num = Interlocked.Increment( ref current_thread) - 1;
for ( int i = num * (max / 4); i < ((num + 1) * (max / 4)); i++)
{
if (a[i] == key)
Interlocked.Exchange( ref f, 1);
}
}
static void Main( string [] args)
{
Thread[] thread = new Thread[thread_max];
for ( int i = 0; i < thread_max; i++)
{
thread[i] = new Thread(ThreadSearch);
thread[i].Start();
}
for ( int i = 0; i < thread_max; i++)
{
thread[i].Join();
}
if (f == 1)
Console.WriteLine( "Key element found" );
else
Console.WriteLine( "Key not present" );
Console.ReadKey();
}
}
}
|
Javascript
const max_val = 16;
const thread_max = 4;
const a = [1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 30, 64, 110, 220];
const key = 202;
let f = 0;
function thread_search(num) {
for (let i = num * (max_val / 4); i < (num + 1) * (max_val / 4); i++) {
if (a[i] === key) {
f = 1;
break ;
}
}
}
for (let i = 0; i < thread_max; i++) {
thread_search(i);
}
if (f === 1) {
console.log( "Key element found" );
} else {
console.log( "Key not present" );
}
|
Output:
Key not present
Exercise: The above code divides array into four subarrays. Extend this to take a parameter that decides number of divisions (or threads).