-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadDemo.java
More file actions
34 lines (29 loc) · 848 Bytes
/
ThreadDemo.java
File metadata and controls
34 lines (29 loc) · 848 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
package Thread;
//创建两个分线程,其中一个线程遍历1-100的偶数,另一个线程遍历1-100的基数
public class ThreadDemo {
public static void main(String[] args) {
MyThread1 m1 = new MyThread1();
MyThread2 m2 = new MyThread2();
m1.start();
m2.start();
// 也可以使用匿名对象的方式执行
}
}
class MyThread1 extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
if( i % 2 == 0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
class MyThread2 extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
if( i % 2 !=0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}