By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

adding task to list stopping a task getting the result of a task

oll3i
100+
P: 679
Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.concurrent.*;
  7. import java.lang.reflect.*;
  8.  
  9. public class Exec1 extends JFrame implements ActionListener {
  10.  
  11.   int k = 0;
  12.   int n = 15;
  13.   JTextArea ta = new JTextArea(40,20);
  14.   List<Future> taskList= new ArrayList<Future>();
  15.   Exec1() {
  16.     add(new JScrollPane(ta));
  17.     JPanel p = new JPanel();
  18.     JButton b = new JButton("Start");
  19.     b.addActionListener(this);
  20.     p.add(b);
  21.     b = new JButton("Stop current");
  22.     b.setActionCommand("Stop");
  23.     b.addActionListener(this);
  24.     p.add(b);
  25.     b = new JButton("Curent result");
  26.     b.setActionCommand("Result");
  27.     b.addActionListener(this);
  28.     p.add(b);
  29.     b = new JButton("Shutdown");
  30.     b.addActionListener(this);
  31.     p.add(b);
  32.     b = new JButton("ShutdownNow");
  33.     b.addActionListener(this);
  34.     p.add(b);
  35.     add(p, "South");
  36.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.     pack();
  38.     setVisible(true);
  39.   }
  40.  
  41.   public void actionPerformed(ActionEvent e)  {
  42.     String cmd = e.getActionCommand();
  43.     try {
  44.       Method m = this.getClass().getDeclaredMethod("task"+cmd);
  45.       m.invoke(this);
  46.     } catch(Exception exc) { exc.printStackTrace(); }
  47.   }
  48.  
  49.  
  50.   class SumTask implements Callable<Integer> {
  51.  
  52.     private int taskNum,
  53.                 limit;
  54.  
  55.     public SumTask(int taskNum, int limit) {
  56.       this.taskNum = taskNum;
  57.       this.limit = limit;
  58.     }
  59.  
  60.     public Integer call() throws Exception {
  61.       int sum = 0;
  62.       for (int i = 1; i <= limit; i++) {
  63.         if (Thread.currentThread().isInterrupted()) return null;
  64.         sum+=i;
  65.         ta.append("Task " + taskNum + " part result = " + sum + '\n');
  66.         Thread.sleep(1000);
  67.       }
  68.       return sum;
  69.     }
  70.   };
  71.  
  72.   Future<Integer> task;
  73.  
  74.   //ExecutorService exec = Executors.newSingleThreadExecutor();
  75.   ExecutorService exec = Executors.newFixedThreadPool(3);
  76.  
  77.   public void taskStart() {
  78.     try {
  79.  
  80.       taskList.add(task);
  81.       task = exec.submit(new SumTask(++k, 15));
  82.     } catch(RejectedExecutionException exc) {
  83.         ta.append("Execution rejected\n");
  84.         return;
  85.     }
  86.     ta.append("Task " + k + " submitted\n");
  87.   }
  88.  
  89.   public void taskResult() {
  90.  
  91.     for(int i=0;i<taskList.size();i++){
  92.         task=taskList.get(i);
  93.     String msg = "";
  94.     if (task.isCancelled()) msg = "Task cancelled.";
  95.     else if (task.isDone()) {
  96.       try {
  97.         msg = "Ready. Result = " + task.get();
  98.       } catch(Exception exc) {
  99.           msg = exc.getMessage();
  100.       }
  101.     }
  102.     else msg = "Task is running or waiting for execution";
  103.     JOptionPane.showMessageDialog(null, msg);
  104.     }
  105.   }
  106.  
  107.   public void taskStop() {
  108.     task.cancel(true);
  109.   }
  110.  
  111.   public void taskShutdown() {
  112.     exec.shutdown();
  113.     ta.append("Executor shutdown\n");
  114.   }
  115.  
  116.   public void taskShutdownNow() {
  117.     List<Runnable> awaiting = exec.shutdownNow();
  118.     ta.append("Executor shutdown now - awaiting tasks:\n");
  119.     for (Runnable r : awaiting) {
  120.       ta.append(r.getClass().getName()+'\n');
  121.     }
  122.  
  123.  }
  124.  
  125.  
  126.   public static void main(String[] args) {
  127.      new Exec1();
  128.   }
  129.  
  130. }
  131.  
  132.  
  133.  
i want to add tasks to a list then when a button is clicked stop a task or when another button is clicked show result of a task
when eg taskResult() is called i need to find that task(eg first task) in a list then show the result i dont want to limit the number of tasks(i cd do that with buttons) but i want to have unlimited number of tasks
but how i can get the result of eg 1st task then the second task etc.?
is it possible ? any hints ?
thank you in advance
Apr 24 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.