Daniel Zawadzki Daniel Zawadzki - 6 months ago 17
Java Question

Concurrently working tasks

Im trying to prepare simple program, which allows user to create few concurrently working tasks. Nothing special. Every tasks start from value 1 and add another 1 to the moment when it reachs maximum (every task got different max value). Then task inform, that the designated value has been reached and it stops. All tasks must be contained in ArrayList, what i made. I need to provide methods, which allows (in every moment)


  1. Check status of the task

  2. Check result of the task

  3. End the task individually, or all in one

  4. Display list, which contains all tasks (Name, Value, Status)



I would prefer to avoid gui, as it's not required by my professor. I already made part, but i don't know how to print all elements of list (TASK_LIST) and stop individual tasks.

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();

}
}


TASK.java

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);
}

}

}


This is how it looks at this moment.

Answer

You need to make your showTASK_LIST() static. Then you can call it with Task.showTASK_LIST();

Like below

   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

The result I got when I ran your code produced an indexOutOfBounds exception in the above code. It is a simple fix.

Change your for loop to this

for(int i = 0; i < TASK_LIST.size(); i++)// changed <= to <

There are only 3 tasks, <= goes up to a fourth task causing problems

I got this output

Task 1 starts.
Task 3 starts.
Task 3 1
Task 1
Task 2
Task 3
Task 2 starts.
Task 1 1
Task 2 1
Task 3 2
Task 1 2
Task 2 2
Task 3 3
===========================================
Task 1 has been completed. Final result = 2
===========================================
Task 2 3
3
Task 3 4
Task 2 4
Task 2 5
Task 3 5
Task 2 6
===========================================
Task 3 has been completed. Final result = 5
===========================================
3
Task 2 7
Task 2 8
Task 2 9
Task 2 10
Task 2 11
Task 2 12
Task 2 13
Task 2 14
Task 2 15
Task 2 16
Task 2 17
Task 2 18
Task 2 19
Task 2 20
===========================================
Task 2 has been completed. Final result = 20
===========================================
3
Comments