Results 1 to 6 of 6

Thread: Task Scheduling in JAVA

  1. #1
    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.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    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();
            }
        }
    }

  3. #3
    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);
        }
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    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.

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    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);
    	}
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    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);	
       }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,567,590.73620 seconds with 17 queries