Skip to content

Instantly share code, notes, and snippets.

@swarawan
Created March 18, 2020 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swarawan/12f196c83fde00e0509cc7c53696c8f4 to your computer and use it in GitHub Desktop.
Save swarawan/12f196c83fde00e0509cc7c53696c8f4 to your computer and use it in GitHub Desktop.
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