Suppose you have a MAP of integers of size 100 filled as keys from 1 to 1000 and
ID: 3849826 • Letter: S
Question
Suppose you have a MAP of integers of size 100 filled as keys from 1 to 1000 and values are random integers. Write a Java code to create two threads to empty the MAP from its elements in a random fashion. The first one removes data from the MAP as follows: it generates a random number between 1 and 100 (represents some key in the MAP) and removes the entry of that number (of course it is possible that it generates the random number several times). This thread keeps removing data till it is notified by the second thread. The second thread keeps checking the if there are still elements in the MAP or not, if the MAP becomes empty, it notifies the 1st thread to stop the removing process. As the program ends, it prints how many second this process took, the seconds are calculated by a third thread. Suppose you have a MAP of integers of size 100 filled as keys from 1 to 1000 and values are random integers. Write a Java code to create two threads to empty the MAP from its elements in a random fashion. The first one removes data from the MAP as follows: it generates a random number between 1 and 100 (represents some key in the MAP) and removes the entry of that number (of course it is possible that it generates the random number several times). This thread keeps removing data till it is notified by the second thread. The second thread keeps checking the if there are still elements in the MAP or not, if the MAP becomes empty, it notifies the 1st thread to stop the removing process. As the program ends, it prints how many second this process took, the seconds are calculated by a third thread.Explanation / Answer
Hello, this is code i have cooked up. Tweak it a little to make it work more accuratly.
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class MapMulti {
static Map<Integer,Integer> map=new HashMap<Integer,Integer>();
Random random = new Random();
MapMulti()
{
for(int i=1;i<100;i++)
{
map.put(i,random.nextInt());
}
}
public static void main(String args[]) throws InterruptedException{
MapMulti m = new MapMulti();
Th1 t1 = new Th1();
Th2 t2 = new Th2();
Th3 t3 = new Th3();
t1.start();
t2.start();
t3.start();
if(Th2.flag==1)
{
t1.interrupt();
t3.interrupt();
System.out.println(Th3.elapsedTime);
}
}
}
class Th1 extends Thread{
public void run(){
List<Integer> numbers = IntStream.rangeClosed(1, 101).boxed().collect(Collectors.toList());
Collections.shuffle(numbers);
for(int i=1;i<=100;i++)
MapMulti.map.remove(numbers.get(i));
}
}
class Th2 extends Thread{
static int flag=0;
public void run(){
if(MapMulti.map.isEmpty())
{
flag=1;
}
}
}
class Th3 extends Thread{
static long elapsedTime=0;
public void run(){
long startTime = System.currentTimeMillis();
long total = 0;
for (int i = 0; i < 10000000; i++) {
total += i;
}
long stopTime = System.currentTimeMillis();
elapsedTime= stopTime - startTime;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.