java programming For this problem, you will use threading to speed up a decrypti
ID: 3582095 • Letter: J
Question
java programming
For this problem, you will use threading to speed up a decryption process. Suppose there is an existing static method int decrypt(String key) that attempts a decryption using the given key. It returns 0 if the decryption was not successful, or a non-zero int code on success. The decryption process is very slow. (You don't have to worry about how decrypt() in implemented) Write a class SuperCrypt 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 check() method should fork off 4 worker threads. Collectively, the workers should try to decrypt all the keys. The check() method should return 0 if all of the decrypts return 0. If a key decrypts successfully (nonzero), then check() should return its non-zero code. Check() 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.Explanation / Answer
PROGRAM CODE:
package morsecodeconversion;
class Crypt
{
public static int decrypt(String key)
{
.....// returns an int
}
}
public class SuperCrypt {
class Threads extends Thread
{
String key;
int result;
Threads(String key)
{
this.key = key;
}
int getResult()
{
return result;
}
@Override
public void run() {
result = Crypt.decrypt(key);
super.run();
}
}
private String keys[];
public SuperCrypt(String keys[])
{
this.keys = keys;
}
public int check()
{
int result = 0;
Threads thread1, thread2 = null, thread3 = null, thread4 = null;
for( int i=0; i<keys.length; i++)
{
thread1 = new Threads(keys[i]);
if(i<keys.length)
thread2 = new Threads(keys[++i]);
if(i<keys.length)
thread3 = new Threads(keys[++i]);
if(i<keys.length)
thread4 = new Threads(keys[++i]);
thread1.run();
thread2.run();
thread3.run();
thread4.run();
if(thread1.getResult() == 0 && thread2.getResult() == 0 && thread3.getResult() == 0 && thread4.getResult() == 0)
{
result = 0;
}else if(thread1.getResult() != 0)
result = thread1.getResult();
else if(thread2.getResult() != 0)
result = thread2.getResult();
else if(thread3.getResult() != 0)
result = thread3.getResult();
else if(thread4.getResult() != 0)
result = thread4.getResult();
}
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.