import java.util.concurrent.Phaser;

/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target= "_blank">Elaborazioni di attività e sotto-fasi concorrenti</a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */
class Main {
	private static class Activity {
		protected Phaser monitor;
		protected String title;

		/**
		 * @see Phaser#register()
		 * 
		 */
		public Activity(Phaser monitor, String title) {
			this.monitor = monitor;
			this.title = title;

			// registra le sub-attività
			monitor.register();
		}
	}

	private static class Activity1 extends Activity implements Runnable {
		/**
		 * @see Phaser#arriveAndAwaitAdvance()
		 * @see Phaser#arriveAndDeregister()
		 * 
		 */
		@Override
		public void run() {
			try {
				System.out.println("Sub-attività: " + title + " :: fase 1 :: codice critico");

				// concede esecuzione ad altri thread
				monitor.arriveAndAwaitAdvance();

				System.out.println("Sub-attività: " + title + " :: fase 2 :: codice critico");

				monitor.arriveAndAwaitAdvance();
			} finally {
				monitor.arriveAndDeregister();
			}
		}

		public Activity1(Phaser monitor, String title) {
			super(monitor, title);

			new Thread(this).start();
		}
	}

	private static class Activity2 extends Activity implements Runnable {
		@Override
		public void run() {
			try {
				System.out.println("Sub-attività: " + title + " :: fase 1 :: codice critico ");

				// concede esecuzione ad altri thread
				monitor.arriveAndAwaitAdvance();

				System.out.println("Sub-attività: " + title + " :: fase 2 :: codice critico");

				monitor.arriveAndAwaitAdvance();

				System.out.println("Sub-attività: " + title + " :: fase 3 :: codice critico");

				monitor.arriveAndAwaitAdvance();

				System.out.println("Sub-attività: " + title + " :: fase 4 :: codice critico");

				monitor.arriveAndAwaitAdvance();
			} finally {
				monitor.arriveAndDeregister();
			}
		}

		public Activity2(Phaser monitor, String title) {
			super(monitor, title);

			new Thread(this).start();
		}
	}

	private static class PhaserOnAdvance extends Phaser {
		private int levels; // totale

		@Override
		protected boolean onAdvance(int phaseByLevel, int activity) {
			// collettore
			System.out.println("fasi del livello " + phaseByLevel + " :: completate ");

			if (levels== phaseByLevel || activity == 0) {
				return true;
			}

			return false;
		}

		public PhaserOnAdvance(int levels) {
			super(1);

			this.levels = levels - 1;
		}
	}

	public static class Activity3 extends Activity implements Runnable {
		@Override
		public void run() {
			while (!monitor.isTerminated()) {
				// fase n ....
				System.out.println("attività: " + title + " :: fase " + (monitor.getPhase() + 1) +  " :: codice critico ");
				monitor.arriveAndAwaitAdvance();
			}
		}

		public Activity3(Phaser monitor, String title) {
			super(monitor, title);

			new Thread(this).start();
		}
	}

	private static class UniTest {

		public void simulate(Phaser monitor) {
			// registra attività master
			monitor.register();

			addSubActivity(monitor);
			advance(monitor);

			// de-registra il monitor
			monitor.arriveAndDeregister();

			System.out.println("attività master terminata: " + monitor.isTerminated());
		}

		private void addSubActivity(Phaser monitor) {
			new Activity1(monitor, "1");
			new Activity2(monitor, "2");
			new Activity2(monitor, "3");
		}

		/**
		 * @see Phaser#getPhase()
		 * 
		 */
		private void advance(Phaser monitor) {
			// intercetta la fine di una fase
			int activityNow;
			while ((activityNow = monitor.getPhase()) < 4) {
				// fa eseguire la n-fase a tutti i thread-concorrenti
				monitor.arriveAndAwaitAdvance();

				System.out.println("-- tutte le fasi :: " + (activityNow + 1) + " :: sono terminate");
			}			
		}

		public void simulate(PhaserOnAdvance monitor) {

			System.out.println("inizio attività master");

			new Activity3(monitor, "0");
			new Activity3(monitor, "1");

			while (!monitor.isTerminated()) {
				// fa eseguire la n-fase a tutti i thread-concorrenti
				monitor.arriveAndAwaitAdvance();
			}

			System.out.println("attività master terminata");
		}
	}

	public static void main(String args[]) {
		// Unit test - elaborazione di un attività in sotto-fasi ( thread con logiche
		// indipendenti da altri processi ma in ordine
		// sequenziale di fase inter-attività )
		new UniTest().simulate(new Phaser());

		// Unit test - Elaborazione di un attività con un collettore alla fine di ogni
		// fase
		//new UniTest().simulate(new PhaserOnAdvance(4));
	}
}

Embed on website

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