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.
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.
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(); } } }
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); } } }
Hi, you need the source code which will Schedule the task periodically, right? Then use the following code which I have created.
In your main program make call to above code to schedule: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 } }
Now the task will repeat after the fixed time interval.Code:java.util.Timer task = new java.util.Timer(); HeartBeatTask timertask = new HeartBeatTask(timeInterval); task.schedule(timertask, 0, timeInterval);
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); } }
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); } }
Bookmarks