-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadRunnableTest.java
More file actions
53 lines (47 loc) · 996 Bytes
/
Copy pathThreadRunnableTest.java
File metadata and controls
53 lines (47 loc) · 996 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
46
47
48
49
50
51
52
53
package java_base;
class Alpha extends Thread{
public void run(){
char ch = ' ';
try {
for(int i = 0; i < 100; i++)
{
ch = (char)('A' + (int)(Math.random() * 26));
System.out.printf("%20c\n", ch);
sleep(3000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Digit implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
char ch = ' ';
try {
for(int i = 0; i < 50; i++)
{
ch = (char)('0' + (int)(Math.random() * 10));
System.out.printf("%-20c\n", ch);
Thread.sleep(6000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadRunnableTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Alpha t1 = new Alpha();
t1.start();
Digit d = new Digit();
Thread t2 = new Thread(d);
t2.start();
}
}
/*
* 使用继承的进程类 创建之后就能使用
* 使用实现接口的进程类 创建之后需要创建线程实例 才能使用
*/