简体   繁体   中英

How to configure a thread that execute on a given time

I am making a meeting broadcast application which will broadcast the message to the participant of meeting. There is a meeting data checker thread which execute after 5 minute. I have done this using this code :

 ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
 scheduledExecutorService.scheduleWithFixedDelay(new DataAccessSchedule(), 0, 5,TimeUnit.MINUTES);

Now the in DataAccessScedule the query get the data of meeting which are expected to be started after 15 minutes,so I get the data before 15 minutes and the time in table are: 01:45 ,... 01:50 , ... 01:52 ,..

so i have calculated the time of all in millisecond by taking difference from current time so now the time in milis are x,y and z. I want to fire the thread on x , y and z time and these thread destroy itself after execution.I do not want to run scheduler here like above did using Executors. Please tell me how should i do that ?

You need a scheduler library for that, for example Quartz Scheduler , that supports cron expressions to specify exactly when the task should be launched.

You can also configure it to run the task finite number of times.

An alternative for writing sheduler library would be to write the similar functionality yourself. You'll need one task running periodically, with the precision you require (it could be 1 minute, but also 1 second) and checking all entries in the data structure, if their start condition matches current date (for example, by matching the date/time against cron expression). It would be, however, reinventing the wheel so don't do as long if you don't want some extra funktionality the existing libraries doesn't provide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM