COYG
COYG

Reputation: 1598

Issue with retrieving String from another class that uses a thread

I have a class that calls a method from another class which uses a thread as it is somewhat a intensive task. The Thread is responsible for looking through a dictionary to find a matching word. When a word is found it should/does set a local variable in that class. I can see that it is successfully setting this String because it say's so in the log. However, whenever I try and retrieve this String from another class and set the TextView to the value of this String, Nothing happens.

I'm using the Thread because a lot of frames were being skipped. However, when I don't use the thread it works as it is suppose to(Minus the frames being skipped).

Here is the method with the thread:

public String checkLetters() {
    new Thread(new Runnable() {
        @Override
        public void run() {
    //Finding the directory on SD Card
    File sdcard = Environment.getExternalStorageDirectory();

    //Retrieve the text file
    File file = new File(sdcard,"NewEnglishDictionary.txt");

   try {
        BufferedReader bufferRead = new BufferedReader(new FileReader(file),24396);
        String line; //= "";

        //While no word found keep looping
        while (wholeWordFound == false ) {
            line = bufferRead.readLine();
            Log.d("ResolveWord", "Current Line: " + line);

            wordReturned = workOutWord(line);
            setWord(wordReturned);
        }

       String value = getWord().toString().toLowerCase();

       Log.d("Value of setWord: ", " equals: "+ value);
        bufferRead.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
        }
    }).start();
    return wordReturned;
}

And calling to retrieve the variable that has been set (according to the log)

tv_WordFound.setText(fixAnagram.getWord());

And the method which is suppose to return it:

public String getWord() {

    Log.d("Test", "getWord: " + wordReturned);
    return wordReturned;

}

Is there something I'm missing with threads?

Cheers for any help. Logcat itself gives me no clue to where the error lies.

Upvotes: 0

Views: 32

Answers (2)

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

you need to using callback or interface or asynctask for this case. Because your Thread inside checkLetters method will end after your checkLetters. That means if you call getWord() immediately after calling checkLetters, you can only get the previous checking result.

Upvotes: 2

Thomas Philipp
Thomas Philipp

Reputation: 276

The Memory model of Java does not guarantee, that values set by one thread are immediately visible by another thread. To guarantee this, you either must declare the variable as "volatile" or have a synchronized involved (e.g. have the getter and setter method with the keyword synchronized).

e.g.

private volatile String word;

or

public synchronized  String getWord() { return word; }
public synchronized void setWord(String w) { word = w; }

Upvotes: 0

Related Questions