/**
 * {@link <a href=
 * "https://[Log in to view URL]"
 * target= "_blank">Thread, dead lock</a>}
 * 
 * @author itammb ( Italia Massimiliano Buscati )
 * @version JDK 1.15
 *
 */
class Main {
	private static class Shared {
		// tutti i metodi dell'istanza sono in lock

		synchronized void input(Thread thread, Shared shared) throws InterruptedException {
			System.out.println("INPUT :: INIZIO RICEZIONE...." + thread.getName());

			Thread.sleep(1000);

			shared.output();
			System.out.println("INPUT :: FINE RICEZIONE....");
		}

		synchronized void output() throws InterruptedException {
			System.out.println("OUTPUT :: INIZIO RICEZIONE...");

			Thread.sleep(1000);

			System.out.println("OUTPUT :: FINE RICEZIONE....");
		}

		/*
		 * void output() throws InterruptedException { System.out.println(
		 * "OUTPUT :: INIZIO RICEZIONE..." );
		 * 
		 * Thread.sleep( 1000 );
		 * 
		 * System.out.println( "OUTPUT :: FINE RICEZIONE...." ); }
		 */
	}

	private static class ThreadDeadLock extends Thread {
		private Shared shared, sharedOther;

		@Override
		public void run() {
			try {
				shared.input(this, sharedOther);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		public ThreadDeadLock(Shared shared, Shared sharedOther) {
			this.shared = shared;
			this.sharedOther = sharedOther;
		}
	}

	private static class ThreadDeadLockOther extends Thread {
		private Shared shared, sharedOther;

		@Override
		public void run() {
			try {
				sharedOther.input(this, shared);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		public ThreadDeadLockOther(Shared shared, Shared sharedOther) {
			this.shared = shared;
			this.sharedOther = sharedOther;
		}
	}

	private static class SharedWaitNotify {
		public synchronized void applay() {
			System.out.println("Thread - inizio elaborazione");		
			notify(); // mette in attesa il thread e notifica a un thread in attesa la possibilità di elaborazione
			System.out.println("Thread - fine elaborazione");	
		}

		public synchronized void applayOther() {
			try {
				System.out.println("ThreadOther - inizio elaborazione");
				wait(); // mette in attesa il thread e aspetta un notify() o notifyAll()
				System.out.println("ThreadOther - fine elaborazione");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static class UniTestMultyThreds {

		public void applayDeadLock() throws InterruptedException {
	         Shared shared = new Shared();
	         Shared sharedOther = new Shared();
	  
	         ThreadDeadLock thread = new ThreadDeadLock( shared, sharedOther );	  
	         ThreadDeadLockOther threadOther = new ThreadDeadLockOther( shared, sharedOther );
	         thread.start();
	         threadOther.start();
	  
	         thread.join(5000);
	         threadOther.join(5000);
	         
	         System.out.println("Thread -> " + thread.getId( ) + " -- " + thread.getState());
	         System.out.println("Thread -> " + threadOther.getId() + " -- " + threadOther.getState());
		}
		
		public void applayWaitNotify() throws InterruptedException {
			SharedWaitNotify shared = new SharedWaitNotify();
     
			Thread thread = new Thread( new Runnable() { public void run() { shared.applay(); } } );
			Thread threadOther = new Thread( new Runnable() { public void run() { shared.applayOther(); } } );

			threadOther.start();
			thread.start();
		}
	}
	
	public static void main(String args[]) throws Exception {

		// Unit test - due thred si contendono la risorsa acquisita dal altro generando un dead-lock
		new UniTestMultyThreds().applayDeadLock();
		
		// Unit test - due thread accedono a metodi sincronizzati di una risorsa. L'ordine di accesso ai metodi genera un dead-lock 
		// new UniTestMultyThreds().applayWaitNotify();

	}
}

Embed on website

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