Thread.sleep another class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Thread.sleep another class



I have a class, which inserts values in a database (e.g. insertdb.java). But I only want them to be inserted every full hour, so after a little bit of researching and trying different approaches, I tried it with a Thread.sleep. How can I implement this code with the insertdb.java class (not a method)? Thanks in advance for your tips!


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class thread {
public static void main(String args) {

ScheduledExecutorService t = Executors.newSingleThreadScheduledExecutor();
t.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3600000); }
catch (Exception e) {}
}
}, 0, 1, TimeUnit.HOURS);
}
}




1 Answer
1



You can make class insertdb a member field, and call the insert in run method.


insertdb


run



Sample code:


import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleTask {

Insertdb insertdb;

public ScheduleTask(Insertdb insertdb) {
this.insertdb = insertdb;
}

public static void main(String args) {

ScheduledExecutorService t = Executors.newSingleThreadScheduledExecutor();
t.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
insertdb.insert();
Thread.sleep(3600000);
} catch (Exception e) {
}
}
}, 0, 1, TimeUnit.HOURS);
}
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions