Natasha Harrell
Natasha Harrell

Reputation: 49

How can I run my java program in a timed loop?

I am trying to get my program to run every 5 minutes. Can anyone tell me how to achieve this? I'm fairly new to java, here's what I have so far. This is a program that prints processes to a text file and then calls that text file to display its data. It would be nice to have this run every 5 minutes.

import java.io.*;
import java.util.StringTokenizer;


public class getProcesses
{

 private String GetProcessListData()
 {
 Process p;
 Runtime runTime;
 String process = null;
 try {
 System.out.println("Allow me to hack your system and see what you have open...");

 //This will let us access windows in order to make this program possible
 runTime = Runtime.getRuntime();


 p = runTime.exec("tasklist");


 InputStream inputStream = p.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


 String line = bufferedReader.readLine();
 process = "&";
 while (line != null) {
 line = bufferedReader.readLine();
 process += line + "&";
 }

 //As we learned in class, we have to close whatever we open
 bufferedReader.close();
 inputStreamReader.close();
 inputStream.close();

 //The following output is a quote from Portal
 System.out.println("System has been modified. Thankyou and Goodbye.");
 } catch (IOException e) {
 System.out.println("Uh oh, looks like we found an IOE");
 e.printStackTrace();
 }
 return process;
 }

 private void showProcessData()
 {
 try {


 String proc = GetProcessListData();


 OutputStreamWriter outputStreamWriter =
 new OutputStreamWriter(new FileOutputStream("ProcessList.txt"));
 BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

 StringTokenizer st = new StringTokenizer(proc, "&");

 while (st.hasMoreTokens()) {
 bufferedWriter.write(st.nextToken());  
 bufferedWriter.newLine();               
 }

 //This will close the outputStreams
 bufferedWriter.close();
 outputStreamWriter.close();

 } catch (IOException ioe) {
 ioe.printStackTrace();
 }

 }

 @SuppressWarnings("resource")
public static void main(String[] args) throws IOException
 {
 getProcesses gpl = new getProcesses();
 gpl.showProcessData();

 BufferedReader in = new BufferedReader(new FileReader("ProcessList.txt"));

 String line;
 while((line = in.readLine()) != null)
 {
     System.out.println(line);
 }
 }
}

Any help is appreciated, thankyou.

Upvotes: 0

Views: 439

Answers (4)

Shrein
Shrein

Reputation: 291

The easier approach is to make your class implement Callable (or Runnable, if you don't want it to return anything), and then run it using an SchedulerExecutorService.

The call() method should contain the code that will be executed, and given that it doesn't allow any parameters, you should provide them in the constructor.

Runnable yourInstance = new YourClass(param1, param2); // Assuming you don't want to return anything.
ScheduledExecutorService scheduledExecutor = Executors
                    .newSingleThreadScheduledExecutor();
scheduledExecutor.scheduleAtFixedRate(yourClassInstance, 0, 10000,
                    TimeUnit.MILLISECONDS);

Upvotes: 2

NullPointerException
NullPointerException

Reputation: 3804

You can use timer task

Reference

example

Upvotes: 0

krilid
krilid

Reputation: 186

If you are running Linux you could use the cron job scheduler to schedule a recurring invocation of your program. Similar schedulers exist for other OS'es. No need to implement sleeping in your program in this case I think.

Upvotes: 0

75inchpianist
75inchpianist

Reputation: 4102

you should set up a scheduled task (Windows) or cron job (unix) to launch the command "java -jar myprogram.jar" every five minutes.

Upvotes: 4

Related Questions