-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo3.java
More file actions
36 lines (34 loc) · 1010 Bytes
/
ThreadDemo3.java
File metadata and controls
36 lines (34 loc) · 1010 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
package java_Threads;
public class ThreadDemo3 {
public static void main(String args[]) {
// new TestThread3().start();
// Runnable对象必须放在一个Thread类中才能运行
TestThread3 tt = new TestThread3();// 创建TestThread类的一个实例
Thread t = new Thread(tt);// 创建一个Thread类的实例
t.start();// 使线程进入Runnable״̬状态
while (true) {
System.out.println("main thread is running");
try {
Thread.sleep(1000); // 1000毫秒
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class TestThread3 implements Runnable // extends Thread
{
// 线程的代码段,执行start时,线程从此处开始执行
public void run() {
while (true) {
System.out.println(Thread.currentThread().getName() + " is running");
try {
Thread.sleep(1000); // 1000����
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}