n Java, Consider a crime wave during two criminals simultaneously commit crimes while two detectives simultaneously  solve the crimes. A crime has an integer seriousness level between 0 (creating a public nuisance) and 4 (murder).   Criminals commit crimes with random seriousness and wait a random amount of time between 0 and 100  milliseconds between crimes.  Each criminal commits 50 crimes and then retires to live from the loot.  Detectives  solve the most serious crimes first, resting for 60 milliseconds after solving each crime.  The application continues running until both the criminals have retired (ie, the two threads containing the criminal Runnables have terminated) and all the crimes have been solved. Here are the first few lines of output from a sample run of the  program: c1 commits a crime of seriousness 1 d1 solves a crime of seriousness 1 c1 commits a crime of seriousness 4 d2 solves a crime of seriousness 4 c2 commits a crime of seriousness 0 c2 commits a crime of seriousness 0 Here are the last few lines of output from a typical run: d1 solves a crime of seriousness 0 d2 solves a crime of seriousness 0 committed: 100 solved: 100 Complete the missing code: package crime; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; public class CrimeWave {     Random r = new Random();          // create the priority queue for the crimes here.  Call it crimes.              Integer committed = 0;     Integer solved = 0;     public void meanStreets() {         Detective d1 = new Detective("d1");         Detective d2 = new Detective("d2");         Criminal c1 = new Criminal("c1");         Criminal c2 = new Criminal("c2");     // create and start the threads (one per criminal and one per detective) here          // write a while loop here that continues as long as at least one of the criminals is     // still in business (ie, which the thread for the criminal is not terminated).  If the     // loop condition is true, use Thread.sleep to wait 200 milliseconds before checking again                    System.out.println("committed: " + committed);         System.out.println("solved: " + solved);     }     public synchronized void commitCrime(Crime c) {          // write one line of code to add the crime to crimes         committed++;     }     public synchronized void solveCrimes(String name) {         try {             while (crimes.isEmpty())                 Thread.sleep(25);     // write code here to remove the next crime from the priority queu crimes and set an int called s to the crime's      // seriousness level             solved++;             System.out.println(name + " solves a crime of seriousness " + s);         } catch (Exception e) {             e.printStackTrace();         }     }     public static void main(String[] args) {         CrimeWave c = new CrimeWave();         c.meanStreets();         System.exit(0);     }     private class Crime implements Comparable { // finish writing Crime here.  Use Random.nextInt(10) to get the random level of seriousness.     }     private class Criminal implements Runnable {         String name;         private Criminal(String nameIn) {             name = nameIn;         }         @Override         public void run() {             for (int i = 0; i < 50; i++) {                 try {                     Thread.sleep(r.nextInt(100));                 } catch (InterruptedException e) {                     e.printStackTrace();                 }                 int s = (r.nextInt(5));                 Crime c = new Crime(s);                 System.out.println(name + " commits a crime of seriousness "                         + s);                 commitCrime(c);             }         }     }     private class Detective implements Runnable {     // finish writing Detective here       } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In Java, Consider a crime wave during two criminals simultaneously commit crimes while two detectives simultaneously 
solve the crimes. A crime has an integer seriousness level between 0 (creating a public nuisance) and 4 (murder).  
Criminals commit crimes with random seriousness and wait a random amount of time between 0 and 100 
milliseconds between crimes.  Each criminal commits 50 crimes and then retires to live from the loot.  Detectives 
solve the most serious crimes first, resting for 60 milliseconds after solving each crime.  The application continues
running until both the criminals have retired (ie, the two threads containing the criminal Runnables have
terminated) and all the crimes have been solved. Here are the first few lines of output from a sample run of the 
program:

c1 commits a crime of seriousness 1
d1 solves a crime of seriousness 1
c1 commits a crime of seriousness 4
d2 solves a crime of seriousness 4
c2 commits a crime of seriousness 0
c2 commits a crime of seriousness 0

Here are the last few lines of output from a typical run:

d1 solves a crime of seriousness 0
d2 solves a crime of seriousness 0
committed: 100
solved: 100

Complete the missing code:

package crime;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

public class CrimeWave {
    Random r = new Random();
    
    // create the priority queue for the crimes here.  Call it crimes.
        
    Integer committed = 0;
    Integer solved = 0;

    public void meanStreets() {
        Detective d1 = new Detective("d1");
        Detective d2 = new Detective("d2");
        Criminal c1 = new Criminal("c1");
        Criminal c2 = new Criminal("c2");

    // create and start the threads (one per criminal and one per detective) here
    
    // write a while loop here that continues as long as at least one of the criminals is
    // still in business (ie, which the thread for the criminal is not terminated).  If the
    // loop condition is true, use Thread.sleep to wait 200 milliseconds before checking again 
    
    
        System.out.println("committed: " + committed);
        System.out.println("solved: " + solved);
    }

    public synchronized void commitCrime(Crime c) {
    
    // write one line of code to add the crime to crimes

        committed++;
    }

    public synchronized void solveCrimes(String name) {
        try {
            while (crimes.isEmpty())
                Thread.sleep(25);

    // write code here to remove the next crime from the priority queu crimes and set an int called s to the crime's 
    // seriousness level

            solved++;
            System.out.println(name + " solves a crime of seriousness " + s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        CrimeWave c = new CrimeWave();
        c.meanStreets();
        System.exit(0);
    }

    private class Crime implements Comparable<Crime> {


// finish writing Crime here.  Use Random.nextInt(10) to get the random level of seriousness.


    }

    private class Criminal implements Runnable {
        String name;

        private Criminal(String nameIn) {
            name = nameIn;
        }

        @Override
        public void run() {
            for (int i = 0; i < 50; i++) {
                try {
                    Thread.sleep(r.nextInt(100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int s = (r.nextInt(5));
                Crime c = new Crime(s);
                System.out.println(name + " commits a crime of seriousness "
                        + s);
                commitCrime(c);
            }
        }

    }

    private class Detective implements Runnable {

    // finish writing Detective here

 

    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Topological Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education