Write a Java program to display strings using Thread.
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");
}
}
[Hello]
[Good morning]
[Rohit]