0

Why does my Thread (Runnable) not get off the run() method once it reaches end of the block?

This is the code:

//1 Entry point for thread with run method
public void run() {
    System.out.println("Hashmaker():/run(). " + threadName + " Running "  );
    try {
        String original = in;
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(original.getBytes());
        byte[] digest = md.digest();
        StringBuffer sb = new StringBuffer();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
            System.out.println("Hashmaker:/run(). original: " + original);
            System.out.println("Hashmaker:/run(). digested(hex):" + sb.toString());
            //src http://www.avajava.com/tutorials/lessons/how-do-i-generate-an-md5-digest-for-a-string.html
        }
            // Let the thread sleep for a while.
            //Thread.sleep(7);
            System.out.println("Hashmaker:/run(). Thread: " + threadName + " recieved a clear password = " + in);
            //Save the hash
            hash=sb.toString();
    }
    /*catch (InterruptedException e) {
        System.out.println("Hashmaker:/run(). Thread " +  threadName + " interrupted.");
    } */
    catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Hashmaker:/run(). Thread " +  threadName + " exiting.");
}

It is ideally supposed to end normally, then my main() method would start other stuff:

public static void main(String[] args) throws Exception {
    //Parse the args
    if (args.length != 1) {
        System.err.println("Main()/: String to MD5 digest should be first parameter\n");
        System.out.println("Main()/: Require now a clear password with 4 char :");
        sc = new Scanner(System.in);
        in = sc.nextLine();
        force = 2;
    }
    else{
        in = args[0];
        force = Integer.parseInt(args[1]);
    }
    //Set the count of Breakers
    count=force;
    //Make the hash
    Hashmaker hm = new Hashmaker(in,"Hashmaker");
    hm.start();
    System.out.println("Main()/: Managing the breakers will start !");

    //Get the hash
    hash=hm.getHash();
    //Manage the Breakers
    f = new File(count);
    for(int i=0; i<count;i++){
        Hashbreaker hb = new Hashbreaker(hash,"HashBreaker"+i);
        f.enfiler(hb);
        hb.start();
    }
}

THE NORMAL SCENARIO TO ME, IS: create and start a thread to generate a digest MD5 hash, get the hash, feed consumer thread with it and bruteforce it till one bruteforce hash give a "compareTo" secret hash == 1.

Sampada
  • 2,931
  • 7
  • 27
  • 39
Blak Oli
  • 1
  • 2
  • 1
    I don't understand why you need a dedicated thread to create the hash, why not simply letting the main thread do it. Could you please clarify? Especially here as you don't have any while loop in your "producer" so it will produce only one hash – Nicolas Filotto Apr 08 '16 at 07:40
  • could you post the code for the Hashbreaker , does it implement a `Runnable` or does it extend `Thread` ?? Also your `Hashmaker ` , which class does it Implement ? because if its again a runnable , it means that when you are calling `hm.getHash();` , the value is null , thus your consumers are feeded with a null value – AntJavaDev Apr 08 '16 at 07:45
  • 1
    "Its supposed to end normally like a kind thread, **then** my Main should start other stuff". Your main thread isn't waiting for `Hashmaker` to finish (unless `getHash()` blocks), and as said, you don't need separate thread if you want the main thread to wait for `Hashmaker` to finish its job. – Kayaman Apr 08 '16 at 08:02
  • @Kayaman Ok right now i'm wrting the technical report and i'm not in the code anymore. But i'll try to remove "getHash()" to see if the thread die after the end of block. – Blak Oli Apr 08 '16 at 09:27
  • @AntJavaDev i can but i think its not usefull to my problem. If hashmaker dont end, hashbreaker is not even call once. – Blak Oli Apr 08 '16 at 09:28
  • @NicolasFillotto mhhh... its my devmethod i will say. Its maybe bad. The point is : separate the logic/strategic stuff from the "Mother"=MD5Main wich need just to manage thread. – Blak Oli Apr 08 '16 at 09:30
  • @BlakOli You should know if `getHash()` is a blocking call or not. Removing it to see what happens sounds like you have no idea what your code does. – Kayaman Apr 08 '16 at 09:31
  • To specifify something dumb : myProgram never reach : `System.out.println("Main()/: Managing the breakers will start !");` So to me the `hm.start();` start() routine just dont want to end... x/ – Blak Oli Apr 08 '16 at 09:32

0 Answers0