import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeoutException;

/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target= "_blank"></a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */
class Main {
	public static class Task implements Runnable {
		private String task;
		private SimpleDateFormat ft;

		public Task(String task) {
			this.task = task;
			ft = new SimpleDateFormat("hh:mm:ss");
		}

		@Override
		public void run() {
			try {
				for (int i = 0; i <= 3; i++) {
					if (i == 0)
						log("INIT");
					else
						log("EXECUTE - STEP-" + i);

					Thread.sleep(1000);
				}
				
				log("COMPLETE");
			} catch (InterruptedException ie) {
				log("KILL");
				ie.printStackTrace();
			}
		}

		private void log(String status) {
			System.out.println(task + " - " + status + " - " + ft.format(new Date()));
		}

		@Override
		public String toString() {
			return "Task [task=" + task + ", hashCode()=" + hashCode() + "]";
		}
	}

	private static class UniTest {
		/**
		 * @see FutureTask#FutureTask(Runnable, Object)
		 * @see FutureTask#cancel(boolean)
		 * @see ExecutorService#submit(Runnable)
		 * 
		 */
		public void simulateFutureTask() throws InterruptedException, ExecutionException {
			List<FutureTask<String>> buffer = new ArrayList<FutureTask<String>>();

			// creazione di un thread-pools con riciclo dei task
			ExecutorService pool = Executors.newCachedThreadPool();

			for (int i = 0; i < 10; i++) {
				FutureTask<String> task = new FutureTask<String>(new Task("task " + i), "RESULT Object ");
				buffer.add(task);
				pool.submit(task);
			}

			for (FutureTask<String> task : buffer)
				if ((System.currentTimeMillis() % 2) == 0)
					// interrompe il processo in esecuzione
					task.cancel(true);
				else
					Thread.sleep(2);

			shutdown(pool);

			executeStatus(buffer);
		}

		private void shutdown(ExecutorService pool) throws InterruptedException {
			pool.shutdown();

			while (!pool.isTerminated())
				Thread.sleep(2);

			System.out.println("SHUTDOWN - OK");
		}

		/**
		 * @see FutureTask#isCancelled()
		 * @see FutureTask#get()
		 * 
		 */
		private void executeStatus(List<FutureTask<String>> buffer) throws InterruptedException, ExecutionException {

			for (FutureTask<String> task : buffer)
				if (task.isCancelled())
					System.out.println(task.toString() + " - CANCELLED");
				else {
					System.out.println(task.toString() + " - " + task.get());
				}
		}
	}

	public static void main(String args[]) throws InterruptedException, ExecutionException, TimeoutException {
		// Unit test - thread-pool ( con task annulabili ) non vi è esplicitamente un passaggio di variabili
		new UniTest().simulateFutureTask();
	}
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: