因此,当我访问一个enpoint(POST请求)时,我首先检查数据库中是否已经存在一个条目。如果是,我希望将其返回给用户(用于跟踪目的)并继续操作。但是我想将id返回给用户并继续这个过程。如何做到这一点?
@RestController
public class StudentController {
@Autowired
private StudentService service;
@PostMapping("/storeInDB")
@ResponseBody
public File_Tracking saveStudentDetails(@RequestBody Student student ) throws IOException {
List<Students> student = new ArrayList<>();
int continue = 0;
int id = 0;
id = service.getId(); // I want to return this and continue some other process which follows发布于 2020-03-07 21:08:54
除了的答案之外,由于这两个操作应该相互跟随(首先应该将id返回给用户,然后操作应该包含),所以您应该考虑调度不同的线程。通常,计算机会随机地给线程分配时钟时间。
例如,如果我们有线程t1和线程t2,并且这些t1打印“嗨”到控制台,t2打印“哇!”在控制台上,启动程序时,您可以在控制台中看到以下内容之一:
嘿哇!哇!
或
哇!嘿嘿哇!哇!
因此,为了达到您的目标,您应该搜索线程同步。此外,您还可以访问站点"https://www.baeldung.com/java-thread-safety“,以了解Java中线程的同步。
此外,在完成此任务之前,我建议您阅读有关线程和进程的内容,因为它们是计算机科学中非常重要的主题。"What is the difference between a process and a thread?“将是一个很好的起点。
https://stackoverflow.com/questions/60564790
复制相似问题