Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 9: (30 points) For this problem, you will use threading process. There

ID: 3843691 • Letter: Q

Question


Question 9: (30 points) For this problem, you will use threading process. There exists to speed up a decryption decrypt method takes a a class called crypt with a static The string and returns a non-zero value if the decrypted successfully otherwise. string can be You do not need to implement the decrypt method. already exists crypt implementatio simply calls decrypt on takes and array of string keys and the each of those strings, returning immediately if decrypt was successful. See code below. However, the decrypting is a slow process so you want to convert this to a multithreaded program. White a class Supercrypt that's a subclass of crypt that can use multiple CPUs to do decryption concurrently. As always, you are free to add ivars, methods, inner classes, and so on. In its constructor, supercrypt takes an array of string keys to try. (code provided) The check0 method should fork off 4 worker threads. Collectively, the workers should try to decrypt all the keys. The check0 method should return o if all of the decrypts return o. lf a key decrypts successfully (non-zero), then check0 should return its non-zero code Check 0 should return immediately when a non-zero code is found. When a successful decrypt is found, we could interrupt() the other workers. However, we will not do this. It is acceptable to allow the other workers to continue to run to completion. class Crypt String 11 keys public crypt (string keuil

Explanation / Answer


Please find the code below.
There is an exception used here. You have to import that specific Exception in your program OR
you can use generic Exception in the code.

If you have any problems, let me know.

Please rate the answer.

Here check() will fork off 4 worker threads and do the decryption.

public class SuperCrypt {
   private String keys[]; // track the key
  
   private int comptdThreadsCount; // How many threads/workers has finished
   private int keyIndex; // which key is being tried next
   private int code; // final result
  
   public SuperCrypt(String[] keys) {
       this.keys = keys;
   }
  
   public int check() {
       // Reset values.
       comptdThreadsCount = 0;
       keyIndex = 0;
       code = 0;
      
       // Create and start 4 worker threads
       for (int i=0; i<4; i++) {
           Thread worker = new Thread() {
               // Thread code - Anonymous inner class
               public void run() {
                   String key;
                  
                   while (code==0 && null != (key=getKey()) ) {
                       // till we find the key, we will loop
                       int code = Crypt.decrypt(key);
                       // if code is non-zero, wake up the main thread
                       if (code != 0) {
                           setCode(code);
                       }
                   }
                   // Once this worker thread is done, log the same.
                   threadDone();
               }
           };
          
           //Start the thread
           worker.setName("Worker Thread " + i);
           worker.start();
       }

       // Wait for the final result
       return getFinalCode();
   }
  
   // Must be synchronized because of wait()
   public synchronized int getFinalCode() {
       // Till all the worker threads are complete.
       while (code == 0 && comptdThreadsCount != 4) {
           try {
               wait();
           } catch (InterruptedException e) {}
       }
       return code;
   }  

   public synchronized void setCode(int code) {
       this.code = code;
       notifyAll();
   }
  
   // Synchronization done because threadDone is incremented
   public synchronized void threadDone() {
       comptdThreadsCount ++;
       if (comptdThreadsCount == 4) {
           notifyAll();
       }
   }
  
   // Synchronization to serialize the traversal of the keys array
   public synchronized String getKey() {
       if (keys == null || keyIndex >= keys.length) {
           // No keys available or left
           return null;
       }
       return keys[keyIndex++];
   }
  
   // Main entry point
   static public void main(String[] args) {
       String [] keys = {"key1", "key2", "key3", "key4", "key5", "The Key"};
       System.out.println("Final result: " + (new SuperCrypt(keys)).check());
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote