0

I am trying to create a variable to return which indicates whether a thread was successful in doing its job or not - however, it requires the variable be final which means it cannot be re assigned.

What is needed in order to facilitate something such as this

public static void sendFeedbackEmail(final String user, final FeedbackRating rating,    final String feedbackText, final EmailAddress emailToSendTo) {
    if (rating != null && !StringUtilities.stringEmptyOrNull(user)) {
        boolean result = false;
        Thread emailSender = new Thread(new Runnable() {
            @Override
            public void run() {
                feedbackText = (StringUtilities.stringEmptyOrNull(feedbackText)) ? "N/A" : feedbackText;
                String message = FEEDBACK_MESSAGE.replace("?", rating.getValue())   + user + " Text: " + feedbackText;
                String emailAddress = (emailToSendTo == null) ? USER_FEEDBACK_EMAIL_ADDRESS : emailToSendTo.getValue();
                try {
                    sendVimbaMessage(user, emailAddress, message);
                } catch (MessagingException e) {
                    result = false;
                }
                result = false;
            }
        });
        emailSender.setDaemon(true);
        emailSender.start();
    }
    return false;
}

Thanks

Biscuit128
  • 5,218
  • 22
  • 89
  • 149
  • What's wrong with returning a boolean? Also, you appear to be trying to return false from a method with a void return type. – Rudi Kershaw Jul 31 '14 at 15:08

3 Answers3

1

Do with a Runnable:

public class YourClass implements Runnable{

    private boolean yourVariable;

    public YourClass(boolean v){
       yourVariable = v
    }

    public void run(){
       .....
    }

    ........

}

And when creating the thread, say you have a variable which is not final:

boolean nonFinalVariable = true;
Thread t = new Thread(new YourClass(nonFinalVariable));
betteroutthanin
  • 7,148
  • 8
  • 29
  • 48
0

How I get around this issue myself is by creating a class dedicated to sharing variable among threads. I do this for my GUI Programs, I got one thread reading from devices over socket and then my GUI window.

public class SharedVar{
    private int int1;
    private boolean isThreadADone;

    public SharedVar(){
        int1 = 0;
        isThreadADone = True;
    }

    public void setInt1(int value){
        int1 = value;
    }
    public int getInt1(){
        return int1;
    }
    public void setIsThreadADone(boolean value){
        isThreadADone = value;
    }
    public boolean isThreadADone(){
        return isThreadADone;
    }

}

when you do this. U must hand this class to everything u want values to be shared with.

SharedVar globalVar = new SharedVar();

All threads needs this "globalVar" for this to work.

Dave P
  • 156
  • 1
  • 8
0

There are more than 1 ways to achieve this.

1) You can pass a pojo which has a boolean variable as field which you can get/set even if your pojo reference is declared as final. 2) You can use Callable instead of Runnable to return status.

Another way would be to update status in a table or file, to indicate error failure.

Community
  • 1
  • 1
NRJ
  • 1,064
  • 3
  • 15
  • 32