In: Computer Science
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.
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());
}
}