Is there any way to write a program in java, so that its main method schedule (or at a 10-15 min interval) another method to executes it at a particular interval?
You can use Job scheduler for this. i.e.
Quartz Job Scheduler.
Refer this Quartz API
Or
You can use ScheduledExecutorService Java Interface
Refer this Documentation
I think you are looking for the Time class.
See Timer Class API
You can use this class like:
You want to perform a Method every 600 miliseconds. You write:
ActionListener taskPerformer = new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
//Do your stuff
}
};
Timer t = new Timer(600, taskPerfomer);
t.start;
There are more options. This example will be executed once but it can be executed in an interval.
I hope it helps.
Use Scheduled Thread pool executor:
Schedual your worker thread to execute at every 10 Seconds
scheduledThreadPool.schedule(worker, 10, TimeUnit.SECONDS);
1) Class WorkerThread .java
public class WorkerThread implements Runnable{
private String command;
public WorkerThread(String s){
this.command=s;
}
#Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Time = "+new Date());
processCommand();
System.out.println(Thread.currentThread().getName()+" End. Time = "+new Date());
}
private void processCommand() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
#Override
public String toString(){
return this.command;
}
}
2) Class ScheduledThreadPool .java
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
//schedule to run after sometime
System.out.println("Current Time = "+new Date());
for(int i=0; i<3; i++){
Thread.sleep(1000);
WorkerThread worker = new WorkerThread("do heavy processing");
scheduledThreadPool.schedule(worker, 10, TimeUnit.SECONDS);
}
//add some delay to let some threads spawn by scheduler
Thread.sleep(30000);
scheduledThreadPool.shutdown();
while(!scheduledThreadPool.isTerminated()){
//wait for all tasks to finish
}
System.out.println("Finished all threads");
}
}
If your task is not so big, you can use Thread.sleep() method(example 10 iteration with 10 minutes delay):
public static void main(String[] args) throws InterruptedException {
methodOne();
for (int i = 0; i < 10; i++) {
Thread.sleep(600000);
methodTwo();
}
}
Related
I have loop that assign task to ExecutorService with fixed size thread, I want the main program wait for threadPool to free one of its' threads to assign another task to it.
Here is my sample code: in this sample code I want finished! be printed at end and want to use ExecutorService.
public static void main(String[] args) {
ExecutorService ex = Executors.newFixedThreadPool(3);
for(int i=0; i< 100; i++) {
ex.execute(new TestThread(i)); // I want the program wait here for at least one thread to free
}
System.out.println("finished!");
}
private static class TestThread implements Runnable {
private int i;
public TestThread(int i) {
this.i = i;
}
#Override
public void run() {
System.out.println("hi: " + i);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I understand you want for the thread that is submitting a job, to block in the case when there is not a free, readily available worker thread in the executor service. This can be useful to apply back-pressure.
At the core the executor service is "simply" composed of a queue of runnables, and of a pool of worker threads.
You can obtain this behaviour by building an executor service with a work-queue of fixed size (in your case, size one).
In code: (note that, your caller thread will still continue after submitting the last job; it will not wait for that job to be completed)
package stackOv;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class BackPressure {
public static void main(String[] args) {
// this is the backing work queue; in this case, it is of bounded size
ArrayBlockingQueue<Runnable> q = new ArrayBlockingQueue<>(1);
ExecutorService ex = new ThreadPoolExecutor(3, 3, 30, TimeUnit.SECONDS, q,
new ThreadPoolExecutor.CallerRunsPolicy());
for(int i=0; i< 100; i++) {
ex.execute(new TestWork(i));
}
System.out.println("finished!");
}
private static class TestWork implements Runnable {
private int i;
public TestWork(int i) {
this.i = i;
}
#Override
public void run() {
System.out.println("hi: " + i);
try {
Thread.sleep(100);
} catch (InterruptedException e) { e.printStackTrace(); }
}
}
}
All you need is:
ex.awaitTermination();
Below is a unit test which uses ScheduledExecutorService to execute a scheduled runnable every second :
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class ConcurrentRequestSimulator {
private static final int NUMBER_REQUESTS = 4;
#Test
public void testGetToDoList() {
try {
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
scheduler.scheduleAtFixedRate(new RequestThreadInvoker(), 0, 1, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
}
private final class RequestThreadInvoker implements Runnable {
public void run() {
ExecutorService es = Executors.newCachedThreadPool();
for (int i = 1; i <= NUMBER_REQUESTS; i++) {
es.execute(new RequestThread());
}
es.shutdown();
while (!es.isTerminated()) {
}
}
}
private final class RequestThread implements Runnable {
public void run() {
try {
System.out.println("in RequestThread");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
The line System.out.println("in RequestThread"); in RequestThread does not appear to be invoked as no output is displayed to console.
I think the issue is that since the test runs to completion, it causes the scheduler to stop scheduling? Can the test be updated so that scheduler is not terminated and RequestThreadInvoker is invoked repeatedly once per second ?
In RequestThreadInvoker I create a new instance of ExecutorService. Could this be causing the issue ?
Adding Thread.sleep(99000); to end of test causes test to wait 99 seconds which is enough time for test to run.
I am new to the Threading, so if please give me an advice for my case.
I would like create a new thread to do something and I don't care this thread can do complete or not.
I intend to use ExecutorCompletionService to do my job but this class is not suitable for me. It must call take or poll to drain a queue to avoid memory leak. So, this means I must wait until the thread complete. I read this from this question
This is the current code
ExecutorService executor = Executors.newCachedThreadPool();
CompletionService<Entity> completion = new ExecutorCompletionService<>(executor);
DoSomeThingClass doSomething = getInstance();
completion.submit(doSomething);
executor.shutdown();
// Continue to do other job and I don't care whenever doSomeThing is complete.
// However when doSomeThing finish, I don't need to do anything to avoid memory leak
For that reason, please give me an approach for my case and some skeleton code for example.
Thank you so much
You can mark this thread as "Daemon". And when your main thread completed, your app will exit.
public static void main(String[] args)
{
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
} catch(InterruptedException e) {}
System.out.println("Thread 2 is finished");
}
});
t.setDaemon(true);
t.start();
System.out.println("Thread 1 is finished");
}
You can use Spring TaskExecutor, it is very useful to raise a thread to run a task.
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
private class MessagePrinterTask implements Runnable {
private String message;
public MessagePrinterTask(String message) {
this.message = message;
}
public void run() {
System.out.println(message);
}
}
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask("Message" + i));
}
}
}
You can check Spring Task Execution documentation here:
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html
Along with you code your Future concept
Future ft=completion.submit(doSomething);
ft.get(timeOut, TimeUnit.MILLISECONDS);
here you can specify Time to execute Thread if it fail to get execute thread get kill(not 100% sure)means it try to interrupt the thread and try to kill
I can resolve my problem as the following code
public static void main(
String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
executor.execute(new Runnable() {
#Override
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
} finally {
System.out.println("Thread 2 is finished");
}
}
});
executor.shutdown();
System.out.println("Thread 1 is finished");
}
The following example shows an issue in the ScheduledExecutorService. I am scheduling two tasks "1" and "2" running longer than the schedule interval. Task "2" submits another task to execute only once.
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestExecutorFairness {
public static void main(final String[] args) {
final int interval = 200;
final int sleeptime = 600;
final ScheduledExecutorService executor = Executors
.newSingleThreadScheduledExecutor();
// schedule task 1
executor.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(sleeptime);
} catch (final InterruptedException e) {
e.printStackTrace();
}
System.out.println("1");
}
}, interval, interval, TimeUnit.MILLISECONDS);
// schedule task 2
executor.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(sleeptime);
} catch (final InterruptedException e) {
e.printStackTrace();
}
System.out.println("2");
// submit task 3
executor.submit(new Runnable() {
#Override
public void run() {
System.out.println("3");
}
});
}
}, interval, interval, TimeUnit.MILLISECONDS);
}
}
The output I would expect is something like
1
2
1
2
3
But it is not executed that way. The task "3" is delayed very long but I need it to be executed ASAP.
Is there any way to change this behaviour to more fairness? Or has someone a better solution?
Interesting. It seems counterintuitive, because the JvaDoc of ScheduledExecutorService explicitly mentions
Commands submitted using the Executor.execute(java.lang.Runnable) and ExecutorService submit methods are scheduled with a requested delay of zero
So one could assume that it should be feasible to submit commands like this. But in this case, there are some peculiarities. I can't point my finger at THE exact reason for this behavior, but it's obviously related to
The tasks taking longer than the schedule interval
The new task being submitted from an executed task
The fact that the ScheduledExecutorService internally uses a DelayedWorkQueue
Most importantly: That you are using a single-threaded ScheduledExecutorService
A considerable problem might also be that this is filling up the work queue and will sooner or later lead to an OutOfMemoryError. This can also be seen in this (slightly adjusted) example:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class TestExecutorFairness {
public static void main(final String[] args) {
final int interval = 200;
final int sleeptime = 600;
final ScheduledExecutorService executor =
Executors.newScheduledThreadPool(1);
final long start = System.currentTimeMillis();
// schedule task 1
executor.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(sleeptime);
} catch (final InterruptedException e) {
e.printStackTrace();
}
System.out.println("1 at "+(System.currentTimeMillis()-start));
}
}, interval, interval, TimeUnit.MILLISECONDS);
// schedule task 2
executor.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(sleeptime);
} catch (final InterruptedException e) {
e.printStackTrace();
}
System.out.println("2 at "+(System.currentTimeMillis()-start));
System.out.println("Submitting 3 to "+executor);
// submit task 3
executor.submit(new Runnable() {
#Override
public void run() {
System.out.println("3 at "+(System.currentTimeMillis()-start));
}
});
}
}, interval, interval, TimeUnit.MILLISECONDS);
}
}
The number of "queued tasks" in the Executor constantly increases.
A solution in this case is rather simple: Instead of a
Executors.newScheduledThreadPool(1)
you can just create a
Executors.newScheduledThreadPool(3)
Of course, this changes the "timing behavior" in this example. I have to assume that the Thread.sleep() in this example was solely intended to simulate a complex computation that did not fit into this example code. But maybe just making sure that the number of threads is at least numberOfPeriodicTasks+1 can also be applied in your real application.
I have a ScheduledThreadPoolExecutor with which I schedule a task to run at a fixed rate. I want the task to be running with a specified delay for a maximum of say 10 times until it "succeeds". After that, I will not want the task to be retried. So basically I'll need to stop running the scheduled task when I want it to be stopped, but without shutting down the ScheduledThreadPoolExecutor. Any idea how I'd do that?
Here's some pseudocode -
public class ScheduledThreadPoolExecutorTest
{
public static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(15); // no multiple instances, just one to serve all requests
class MyTask implements Runnable
{
private int MAX_ATTEMPTS = 10;
public void run()
{
if(++attempt <= MAX_ATTEMPTS)
{
doX();
if(doXSucceeded)
{
//stop retrying the task anymore
}
}
else
{
//couldn't succeed in MAX attempts, don't bother retrying anymore!
}
}
}
public void main(String[] args)
{
executor.scheduleAtFixedRate(new ScheduledThreadPoolExecutorTest().new MyTask(), 0, 5, TimeUnit.SECONDS);
}
}
run this test, it prints 1 2 3 4 5 and stops
public class ScheduledThreadPoolExecutorTest {
static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(15); // no
static ScheduledFuture<?> t;
static class MyTask implements Runnable {
private int attempt = 1;
public void run() {
System.out.print(attempt + " ");
if (++attempt > 5) {
t.cancel(false);
}
}
}
public static void main(String[] args) {
t = executor.scheduleAtFixedRate(new MyTask(), 0, 1, TimeUnit.SECONDS);
}
}
Nicely cancelled outside of thread:
public class ScheduleTest {
#Test
public void testCancel() throws Exception {
final ScheduledThreadPoolExecutor EXECUTOR = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(2);
ScheduledFuture f1 = EXECUTOR.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
System.out.println("Im alive 1");
}
}, 0, 1, TimeUnit.SECONDS);
ScheduledFuture f2 = EXECUTOR.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
System.out.println("Im alive 2");
}
}, 0, 2, TimeUnit.SECONDS);
Thread.sleep(10000);
f1.cancel(true);
System.out.println("f1 cancel");
Thread.sleep(10000);
f2.cancel(false);
System.out.println("f2 cancel");
Thread.sleep(10000);
}
}
Sometimes thread couldn't be cancelled, this solved usually via volatile boolean isCancelled;
CountDownLatch is an alternative approach. When the thread completes, call countDown() on the latch. The calling thread calls latch.await() until all threads complete. At that point call ExecutorService.shutdownNow() so that your main thread doesn't turn into a zombie.
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPoolExecutorTest {
static int i = 0;
public static void main(String[] args) throws Exception {
final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
final CountDownLatch latch = new CountDownLatch(1);
executor.scheduleAtFixedRate(() -> {
System.out.println(++i);
if (i > 4) {
latch.countDown();
}
}, 0, 100, TimeUnit.MILLISECONDS);
latch.await();
executor.shutdownNow();
}
}