package mariaJava;

public class AsyncTests {

	public void asyncCall() {
		Class1 class1 = new Class1();
		Class2 class2 = new Class2();
			
		System.out.println("T1: Doing something that takes time:");
		class2.doSomethingSlowInAnotherThread(class1);
		System.out.println("T1: Immediately after class2 is called");
	}
	
	private class Class2 {
		public void doSomethingSlowInAnotherThread(Class1 class1) {
			System.out.println("T1: In Class 2. About to do something slow in another thread");
			
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						System.out.println("T2: Sleeping...");
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					class1.afterDoingSomethingSlowInClass2();
				}
			});
			thread.start();
			System.out.println("T1: Async call done.");
		}
	}

	private class Class1 {

		public void afterDoingSomethingSlowInClass2() {
			System.out.println("T2: Class1-callback: After doing something Slow in class1");
		}
		
	}

}
