-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathSampleThread.java
More file actions
45 lines (38 loc) · 846 Bytes
/
SampleThread.java
File metadata and controls
45 lines (38 loc) · 846 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
37
38
39
40
41
42
43
44
45
class Thread1 extends Thread {
@Override
public void run() {
while (true) {
System.out.println("I am Thread1.");
try {
Thread.sleep(500);
} catch (Exception ex) {
System.err.println("exception");
}
}
}
}
class Thread2 extends Thread {
@Override
public void run() {
while (true) {
System.out.println("I am Thread2.");
try {
Thread.sleep(500);
} catch (Exception ex) {
System.err.println("exception");
}
}
}
}
class SampleThread {
public static void main(String args[]) throws Exception {
Thread t1 = new Thread1();
t1.start();// separate stack memory is allocated, then run gets called
Thread t2 = new Thread2();
t2.start();// separate stack memory is allocated, then run gets called
while (true) {
System.out.println("I am main thread.");
Thread.sleep(1000);
}
}
}