Write this program in java This program involves 20 threads

Write this program in java.

This program involves 20 threads. All threads access a common (shared) data structure: An ARRAY with 100 strings. Each thread sometimes replaces a string and (other times) searches the ARRAY for some string, according to the specifications below.

At the start of the program: Generate a POOL of 125 strings (each small string consists of random upper case letters with random length). Randomly select 100 strings from the POOL to fill the ARRAY. It is possible that POOL & ARRAY contain duplicate strings. Then, start the 20 threads.

Each thread uses a random integer OPERATION to select its operation on the ARRAY, as follows:
1) Search the array for some randomly chosen string from the pool and count the number of occurence of this string in the ARRAY, when the value of OPERATION is between 0 & 1999 (i.e. ocassionally a search fails)
2) When OPERATION is between 2000 & 2500, randomly select a string from the POOL, search & replace this string (first occurence) in the ARRAY with a second randomly chosen string from the POOL.
Each thread executes a total of 500 random operations.

Because ARRAY continues to mutate, this problem exhibits critical sections. That is, the 20 threads must be properly coordinated with lock(s), i.e., ReentrantLock. Select a sufficient locking strategy for the data structure to produce a desirable, possibly parallel, software.

Moreover, statistics on the delays of each operation by each thread is to be recorded & reported. That is, record the amount of time that each thread has to wait (for other thread(s)) before it can search for its string in the ARRAY & the amount of time that each thread has to wait (for other thread(s)) before it can replace a string in the ARRAY. Tabulate your data (with proper format) for each thread and calculate the average and standard deviation (find formulas in the Internet if necessary) of \"wait\" time per search operation & \"wait\" time per replace operation by each thread.

Solution

#define _REENTRANT
#include <stdio.h>
#include <thread.h>

/* Function prototypes for thread routines */
void *sub_a(void *);
void *sub_b(void *);
void *sub_c(void *);
void *sub_d(void *);
void *sub_e(void *);
void *sub_f(void *);

thread_t thr_a, thr_b, thr_c;

void main()
{
thread_t main_thr;

main_thr = thr_self();
printf(\"Main thread = %d\ \", main_thr);

if (thr_create(NULL, 0, sub_b, NULL, THR_SUSPENDED|THR_NEW_LWP, &thr_b))
fprintf(stderr,\"Can\'t create thr_b\ \"), exit(1);

if (thr_create(NULL, 0, sub_a, (void *)thr_b, THR_NEW_LWP, &thr_a))
fprintf(stderr,\"Can\'t create thr_a\ \"), exit(1);

if (thr_create(NULL, 0, sub_c, (void *)main_thr, THR_NEW_LWP, &thr_c))
fprintf(stderr,\"Can\'t create thr_c\ \"), exit(1);

printf(\"Main Created threads A:%d B:%d C:%d\ \", thr_a, thr_b, thr_c);
printf(\"Main Thread exiting...\ \");
thr_exit((void *)main_thr);
}

void *sub_a(void *arg)
{
thread_t thr_b = (thread_t) arg;
thread_t thr_d;
int i;

printf(\"A: In thread A...\ \");

if (thr_create(NULL, 0, sub_d, (void *)thr_b, THR_NEW_LWP, &thr_d))
fprintf(stderr, \"Can\'t create thr_d\ \"), exit(1);

printf(\"A: Created thread D:%d\ \", thr_d);

/* process
*/
for (i=0;i<1000000*(int)thr_self();i++);
printf(\"A: Thread exiting...\ \");
thr_exit((void *)77);
}

void * sub_b(void *arg)
{
int i;

printf(\"B: In thread B...\ \");

/* process
*/

for (i=0;i<1000000*(int)thr_self();i++);
printf(\"B: Thread exiting...\ \");
thr_exit((void *)66);
}

void * sub_c(void *arg)
{
void *status;
int i;
thread_t main_thr, ret_thr;

main_thr = (thread_t)arg;

printf(\"C: In thread C...\ \");

if (thr_create(NULL, 0, sub_f, (void *)0, THR_BOUND|THR_DAEMON, NULL))
fprintf(stderr, \"Can\'t create thr_f\ \"), exit(1);

printf(\"C: Join main thread\ \");

if (thr_join(main_thr,(thread_t *)&ret_thr, &status))
fprintf(stderr, \"thr_join Error\ \"), exit(1);

printf(\"C: Main thread (%d) returned thread (%d) w/status %d\ \", main_thr, ret_thr, (int) status);

/* process
*/

for (i=0;i<1000000*(int)thr_self();i++);
printf(\"C: Thread exiting...\ \");
thr_exit((void *)88);
}


void * sub_d(void *arg)
{
thread_t thr_b = (thread_t) arg;
int i;
thread_t thr_e, ret_thr;
void *status;

printf(\"D: In thread D...\ \");

if (thr_create(NULL, 0, sub_e, NULL, THR_NEW_LWP, &thr_e))
fprintf(stderr,\"Can\'t create thr_e\ \"), exit(1);

printf(\"D: Created thread E:%d\ \", thr_e);
printf(\"D: Continue B thread = %d\ \", thr_b);

thr_continue(thr_b);
printf(\"D: Join E thread\ \");

if(thr_join(thr_e,(thread_t *)&ret_thr, &status))
fprintf(stderr,\"thr_join Error\ \"), exit(1);

printf(\"D: E thread (%d) returned thread (%d) w/status %d\ \", thr_e,
ret_thr, (int) status);

/* process
*/

for (i=0;i<1000000*(int)thr_self();i++);
printf(\"D: Thread exiting...\ \");
thr_exit((void *)55);
}


void * sub_e(void *arg)
{
int i;
thread_t ret_thr;
void *status;

printf(\"E: In thread E...\ \");
printf(\"E: Join A thread\ \");

if(thr_join(thr_a,(thread_t *)&ret_thr, &status))
fprintf(stderr,\"thr_join Error\ \"), exit(1);

printf(\"E: A thread (%d) returned thread (%d) w/status %d\ \", ret_thr, ret_thr, (int) status);
printf(\"E: Join B thread\ \");

if(thr_join(thr_b,(thread_t *)&ret_thr, &status))
fprintf(stderr,\"thr_join Error\ \"), exit(1);

printf(\"E: B thread (%d) returned thread (%d) w/status %d\ \", thr_b, ret_thr, (int) status);
printf(\"E: Join C thread\ \");

if(thr_join(thr_c,(thread_t *)&ret_thr, &status))
fprintf(stderr,\"thr_join Error\ \"), exit(1);

printf(\"E: C thread (%d) returned thread (%d) w/status %d\ \", thr_c, ret_thr, (int) status);

for (i=0;i<1000000*(int)thr_self();i++);

printf(\"E: Thread exiting...\ \");
thr_exit((void *)44);
}


void *sub_f(void *arg)
{
int i;

printf(\"F: In thread F...\ \");

while (1) {
for (i=0;i<10000000;i++);
printf(\"F: Thread F is still running...\ \");
}
}

Write this program in java. This program involves 20 threads. All threads access a common (shared) data structure: An ARRAY with 100 strings. Each thread someti
Write this program in java. This program involves 20 threads. All threads access a common (shared) data structure: An ARRAY with 100 strings. Each thread someti
Write this program in java. This program involves 20 threads. All threads access a common (shared) data structure: An ARRAY with 100 strings. Each thread someti
Write this program in java. This program involves 20 threads. All threads access a common (shared) data structure: An ARRAY with 100 strings. Each thread someti

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site