473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

oll3i
679 512MB
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
0 1533

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

Similar topics

3
by: Mark Preuss | last post by:
Hi all, Anybody seen where if after a Build with errors the Task List won't pop up when you click on the tab? I have to shut down the whole IDE to get it to come up. This is in VS.NET 2003. I...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
9
by: Ben Dewey | last post by:
Project: ---------------------------- I am creating a HTTPS File Transfer App using ASP.NET and C#. I am utilizing ActiveDirectory and windows security to manage the permissions. Why reinvent...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
2
by: shiry | last post by:
Hi, I need to do some important cleanup before my console application exists. I got some ideas from the groups and I used the console ctrl event. This is working well and it fires for all cases,...
6
by: Marcus | last post by:
Hi, I am a newbie at C#. Here is my current problem: I want my application to iterate through the list of tasks presented in task manager. I want it to look at the task names and if a certain...
9
by: Neo Geshel | last post by:
I have strip-mined, strip-searched, and completely exhausted the Internet (up to the 30th page on Google, with 100 results per page!!), all without finding an answer to my question AS TO WHY IT...
9
by: joanne matthews (RRes-Roth) | last post by:
I'm getting different results when I add up a list of floats depending on the order that I list the floats. For example, the following returns False: def check(): totalProp=0 inputs= for each...
12
by: Bob Jones | last post by:
I have an odd business requirement and I think that the implementation is not correct in the terms of OOP development. Any help on the concepts would be very appreciated! We currently have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.