how to write background scanner thread in java?
In java, a thread will write data into a database table, it will have a column like Last_modified_timestamp. Then another background thread will scan the table to see what is new row there. the scanner wi开发者_运维技巧ll select those new-added and do some logic.
How to write these system ?
Thanks
The background scanner thread can use a timer to check the table for new insertion every X seconds, then you could also make this thread a daemon with the myThread.setDeamon(true), to make run with a lower priority. Unless you need this thread to check for new insertion asap.
The scanner need a "last_timestamp_proceed" attribute, which you will update everytime the scanner select the newly added rows. Your background scanner thread will just have to execute a query on the table with a WHERE clause to get the rows with a "last_modified_timestamp" value after the value of the "last_timestamp_proceed" attribute.
A thing to pay attention too is the logic you wanna run in the scanner thread, maybe your records in the db will be changed while the scanner thread extract them and do some logic, you might need a to lock those records before the scanner thread do some logic, then once the logic done, unlock the rows.
精彩评论