Java 兼任工作任务

Java 兼任工作任务,java,task,java.util.concurrent,Java,Task,Java.util.concurrent,我正在准备一个简单的程序,它允许用户创建几个并发的工作任务。没什么特别的。每个任务从值1开始,在达到最大值时再加上一个1(每个任务得到不同的最大值)。然后,任务通知,已达到指定值并停止。所有任务都必须包含在ArrayList中,即我制作的。我需要提供方法,允许(在任何时刻) 检查任务的状态 检查任务的结果 单独或一次完成任务 显示列表,其中包含所有任务(名称、值、状态) 我宁愿避免使用gui,因为我的教授不需要它。我已经做了部分,但我不知道如何打印列表(任务列表)的所有元素并停止单个任务 MAI

我正在准备一个简单的程序,它允许用户创建几个并发的工作任务。没什么特别的。每个任务从值1开始,在达到最大值时再加上一个1(每个任务得到不同的最大值)。然后,任务通知,已达到指定值并停止。所有任务都必须包含在ArrayList中,即我制作的。我需要提供方法,允许(在任何时刻)

  • 检查任务的状态
  • 检查任务的结果
  • 单独或一次完成任务
  • 显示列表,其中包含所有任务(名称、值、状态)
  • 我宁愿避免使用gui,因为我的教授不需要它。我已经做了部分,但我不知道如何打印列表(任务列表)的所有元素并停止单个任务

    MAIN.java

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Main {
    
      public static void main(String[] args) {
    
        ExecutorService exec = Executors.newCachedThreadPool();
    
        Task t1 = new Task("Task 1", 2);
        Task t2 = new Task("Task 2", 20);
        Task t3 = new Task("Task 3", 5);
    
        exec.execute(t1);
        exec.execute(t2);
        exec.execute(t3);
    
        //showTASK_LIST();
    
      }
    }
    
        import java.util.ArrayList;
        import java.util.List;
    
        class Task implements Runnable {
            String TASK_NAME;
            int TASK_RESULT;
            int TASK_GOAL;
            String TASK_STATUS;
            static List<Task> TASK_LIST = new ArrayList<Task>();
    
            public Task(String name, int goal) {
                this.TASK_NAME = name;
                this.TASK_GOAL = goal;
                this.TASK_STATUS = "INACTIVE";
                TASK_LIST.add(this);
            }
    
            public void run() {
                TASK_STATUS="ACTIVE";
                System.out.println(TASK_NAME + " starts.");
                while (TASK_RESULT != TASK_GOAL){
                    //if (Thread.currentThread().isInterrupted()) return;
                    TASK_RESULT++;
                    System.out.println(TASK_NAME + " " + TASK_RESULT);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Thread.yield();
    
                }
                System.out.println("===========================================\n" + 
                                    TASK_NAME + " has been completed. Final result = " + TASK_GOAL +
                                    "\n===========================================" );
                setTASK_STATUS("COMPLETED");
                System.out.println(TASK_LIST.size());
    
    
            }
    
            //Method to check current result
            public int getTASK_RESULT() {
                return TASK_RESULT;
            }
    
            //Method to check current status
            public String getTASK_STATUS() {
                return TASK_STATUS;
            }
    
            //Method to change result
            public void setTASK_STATUS(String status) {
                TASK_STATUS = status;
            }
    
            //This part doesnt work
            public showTASK_LIST(){
                for(int i = 0; i <= TASK_LIST.size(); i++){
                    System.out.println(TASK_LIST.get(i).TASK_NAME);
                }
    
            }
    
        }
    
    TASK.java

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class Main {
    
      public static void main(String[] args) {
    
        ExecutorService exec = Executors.newCachedThreadPool();
    
        Task t1 = new Task("Task 1", 2);
        Task t2 = new Task("Task 2", 20);
        Task t3 = new Task("Task 3", 5);
    
        exec.execute(t1);
        exec.execute(t2);
        exec.execute(t3);
    
        //showTASK_LIST();
    
      }
    }
    
        import java.util.ArrayList;
        import java.util.List;
    
        class Task implements Runnable {
            String TASK_NAME;
            int TASK_RESULT;
            int TASK_GOAL;
            String TASK_STATUS;
            static List<Task> TASK_LIST = new ArrayList<Task>();
    
            public Task(String name, int goal) {
                this.TASK_NAME = name;
                this.TASK_GOAL = goal;
                this.TASK_STATUS = "INACTIVE";
                TASK_LIST.add(this);
            }
    
            public void run() {
                TASK_STATUS="ACTIVE";
                System.out.println(TASK_NAME + " starts.");
                while (TASK_RESULT != TASK_GOAL){
                    //if (Thread.currentThread().isInterrupted()) return;
                    TASK_RESULT++;
                    System.out.println(TASK_NAME + " " + TASK_RESULT);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Thread.yield();
    
                }
                System.out.println("===========================================\n" + 
                                    TASK_NAME + " has been completed. Final result = " + TASK_GOAL +
                                    "\n===========================================" );
                setTASK_STATUS("COMPLETED");
                System.out.println(TASK_LIST.size());
    
    
            }
    
            //Method to check current result
            public int getTASK_RESULT() {
                return TASK_RESULT;
            }
    
            //Method to check current status
            public String getTASK_STATUS() {
                return TASK_STATUS;
            }
    
            //Method to change result
            public void setTASK_STATUS(String status) {
                TASK_STATUS = status;
            }
    
            //This part doesnt work
            public showTASK_LIST(){
                for(int i = 0; i <= TASK_LIST.size(); i++){
                    System.out.println(TASK_LIST.get(i).TASK_NAME);
                }
    
            }
    
        }
    
    import java.util.ArrayList;
    导入java.util.List;
    类任务实现可运行{
    字符串任务名称;
    int任务结果;
    任务目标;
    字符串任务状态;
    静态列表任务_List=new ArrayList();
    公共任务(字符串名称,整数目标){
    this.TASK_NAME=名称;
    this.TASK_GOAL=目标;
    this.TASK_STATUS=“非活动”;
    任务列表。添加(此);
    }
    公开募捐{
    任务状态=“活动”;
    System.out.println(任务名称+“开始”);
    while(任务结果!=任务目标){
    //if(Thread.currentThread().isInterrupted())返回;
    任务_结果++;
    System.out.println(任务名称+“”+任务结果);
    试一试{
    睡眠(500);
    }捕捉(中断异常e){
    e、 printStackTrace();
    }
    螺纹屈服强度();
    }
    System.out.println(“============================================================================\n”+
    任务_NAME+”已完成。最终结果=“+TASK_目标”+
    “\n=======================================================”;
    设置任务_状态(“已完成”);
    System.out.println(TASK_LIST.size());
    }
    //检查当前结果的方法
    public int getTASK_RESULT(){
    返回任务结果;
    }
    //检查当前状态的方法
    公共字符串getTASK_STATUS(){
    返回任务状态;
    }
    //改变结果的方法
    public void setTASK_状态(字符串状态){
    任务状态=状态;
    }
    //这部分不行
    公共showTASK_LIST(){
    
    对于(int i=0;i您需要将
    showTASK_LIST()
    设置为静态。然后您可以使用
    Task.showTASK_LIST();

    如下

       public static void showTASK_LIST(){
            for(int i = 0; i <= TASK_LIST.size(); i++){
                System.out.println(TASK_LIST.get(i).TASK_NAME);
            }
    
    Task.showTASK_LIST();//goes in your main method to show the tasks
    

    您需要将
    showTASK_LIST()
    设置为静态。然后可以使用
    Task.showTASK_LIST();

    如下

       public static void showTASK_LIST(){
            for(int i = 0; i <= TASK_LIST.size(); i++){
                System.out.println(TASK_LIST.get(i).TASK_NAME);
            }
    
    Task.showTASK_LIST();//goes in your main method to show the tasks
    

    您的代码存在线程安全问题,因为您试图从多个线程读取/写入共享资源(本例中为任务对象)上的值:

    您正在从一个线程更改任务状态(写入),并尝试从另一个线程读取任务状态(读取),这是主线程。在这种情况下:

    Task.showTASK_LIST()
    
    同样的问题也存在于:

          //Method to check current result
        public int getTASK_RESULT() {
            return TASK_RESULT;
        }
    
        //Method to check current status
        public String getTASK_STATUS() {
            return TASK_STATUS;
        }
    
    有关Java并发性和线程安全的信息,请访问:

    在重用代码的同时,我将提供自己的实现,但在此之前,请在深入研究代码之前先做几点注释:

  • 使用Callable接口而不是Runnable,因为您希望在处理结束时返回结果(Callable类似于Runnable,但您可以返回结果)
  • 使用Executor submit()方法返回Future对象(Future有许多有用的方法,如:isDone()、cancel()、get()等)
  • 管理主线程中以及任务类之外的任务列表
  • StatusChangeListener

    public interface StatusChangeListener {
    
      void statusChanged(String oldStatus, String newStatus);
    }
    
    Main

    import java.util.ArrayList;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    
    public class Main {
    
      public static void main(String[] args) {
    
        ExecutorService executor = Executors.newCachedThreadPool();
    
        //manage your tasks list here
        ArrayList<Task> tasks = new ArrayList<>();
        Task t1 = new Task("Task 1", 2);
        Task t2 = new Task("Task 2", 20);
        Task t3 = new Task("Task 3", 5);
    
        tasks.add(t1);
        tasks.add(t2);
        tasks.add(t3);
    
        //add status change listener to be notified for t1 status changes
        t1.addStatusChangeListener(new MyListener());
    
        System.out.println("tasks = " + tasks);
    
        //use submit instead of execute
        Future<Integer> future1 = executor.submit(t1);
    
        System.out.println("task 1 is done = " + future1.isDone());
    
        Future<Integer> future2 = executor.submit(t2);
        Future<Integer> future3 = executor.submit(t3);
        //cancel task 3
        future3.cancel(true);
    
        try {
          System.out.println("future 1 result= " + future1.get());
          System.out.println("future 2 result= " + future2.get());
        } catch (InterruptedException e) {
          e.printStackTrace();
        } catch (ExecutionException e) {
          e.printStackTrace();
        }
      }
    
      //you can do the same with the progress. e.g. add progress listener
      private static class MyListener implements StatusChangeListener{
    
        @Override
        public void statusChanged(String oldStatus, String newStatus) {
          System.out.println(String.format("Thread : %s status changed from %s to %s", Thread.currentThread(), oldStatus, newStatus));
        }
      }
    
    }
    
    import java.util.ArrayList;
    导入java.util.concurrent.ExecutionException;
    导入java.util.concurrent.ExecutorService;
    导入java.util.concurrent.Executors;
    导入java.util.concurrent.Future;
    公共班机{
    公共静态void main(字符串[]args){
    ExecutorService executor=Executors.newCachedThreadPool();
    //在此处管理任务列表
    ArrayList tasks=新建ArrayList();
    任务t1=新任务(“任务1”,2);
    任务t2=新任务(“任务2”,20);
    任务t3=新任务(“任务3”,5);
    任务。添加(t1);
    任务。添加(t2);
    任务。添加(t3);
    //添加要通知t1状态更改的状态更改侦听器
    t1.addStatusChangeListener(新的MyListener());
    System.out.println(“tasks=“+tasks”);
    //使用提交而不是执行
    未来1=执行人提交(t1);
    System.out.println(“任务1已完成=“+future1.isDone());
    未来2=执行人提交(t2);
    未来3=执行人提交(t3);
    //取消任务3
    未来3.取消(真);
    试一试{
    System.out.println(“future 1 result=“+future1.get());
    System.out.println(“future 2 result=“+future2.get());
    }捕捉(中断异常e){
    e、 printStackTrace();
    }捕获(执行例外){
    e、 printStackTrace();
    }
    }
    //您可以对进度执行相同的操作。例如,添加进度侦听器
    私有静态类MyListener实现StatusChangeListener{
    @凌驾
    公共无效状态已更改(字符串oldStatus、字符串newStatus){
    System.out.println(String.format(“线程:%s状态从%s更改为%s”,Thread.currentThread(),oldStatus,newStatus));
    }
    }
    }
    
    任务

    import java.util.concurrent.Callable;
    
    class Task implements Callable<Integer> {
    
      //can be replaced by enum
      private String TASK_STATUS;
      private String TASK_NAME;
      private int TASK_RESULT;
      private int TASK_GOAL;
      private StatusChangeListener statusChangeListener;
    
    
      public Task(String name, int goal) {
        this.TASK_NAME = name;
        this.TASK_GOAL = goal;
        this.TASK_STATUS = "INACTIVE";
      }
    
      //private Method to change result
      private void setTASK_STATUS(String newStatus) {
        String oldStatus = TASK_STATUS;
        TASK_STATUS = newStatus;
        //notify status changes
        if (statusChangeListener != null)
          statusChangeListener.statusChanged(oldStatus, newStatus);
      }
    
      @Override
      public Integer call() throws Exception {
        setTASK_STATUS("ACTIVE");
        System.out.println(TASK_NAME + " starts.");
        while (TASK_RESULT != TASK_GOAL) {
          //if (Thread.currentThread().isInterrupted()) return;
          TASK_RESULT++;
          System.out.println(TASK_NAME + " " + TASK_RESULT);
          try {
            Thread.sleep(500);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          Thread.yield();
    
        }
        System.out.println("===========================================\n" +
                TASK_NAME + " has been completed. Final result = " + TASK_GOAL +
                "\n===========================================");
        setTASK_STATUS("COMPLETED");
        return TASK_RESULT;
      }
    
      public void addStatusChangeListener(StatusChangeListener statusChangeListener) {
        this.statusChangeListener = statusChangeListener;
      }
    }
    
    import java.util.concurrent.Callable;
    类任务实现可调用{
    //可以用枚举替换
    私有字符串任务状态;
    私有字符串任务名称;
    私有int任务_结果;
    私人内部任务(u)目标;;
    私有状态更改侦听器状态更改侦听器;
    公众的