Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Task Scheduling in JAVA

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-11-2009
Member
 
Join Date: Jan 2009
Posts: 65
Task Scheduling in JAVA
  

Hi, Can anyone tell me how to perform the task scheduling code in java? I have tried it lot, but not able to get the solution, so please help me in this situation. I am waiting for your reply. Please, give me code regarding this.

Reply With Quote
  #2  
Old 30-11-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Task Scheduling in JAVA

Hi, I don't know how to do it, but on internet I have got the code which I think help you in some extent. Try to learn it. I think it work for you.

Code:
package org.tiling.scheduling;
import java.util.TimerTask;
public abstract class SchedulerTask implements Runnable 
{
    final Object lock = new Object();
    int status = VIRGIN;
    static final int VIRGIN = 0;
    static final int SCHEDULED = 1;
    static final int CANCELLED = 2;
    TimerTask timerTask;
    protected SchedulerTask() 
   {
    }
    public abstract void run();
    public boolean cancel() 
    {
        synchronized(lock) 
       {
            if (timerTask != null)  
            {
                timerTask.cancel();
            }
            boolean result = (state == SCHEDULED);
            status = CANCELLED;
            return result;
        }
    }
    public long scheduledExecutionTime() 
       {
        synchronized(lock) 
        {
         return timerTask == null ? 0 : timerTask.scheduledExecutionTime();
        }
    }
}
Reply With Quote
  #3  
Old 30-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Task Scheduling in JAVA

Hi, from one of the book which I am reading for threading I have got the following code, I think it will give you what you want regarding the task scheduling in java.

Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class URLMonitorPanel extends JPanel implements URLPingTask.URLUpdate 
{
  Timer tmr;
  URL url;
  URLPingTask tsk;
  JPanel status;
  JButton start, stop;
  public taskschedule(String url, Timer t) throws MalformedURLException
 {
    setLayout(new BorderLayout());
    tmr = t;
    this.url = new URL(url);
    add(new JLabel(url), BorderLayout.CENTER);
    JPanel temp = new JPanel();
    status = new JPanel();
    status.setSize(20, 20);
    temp.add(status);
    start = new JButton("Start");
    start.setEnabled(false);
    start.addActionListener(new ActionListener() 
{
      public void actionPerformed(ActionEvent ae) 
{
        makeTask();
        start.setEnabled(false);
        stop.setEnabled(true);
      }
    });
    stop = new JButton("Stop");
    stop.setEnabled(true);
    stop.addActionListener(new ActionListener() 
{
      public void actionPerformed(ActionEvent ae) 
{
        tsk.cancel();
        start.setEnabled(true);
        stop.setEnabled(false);
      }
    });
    temp.add(start);
    temp.add(stop);
    add(temp, BorderLayout.EAST);
    makeTask();
  }
  private void makeTask() 
{
    tsk = new URLPingTask(url, this);
    tmr.schedule(tsk, 0L, 5000L);
  }
  public void isAlive(final boolean b) 
{
    SwingUtilities.invokeLater(new Runnable() 
{
      public void run() 
{
        status.setBackground(b ? Color.GREEN : Color.RED);
        status.repaint();
      }
    });
  }
  public static void main(String[] args) throws Exception 
{
    JFrame frame = new JFrame("URL Monitor");
    Container c = frame.getContentPane();
    c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
    Timer t = new Timer();
    String[] u = new String[]{"http://www.java2s.com","http://www.java2s.com"};
    
    for (int i = 0; i < u.length; i++) 
{
      c.add(new taskschedule(u[i], t));
    }
    frame.addWindowListener(new WindowAdapter() 
{
      public void windowClosing(WindowEvent evt) 
{
        System.exit(0);
      }
    });
    frame.pack();
    frame.show();
  }
 }

class URLPingTask extends TimerTask 
  {
  public interface URLUpdate
 {
    public void isAlive(boolean b);
  }
  URL url;
  URLUpdate updater;
  public URLPingTask(URL url) 
  {
    this(url, null);
  }

  public URLPingTask(URL url, URLUpdate uu) 
  {
    this.url = url;
    updater = uu;
  }

  public void run() {
    if (System.currentTimeMillis() - scheduledExecutionTime() > 5000) 
  {
      return;
    }
    try 
  {
      HttpURLConnection huc = (HttpURLConnection) url.openConnection();
      huc.setConnectTimeout(1000);
      huc.setReadTimeout(1000);
      int code = huc.getResponseCode();
      if (updater != null)
        updater.isAlive(true);
    } catch (Exception e) 
   {
      if (updater != null)
        updater.isAlive(false);
    }
  }
}
Reply With Quote
  #4  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Task Scheduling in JAVA

Hi, you need the source code which will Schedule the task periodically, right? Then use the following code which I have created.

Code:
import java.util.TimerTask;
public class HeartBeatTask extends TimerTask
{
	private int time;
  	public HeartBeatTask(int timeInterval)
	{
    		this.time=timeInterval;
  	}

  	public void run() 
	{
    		// add the task here
 	}
}
In your main program make call to above code to schedule:

Code:
java.util.Timer task = new java.util.Timer();
  	HeartBeatTask timertask = new HeartBeatTask(timeInterval);
 	task.schedule(timertask, 0, timeInterval);
Now the task will repeat after the fixed time interval.
Reply With Quote
  #5  
Old 30-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Task Scheduling in JAVA

Hi, I am giving you the code which will schedule a timer task to run at a certain time and repeatedly. I think this is what you mean by task scheduling in java, right?
Code:
import java.util.*;
import java.io.*;

public class taskschdl
{
	public static void main(String[] args) throws IOException
	{
		int dly = 1000;
		Timer tmr = new Timer();
		System.out.println("What do you want To Do Certain time or Repeat time?");
		System.out.print("Please enter \'C\' or \'R\' for that: ");
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String ans = in.readLine();
		if (ans.equals("C") || ans.equals("c"))
		{
			tmr.schedule(new TimerTask()
			{
				public void run()
				{
					System.out.println("Reply from --------");
				}
			},dly);
		}
		else if(ans.equals("r") || ans.equals("R"))
		{
			tmr.schedule(new TimerTask()
			{
				public void run()
				{
					System.out.println("Reply from --------");
				}
			},dly, 1000);
		}
		else	
		{
			System.out.println("Invalid Entry.");
			System.exit(0);
		}
		System.exit(0);
	}
}
Reply With Quote
  #6  
Old 30-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Task Scheduling in JAVA

Hi friend, I have a simple code for task scheduling in java. I think this will help you to build your own logic.

Code:
import java.util.*;
class Task extends TimerTask 
{  
    int count = 1;
    public void run() 
    {
        System.out.println(count+" : Techarena.in");
	count++;
    }
}
class TaskScheduling 
{
   public static void main(String[] args) 
   {
       Timer tmr = new Timer();   
       tmr.schedule( new Task(), 5000);	
   }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Task Scheduling in JAVA"
Thread Thread Starter Forum Replies Last Post
Need Help with Java Code building easy task xCointoss Software Development 1 26-04-2011 07:51 PM
Scheduled task program in java Aaliya Seth Software Development 4 08-03-2010 01:48 PM
Thread scheduling In Java samualres Software Development 5 12-02-2010 06:50 PM
Scheduling multiple resources on a task to finish quicker counselor Microsoft Project 4 12-08-2009 07:09 PM
Task scheduling : error 0x80070005: Access denied François Miermont Windows Server Help 29 18-04-2005 05:59 PM


All times are GMT +5.5. The time now is 06:35 PM.