-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGreetingThreads.java
36 lines (32 loc) · 1.14 KB
/
GreetingThreads.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*EXP-7 Write a Java program that creates three threads. First thread displays ―Good Morning, every one second, the second thread displays Hello, every two seconds
and the third thread displays Welcome every three seconds.
*/
public class GreetingThreads {
public static void main(String[] args) {
Thread thread1 = new Thread(new GreetingRunnable("Good Morning", 1000));
Thread thread2 = new Thread(new GreetingRunnable("Hello", 2000));
Thread thread3 = new Thread(new GreetingRunnable("Welcome", 3000));
thread1.start();
thread2.start();
thread3.start();
}
}
class GreetingRunnable implements Runnable {
private String greeting;
private long interval;
public GreetingRunnable(String greeting, long interval) {
this.greeting = greeting;
this.interval = interval;
}
@Override
public void run() {
try {
while (true) {
System.out.println(greeting);
Thread.sleep(interval);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}