0

I'm using the ReadData class from https://github.com/iota-community/java-iota-workshop/blob/master/src/main/java/com/iota/ReadData.java to retrieve a message from the Iota Tangle (essentially a distributed Network) via a hash value (the bundlehash).

That's my method:

private String readMessageFromHash(String BundleHash) {
        final String[] s = new String[]{""};
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    s[0] = ReadData.getTMessage(BundleHash);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return s[0];
    }

I need the return value in my next line of code but without multithreading my program crashes. With mutlithreading it sometimes works, but most of time it doesn't work (returns an empty String).

I tried using:

        thread.start();
        try {
            while(s[0].length < 1){}
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return s[0];

but it just loops infinitely.

I think the issue is my program not waiting long enough for a response from the network.

Solaire
  • 15
  • 3

0 Answers0