我正在尝试创建一个要返回的变量,该变量指示线程是否成功完成其工作 - 但是,它要求该变量是最终变量,这意味着它不能被重新分配。
为了促进这样的事情需要什么
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;
}
谢谢
最佳答案
使用 Runnable 执行以下操作:
public class YourClass implements Runnable{
private boolean yourVariable;
public YourClass(boolean v){
yourVariable = v
}
public void run(){
.....
}
........
}
创建线程时,假设您有一个不是最终变量:
boolean nonFinalVariable = true;
Thread t = new Thread(new YourClass(nonFinalVariable));
关于java - 如何从线程分配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25062434/