A cikin duniyar yau, sarrafa lokaci yana taka muhimmiyar rawa, musamman a duniyar shirye-shirye, inda galibi ana sa ran za mu samar da ingantacciyar code. Manufar jira na ɗan lokaci kafin aiwatar da takamaiman aiki ya zama ruwan dare gama gari. A cikin Java, ana amfani da hanyar `Thread.sleep()` don ƙirƙirar jinkiri; duk da haka, akwai wasu hanyoyin da suka fi dacewa don cimma wannan. A cikin wannan labarin, za mu shiga cikin amfanin ƙirƙirar jinkirin lokaci a cikin Java, musamman mai da hankali kan ''ScheduledExecutorService'. Sannan za mu tattauna wasu dakunan karatu da ayyuka masu alaƙa da wannan batu.
Gabatarwa
'ScheduledExecutorService' babban aji ne don tsarawa da aiwatar da ayyuka akan zaren guda ɗaya ko tafkin zare lokaci-lokaci. Yana ba da hanyoyi masu dacewa da yawa don gudanar da ayyuka, kamar `schedule`, `scheduleAtFixedRate`, da `scheduleWithFixedDelay`. Waɗannan hanyoyin suna ba masu haɓaka damar ƙirƙirar zaren da za su iya aiwatar da ayyuka lokaci-lokaci ko bayan wani ɗan lokaci.
Maganin Matsala
Bari mu ɗauka muna son ƙirƙirar aikin da ke gudana bayan jira na daƙiƙa uku. Za mu iya cimma wannan ta amfani da 'ScheduledExecutorService'. Da farko, dole ne mu shigo da darussan da suka dace daga kunshin `java.util.concurrent`.
import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;
Bayan haka, muna ƙirƙiri misalin 'ScheduledExecutorService' ta amfani da hanyar'Executors.newScheduledThreadPool()'.
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
Yanzu za mu iya ƙirƙirar ɗawainiya da ke gudana bayan jira na daƙiƙa uku.
Runnable task = () -> System.out.println("Task executed after waiting for 3 seconds."); scheduledExecutorService.schedule(task, 3, TimeUnit.SECONDS);
Tabbatar cewa an rufe 'ScheduledExecutorService' bayan an aiwatar da aikin don guje wa ɗumbin albarkatu.
scheduledExecutorService.shutdown();
Bayanin mataki-mataki na Code
- Da farko, shigo da darussan da suka dace daga kunshin `java.util.concurrent`.
- Ƙirƙiri misali na ajin 'ScheduledExecutorService' ta amfani da hanyar'Executors.newScheduledThreadPool()'.
- Ƙayyade aikin 'Runnable' wanda zai yi aikin da ake so.
- Yi amfani da hanyar 'schedule()' na'ScheduledExecutorService' don saita jinkiri don aiwatar da aikin.
- Kashe 'ScheduledExecutorService' bayan an aiwatar da aikin. Wannan yana da mahimmanci don hana kwararar albarkatun ƙasa.
Dakunan karatu masu alaƙa da Ayyuka
Abubuwan da ake tsammanin Selenium WebDriver
Selenium sanannen ɗakin karatu ne mai sarrafa kansa na yanar gizo don Java. Selenium WebDriver yana goyan bayan kula da jira ta amfani da 'Hararan da ake tsammani' kuma yana jira. Ana iya amfani da waɗannan don inganta rubutun gwaji, yin su mafi inganci da inganci.
Apache Commons Lang's StopWatch
Laburaren Apache Commons Lang yana ba da amfani mai amfani da aka sani da 'StopWatch' wanda zai iya auna lokacin da ya wuce a Java. Ana iya amfani da wannan kayan aiki a cikin yanayin yanayi inda mutum ke buƙatar yin benchmarking ko duba aikin toshe lambar.
Masu ƙidayar lokaci da Ayyukan lokaci a Java
Wani madadin ƙirƙirar ayyuka da aka tsara a Java shine ta yin amfani da azuzuwan 'Timer' da 'TimerTask'. Ajin 'Timer' yana ba da kayan aiki don tsara ''TimerTask' don aiwatarwa a ƙayyadadden tazara ko bayan ƙayyadadden jinkiri. Duk da yake ƙasa da sassauƙa fiye da 'ScheduledExecutorService', yana aiki azaman zaɓi mai dacewa don ayyuka masu sauƙi da aka tsara a Java.
A ƙarshe, jiran takamaiman lokaci kafin aiwatar da wani yanki ana buƙatar sau da yawa a cikin yanayin shirye-shirye da yawa. Java yana ba da abubuwan amfani daban-daban, kamar 'ScheduledExecutorService', 'Timer', da 'TimerTask', don cika wannan aikin. Fahimtar amfani da su da kuma yadda za su iya haɓakawa da haɓaka aikin lamba yana da mahimmanci ga masu haɓakawa waɗanda ke aiki akan aikace-aikace masu saurin lokaci.