以下是我的代码的一部分:
Thread refreshHistory=new Thread(()->{
try{
while(true){
todoDA.refreshHistory();
Thread.sleep(secondsToWait*1000);
}
}catch(Exception e){}
});
有没有办法确保{/ 1}}在线程进入睡眠状态之前始终执行?
我有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
似乎我的对象被传递为null(有时它工作正常)。即使那时它应该已经创建了。如果我在调用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);
?
大多数情况下,这段代码运行正常,但有时我会犯错误。
答案 0 :(得分:0)
您的代码将按顺序执行,因此行todoDA.refreshHistory();
将首先执行,然后线程将休眠,
但是当行todoDA.refreshHistory();
开始另一个线程时,第二个线程将在不确定的时间开始
答案 1 :(得分:0)
调用将始终在Thread进入休眠状态之前发生。因为你显然在一个更大的上下文中遇到问题,而不仅仅是一个线程,它取决于你的函数接下来会执行什么。