Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multithread in Java
class BookAgent(private val bookService: BookService) : Thread() {
override fun run() {
val uuid = UUID.randomUUID().toString()
for (n in 0..10) {
sleep(5000)
bookService.save(
BookDataRequestModel(
name = "Book $uuid version: $n",
excerpt = "excerpt",
content = "content",
author = "author",
publisher = "publisher"
)
)
}
}
}
@RestController
@RequestMapping(value = ["/book"])
class BookDataController : BaseController() {
@Autowired
lateinit var bookService: BookService
@PostMapping(value = ["thread"])
@Transactional(rollbackFor = [Exception::class])
fun addUsingThread(): ResponseEntity<ResultResponse> {
BookAgent(bookService).start()
return generateResponse(true).build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment