Java Program to display strings using Thread

Write a Java program to display strings using Thread.



Source Code
class Sync {
	
	void display(String s) {
		System.out.print("[" + s);
		try {
			Thread.sleep(1000);
		}
		catch(InterruptedException e) {
			System.out.println("Exception");
		}
		System.out.println("]");
	}
}

class SyncTest extends Thread {
	String z = "";
	Sync x;
	Thread t;
	
	public SyncTest(Sync y, String s) {
		x = y;
		z = s;
		t = new Thread(this);
		t.start();
	}
	
	public void run() {
		x.display(z);
	}
}

public class SynchronizationDemo {

	public static void main(String[] args) {
		
		Sync s = new Sync();
		SyncTest s1 = new SyncTest(s, "Hello");
		SyncTest s2 = new SyncTest(s, "Rohit");
		SyncTest s3 = new SyncTest(s, "Good morning");
	}
}
Output
[Hello]
[Good morning]
[Rohit]





"Coding Hub - Learn to code" app now available on Google Play Store