import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * {@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 "RunnableTask [task=" + task + ", hashCode()=" + hashCode() + "]";
		}
	}

	private static class UniTest {
		private static SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss");

		/**
		 * @see Executors#newFixedThreadPool(int)
		 * @see ExecutorService#execute(Runnable)
		 * 
		 */
		public void simulateFixedThreadPool(int nTasks, int nThreads, int timeout) {
			// creazione di un thread-pools con un buffer fisso di task attivi
			// i task non attivi rimangono in coda
			// la coda è illimitata
			ExecutorService pool = Executors.newFixedThreadPool(nThreads);

			for (int i = 0; i < nTasks; i++) {
				Runnable task = new Task("task_" + i);
				pool.execute(task);
			}

			shutdownNow(pool, timeout); // timeout in secondi
		}

		public void simulateWorkStealingPool(int nTasks, int parallelism, int timeout) {
			// creazione di un thread-pools con task attivi paralleli
			ExecutorService pool = Executors.newWorkStealingPool(parallelism);

			for (int i = 0; i < nTasks; i++) {
				Runnable task = new Task("task_" + i);
				pool.execute(task);
			}

			shutdown(pool, timeout);
		}

		/**
		 * @see ExecutorService#awaitTermination(long, TimeUnit)
		 * @see ExecutorService#shutdownNow()
		 * 
		 */
		private static void shutdownNow(ExecutorService pool, int timeout) {
			try {
				// attende il termine delle attività per un timeout in secondi
				// le attività in running non vengono forzate
				if (!pool.awaitTermination(timeout, TimeUnit.SECONDS)) {

					// tenta di interrompere tutte le attività in esecuzione attiva
					// interrompe l'elaborazione delle attività in attesa
					// restituisce un elenco delle attività in attesa di esecuzione
					List<Runnable> tasks = pool.shutdownNow();

					for (Runnable task : tasks)
						System.out.println(task.toString());

					if (!tasks.isEmpty())
						log("INTERRUPTED");
					else
						log("SHUTDOWN OK");
				}
			} catch (InterruptedException ie) {
				log("SHUTDOWN ERROR");
				ie.printStackTrace();
				Thread.currentThread().interrupt();
			}
		}

		/**
		 * @see ExecutorService#shutdown()
		 * @see ExecutorService#isTerminated()
		 * 
		 */
		private static void shutdown(ExecutorService pool, int timeout) {
			try {
				if (!pool.awaitTermination(timeout, TimeUnit.SECONDS)) {
					// avvia un arresto ordinato in cui vengono eseguite le attività inviate
					// non verranno accettate nuove attività
					pool.shutdown();

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

					log("SHUTDOWN OK");
				}
			} catch (InterruptedException ie) {
				log("SHUTDOWN ERROR");
				ie.printStackTrace();
				Thread.currentThread().interrupt();
			}
		}

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

	public static void main(String args[]) {
		// Unit test - thread-pool con buffer thread limitato
		new UniTest().simulateFixedThreadPool(5, 3, 10);

		// Unit test - thread-pool con coda di task multipla
		// new UniTest().simulateWorkStealingPool(5, 4, 4);
	}
}

Embed on website

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