-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMThread.java
51 lines (48 loc) · 907 Bytes
/
MThread.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class MThread
{
public static void main(String[] args)
{
System.out.println("Hello World!");
thread2 t1 = new thread2("线程实例1");
t1.start(); //调用
thread2 t2 = new thread2("线程实例2");
t2.start();
thread2 t3 = new thread2("线程实例3");
t3.start();
}
}
// 自定义线程类
class thread2 extends Thread
{
Thread thread; // 定义线程实例
String str;
// 构造函数
public thread2(String str)
{
this.str = str;
}
// 启动线程
public void start()
{
thread = new Thread(this);
thread.start();
}
public void run()
{
int i = 0;
while(thread != null)
{
try {
// 计数到5时睡眠10秒
if (i == 5)
sleep(10000);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
System.out.println(str);
i++;
}
}
}