-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathJLab0801.java
More file actions
36 lines (34 loc) · 993 Bytes
/
Copy pathJLab0801.java
File metadata and controls
36 lines (34 loc) · 993 Bytes
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
import java.lang.Math;
public class JLab0801 {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintThread thread1,thread2,thread3,thread4;
thread1 = new PrintThread("thread1");
thread2 = new PrintThread("thread2");
thread3 = new PrintThread("thread3");
thread4 = new PrintThread("thread4");
System.err.println("\n Starting threads");
thread1.start();
thread2.start();
thread3.start();
thread4.start();
System.err.println("Threads started\n");
}
}
class PrintThread extends Thread{
private int sleepTime;
public PrintThread(String name){
super(name);
sleepTime = (int)(Math.random() * 5000);
System.err.println("Name:"+getName()+";sleep:"+sleepTime);
}
public void run(){
try{
System.err.println(getName()+" going to sleep");
Thread.sleep(sleepTime);
}catch(InterruptedException interruptedException){
System.err.println(interruptedException.toString());
}
System.err.println(getName()+" done sleeping");
}
}