Mysql simulation queue Java code -- initialization data drop table if exists t_msg_queues; create table t_msg_queues (msg_id bigint unsigned not null AUTO_INCREMENT, msg_content VARCHAR (255) not null, owner_thread_id int not null default-1, primary key (msg_id) ENGINE = innodb default charset = utf8; SET @ maxRandom = POWER (10, 6 ); insert into 't_ msg_queues '('msg _ content') VALUES (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom ))), (CONCAT ("cont _", CEIL (RAND () * @ maxRandom); -- get an unprocessed message set session autocommit = 1; SET @ msgID =-1; UPDATE t_msg_queues SET owner_thread_id = GREATEST (CONNECTION_ID (), (@ msgID: = msg_id) * 0) WHERE owner_thread_id =-1 order by msg_id LIMIT 1; -- If @ msgID is-1, it indicates that there is no message to be processed. Otherwise, it indicates the msg_id to be processed.