forked from coding-technology/JavaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo02.java
More file actions
41 lines (31 loc) · 1.07 KB
/
ThreadDemo02.java
File metadata and controls
41 lines (31 loc) · 1.07 KB
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
public class ThreadDemo02 implements Runnable {
//线程执行的方法
@Override
public void run() {
for(int i=0;i<100;i++){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println( Thread.currentThread().getName()+":"+i );
}
}
//Runnable方式仍然是借用Thread来实现的多线程
public static void main(String[] args) {
ThreadDemo02 t1 = new ThreadDemo02();
ThreadDemo02 t2 = new ThreadDemo02();
//启动线程需要使用start()方法,但是Runnable没有提供start(),如何解决?
//Thread有start() Runnable没有start()
//Runnable ->Thread
//Thread(Runable )
Thread th1 = new Thread( t1,"T1-");
Thread th2 = new Thread( t2);
// th1.setName("T1");
th1.setPriority(Thread.MAX_PRIORITY);
th2.setPriority(Thread.MIN_PRIORITY);
th2.setName("T2");
th1.start();
th2.start();
}
}