Question

In: Computer Science

Write a multithreaded program that tests your solution to HW#1. You will create several threads –...

Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination). On UNIX and Linux systems, sleeping is accomplished through the sleep() function, which is passed an integer value representing the number of seconds to sleep.

This is my HW#1 solution: public class PidManager{ private static final int MIN_PID = 300; private static final int MAX_PID = 5000; private static int[] pidArr; /* *This method is to allocate the *the array to store the pids. *@param none *@return int */ public static int allocate_map(){ pidArr = new int[MAX_PID - MIN_PID]; if (pidArr == null){ System.out.println("Not allocated."); return -1; } for (int i = 0; i < pidArr.length; i++) { pidArr[i] = 0; } System.out.println("Allocated."); return 1; } /* *This method is used to allocates and returns a pid. *@param none *@return int pidNum */ public static int allocate_pid(){ if (pidArr == null){ System.out.println("Pid manager is null "); return -1; } int pidNum = -1; for (int i = 0; i < pidArr.length; i++){ if (pidArr[i] == 0){ pidArr[i] = 1; pidNum = i + MIN_PID; break; } } if (pidNum == -1){ System.out.println("Cannot allocate Pid"); return -1; } System.out.println("Allocated Pid :" + pidNum); return pidNum; } /* *This method is used to release a pid. *@param int num *@return void */ public static void release_pid(int pidNum){ if (pidArr == null){ System.out.println("Pid manager is null. "); return; } if (pidNum < MIN_PID || pidNum > MAX_PID){ System.out.println("Pid is out of range, has to be in range from 300 to 5000"); } int newPid = pidNum - MIN_PID; if (pidArr[newPid] == 0){ System.out.println(pidNum + " is released."); return; } System.out.println("Releasing PID :" + pidNum); pidArr[newPid] = 0; } }

Thank you in advance.

Solutions

Expert Solution

Ans.) Your answer is very messed up. I will give you the code. check it.

Code-

class Multi extends Thread
{
public void run()
{
System.out.println("Thread is running....!");
}

public static void main(String args[])
{
Multi t1=new Multi();
t1.start();
}
}


//Thread example by implementing runnable interfaces
class Multi2 implements Runnable
{
public void run()
{
System.out.println("Thread is running...---");
}
public static void main(String args[])
{
Multi2 m1=new Multi2();
Thread t1=new Thread(m1);
t1.start();
}
}


// Sleep method example
class SleepMethod extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
try{Thread.sleep(500);}catch(InterruptedException e)
{System.out.println(e);}
System.out.print(i);
}
}
public static void main(String args[])
{
SleepMethod t1=new SleepMethod();
SleepMethod t2=new SleepMethod();
t1.start();
t2.start();
}
}
//Join method example
class Multi extends Thread
{
public void run()
{
System.out.println("Running...");
System.out.println(Thread.currentThread().getName());
}
public static void main(String args[])
{
Multi t1=new Multi();
Multi t2=new Multi();
System.out.println("Name of t1: "+t1.getName());
System.out.println("Name of t2: "+t2.getName());

System.out.println("Id of t1: "+t1.getId());
System.out.println("Id of t2: "+t2.getId());
System.out.println("Priority of t1: "+t1.getPriority());
t1.setName("Priyanshu");
t1.start();
System.out.println("Name of t1 after changing: "+t1.getName());
  
}
}


Related Solutions

Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create...
Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination).On UNIX and Linux systems,...
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads: If the current number is divisible by 2, then print HAP If the current number is divisible by 5, then print PY If the current number is divisible by both 2 and 5, then print HAPPY If the number is neither divisible...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
write C program to create 4 threads for summing the numbers between 1 and 40 where...
write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following. The sum from 1 to 10 = 55 The sum from 11 to 20 =...
Write a multithreaded program that calculates various statistical values for a list of numbers. This program...
Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers 90 81 78 95 79 72 85 The program will report The...
C Programming language problem I need to write a program which uses several threads (like 8...
C Programming language problem I need to write a program which uses several threads (like 8 threads for example) to sum up a number. The following program is using 1 thread but I need several threads to compile it. There was a hint saying that using multiple separate for loop and combining them will make a multi-threaded program. #include <stdio.h> #include <stdlib.h> #include <pthread.h> int sum; // this data is shared by the threads void *runner(void *param); // threads call...
Choose only one problem: 1- Write a multithreaded program that calculates various statistical values for a...
Choose only one problem: 1- Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the minimum value, and the third will determine the maximum value. The variables representing the average, minimum, and maximum values will be stored globally. The worker threads...
4. Write a multithreaded program that calculates various statistical values for a list of numbers. This...
4. Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create five separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, the third will determine the minimum value, fourth will determine the median value and the fifth will compute the std. deviation. For example, suppose your program is passed...
HW 8-1a   1.)   Referring to the UML class diagram, create a program that utilizes the Student...
HW 8-1a   1.)   Referring to the UML class diagram, create a program that utilizes the Student class. - id : Integer - units : Integer - name : String + Student ( ) : + Student (id : Int, name : String, units : Int) : + ~Student( ) : + setID(id : Integer) : void + setName(name: String) : void + setUnits(units : Integer ) : void + displayRecord() : void 2.)   Include 3 files: -   Source.cpp -   Student.h...
Multithreading Assignment In this homework, you will implement a multithreaded solution to finding the sum of...
Multithreading Assignment In this homework, you will implement a multithreaded solution to finding the sum of 9,000,000 double values. Begin, by creating a method that creates an array (not an arrayList) of 9000000 double’s and populates each index with a random number. Create a class to hold the sum of all the numbers in the array. Protect access to that sum by using a ReentrantLock appropriately. This means that only methods in this class can access or modify the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT