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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

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

    Code:
     
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.*;
    import java.lang.reflect.*;
    
    public class Exec1 extends JFrame implements ActionListener {
    
      int k = 0;
      int n = 15;
      JTextArea ta = new JTextArea(40,20);
      List<Future> taskList= new ArrayList<Future>();
      Exec1() {
        add(new JScrollPane(ta));
        JPanel p = new JPanel();
        JButton b = new JButton("Start");
        b.addActionListener(this);
        p.add(b);
        b = new JButton("Stop current");
        b.setActionCommand("Stop");
        b.addActionListener(this);
        p.add(b);
        b = new JButton("Curent result");
        b.setActionCommand("Result");
        b.addActionListener(this);
        p.add(b);
        b = new JButton("Shutdown");
        b.addActionListener(this);
        p.add(b);
        b = new JButton("ShutdownNow");
        b.addActionListener(this);
        p.add(b);
        add(p, "South");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e)  {
        String cmd = e.getActionCommand();
        try {
          Method m = this.getClass().getDeclaredMethod("task"+cmd);
          m.invoke(this);
        } catch(Exception exc) { exc.printStackTrace(); }
      }
    
    
      class SumTask implements Callable<Integer> {
    
        private int taskNum,
                    limit;
    
        public SumTask(int taskNum, int limit) {
          this.taskNum = taskNum;
          this.limit = limit;
        }
    
        public Integer call() throws Exception {
          int sum = 0;
          for (int i = 1; i <= limit; i++) {
            if (Thread.currentThread().isInterrupted()) return null;
            sum+=i;
            ta.append("Task " + taskNum + " part result = " + sum + '\n');
            Thread.sleep(1000);
          }
          return sum;
        }
      };
    
      Future<Integer> task;
    
      //ExecutorService exec = Executors.newSingleThreadExecutor();
      ExecutorService exec = Executors.newFixedThreadPool(3);
    
      public void taskStart() {
        try {
        	
          taskList.add(task);
          task = exec.submit(new SumTask(++k, 15));
        } catch(RejectedExecutionException exc) {
            ta.append("Execution rejected\n");
            return;
        }
        ta.append("Task " + k + " submitted\n");
      }
    
      public void taskResult() {
    	  
    	for(int i=0;i<taskList.size();i++){
    	    task=taskList.get(i);
        String msg = "";
        if (task.isCancelled()) msg = "Task cancelled.";
        else if (task.isDone()) {
          try {
            msg = "Ready. Result = " + task.get();
          } catch(Exception exc) {
              msg = exc.getMessage();
          }
        }
        else msg = "Task is running or waiting for execution";
        JOptionPane.showMessageDialog(null, msg);
        }
      }
    
      public void taskStop() {
        task.cancel(true);
      }
    
      public void taskShutdown() {
        exec.shutdown();
        ta.append("Executor shutdown\n");
      }
    
      public void taskShutdownNow() {
        List<Runnable> awaiting = exec.shutdownNow();
        ta.append("Executor shutdown now - awaiting tasks:\n");
        for (Runnable r : awaiting) {
          ta.append(r.getClass().getName()+'\n');
        }
    
     }
    
    
      public static void main(String[] args) {
         new Exec1();
      }
    
    }
    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
Working...