Вот часть моего кода:выполнения в Java
Thread refreshHistory=new Thread(()->{
try{
while(true){
todoDA.refreshHistory();
Thread.sleep(secondsToWait*1000);
}
}catch(Exception e){}
});
Есть ли способ, чтобы убедиться, что todoDA.refreshHistory();
всегда будет выполняться до того поток переходит в спящий режим?
У меня был Abort due to constraint violation (NOT NULL constraint failed: Tag_todo.todo_id)
. В этом коде:
Первый класс:
Todo createdTodo=null;
if(typePicker.getValue().getName().equals("1")){
TodoCalendar todoCalendar=todoDA.createTodoCalendar(lDate,todoTime.toString());
if(newGroup.isSelected()){
todoDA.createTodo(todoNameString, description, importancePicker.getValue(), newGroupObject, timerType, typePicker.getValue(), null, todoCalendar);
}else{
createdTodo=todoDA.createTodo(todoNameString, description, importancePicker.getValue(), groupPicker.getValue(), timerType, typePicker.getValue(), null, todoCalendar);
}
}else if(typePicker.getValue().getName().equals("2")){
TodoWeek todoWeek=todoDA.createTodoWeek(mondayTime, tuesdayTime, wednesdayTime, thursdayTime, fridayTime, saturdayTime, sundayTime);
if(newGroup.isSelected()){
todoDA.createTodo(todoNameString, description, importancePicker.getValue(), newGroupObject, timerType, typePicker.getValue(),todoWeek,null);
}else{
createdTodo=todoDA.createTodo(todoNameString, description, importancePicker.getValue(), groupPicker.getValue(), timerType, typePicker.getValue(),todoWeek, null);
}
}else if(typePicker.getValue().getName().equals("3")){
TodoCalendar todoCalendar=todoDA.createTodoCalendar(lDate,null);
if(newGroup.isSelected()){
todoDA.createTodo(todoNameString, description, importancePicker.getValue(), newGroupObject, timerType, typePicker.getValue(), null, todoCalendar);
}else{
createdTodo=todoDA.createTodo(todoNameString, description, importancePicker.getValue(), groupPicker.getValue(), timerType, typePicker.getValue(), null, todoCalendar);
}
}
/*********************************************************WORK WITH TAGS***************************************************************************/
if(!allTags.equals("")){
for(int i=0;i<tagsList.length;i++){
String readyTag=tagsList[i].trim();
if(!readyTag.equals("")){
readyTag=readyTag.replaceAll("\\s+", " ");//get rid of not needed spaces
todoDA.createConnectTag(readyTag,createdTodo);
}//not empty tag
}//go through all tags
}//end if tags not empty
Второй класс:
public void createConnectTag(String myTag, Todo todo){
Session session=mainApp.getSessionFactory().openSession();
session.beginTransaction();
Tag createdTag=new Tag(tag);
session.save(createdTag);
TagTodo tagTodo=new TagTodo(createdTag, todo);
session.save(tagTodo);
session.getTransaction().commit();
session.close();}
кажется, что мой объект передается в нуль (иногда это работает отлично). Хотя в то время он должен быть уже создан. Если я добавлю несколько System.out.println();
перед вызовом createConnectTag(readyTag,createdTodo);
, все будет работать. Поэтому мне было интересно, как позвонить createConnectTag(readyTag,createdTodo);
после того, как createTodo
закончил выполнение?
В большинстве случаев этот код работает нормально, но иногда я получаю ошибку.
Это уже будет. Почему вы думаете иначе? – pathfinderelite
код внутри вашего цикла находится внутри потока, но не вызывает поточную функцию, todoDA.refreshHistory будет выполнена перед сном. – jeorfevre
Это просто иногда, когда у меня есть 'setFunction();' и после этого 'executeFunction();' Я запускаю 'NullPointerException'. Поэтому я попытался выяснить, как решить проблему. Но я только нашел, как исполнять отдельные потоки по порядку. – Alyona