简体   繁体   中英

Executing method 1 to N times per second

Let's take the following execution example:

MyRequest request = new MyRequest(args);
request.execute(params);

How can I perform the above 1 to n times (ie n=50 ) per second?

Edit

Furthermore, if we have i objects, each of which call n requests:

for(MyObject obj : objects) {
    // Execute n requests (i.e. in for loop)
}

How can I ensure that the execution happens within one second?

To ensure that n requests are executed in 1 Second you would have to know how long 1 execution lasts to run them sequential, otherwise you should use Threads to run them in parralel and start them with a delay to exactly fit 1 Second

for(int i=0;i<n;i++){
   MyRequest request = new MyRequest(args);
   Thread th=new Thread(()-> request.execute());
   th.start();
   Thread.sleep(1000/n);
}

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