Q9 For this problem, you will use threading to speed up a decryption process. Th
ID: 3843685 • Letter: Q
Question
Q9
For this problem, you will use threading to speed up a decryption process. There exists a class called Crypt with a static method decrypt. The decrypt method takes a string and returns a non-zero value if the string can be decrypted successfully and 0 otherwise.
You do not need to implement the decrypt method. Its implementation already exists.
The Crypt class takes and array of string keys and the check methods simply calls decrypt on 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.
Write a class SuperCrypt that is a subclass of Crypt that can use multiple CPUs to fo decryption concurrently. As always, you are free to add ivars, methods, inner class and so on.
In its constructor, SuperCrypt takes an array of String keys to try. (code provided)
The check() methods should fork off 4 worker threads, Collectively, the workers should try to decrypt all the keys. The check() methods should return 0 if all of the decrypts return 0. If a key decrypts successfully(non-zero), then check() should return its non-zero code. check() should return immediately when a non-zero code is found.
When a successful decrypts 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 the completion.
Explanation / Answer
class Crypt {
public static int decrypt(String key) {...}
}
public class SuperCrypt {
private String keys[];
public SuperCrypt(String[] keys) {
this.keys = keys;
}
public int check() {
class Crypt {
private static java.util.Random r = new java.util.Random();
public static int decrypt(String key) {
for (int i=0; i<5; i++) {
System.out.println(Thread.currentThread().getName() + ": Decrypting
key " + key + ", step " + i);
try {
Thread.sleep(r.nextInt(1000));
} catch (InterruptedException e) {}
}
if (key.equals("The Key")) {
return 1;
}
return 0;
}
}
public class SuperCrypt {
private String keys[];
private int keyIdx;
private int code;
private int threadsDone;
public SuperCrypt(String[] keys) {
this.keys = keys;
}
public int check() {
keyIdx = 0;
code = 0;
threadsDone = 0;
for (int i=0; i<4; i++) {
Thread worker = new Thread() {
public void run() {
String key;
while (code==0 && null != (key=getKey()) ) {
int code = Crypt.decrypt(key);
if (code != 0) {
setCode(code);
}
}
threadDone();
}
};
worker.setName("Worker " + i);
worker.start();
}
return getFinalCode();
}
public synchronized int getFinalCode() {
while (code == 0 && threadsDone != 4) {
try {
wait();
} catch (InterruptedException e) {}
}
return code;
}
public synchronized void setCode(int code) {
this.code = code;
notifyAll();
}
public synchronized void threadDone() {
threadsDone ++;
if (threadsDone == 4) {
notifyAll();
}
}
public synchronized String getKey() {
if (keys == null || keyIdx >= keys.length) {
return null;
}
return keys[keyIdx++];
}
static public void main(String[] args) {
String [] keys = {"key1", "key2", "key3", "key4", "key5", "The Key"};
System.out.println("Final result: " + (new SuperCrypt(keys)).check());
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.