首页 > 代码库 > java 线程1
java 线程1
public static void main(String[] args) {
Runner1 r1 = new Runner1();
Thread t = new Thread(r1);
t.start();
for (int i = 0; i < 100; i++) {
System.out.println("MainThread-->"+i);
}
}
//Runner1实现Runable接口
class Runner1 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println("Runner1-->"+i);
}
}
}
程序运行结果:
Runner1-->0
MainThread-->0
MainThread-->1
MainThread-->2
MainThread-->3
MainThread-->4
MainThread-->5
Runner1-->1
MainThread-->6
Runner1-->2
MainThread-->7
Runner1-->3
MainThread-->8
MainThread-->9
MainThread-->10
MainThread-->11
MainThread-->12
MainThread-->13
MainThread-->14
MainThread-->15
MainThread-->16
MainThread-->17
MainThread-->18
MainThread-->19
Runner1-->4
MainThread-->20
Runner1-->5
MainThread-->21
MainThread-->22
MainThread-->23
MainThread-->24
Runner1-->6
MainThread-->25
Runner1-->7
MainThread-->26
Runner1-->8
Runner1-->9
Runner1-->10
Runner1-->11
Runner1-->12
Runner1-->13
Runner1-->14
Runner1-->15
Runner1-->16
MainThread-->27
Runner1-->17
Runner1-->18
Runner1-->19
Runner1-->20
Runner1-->21
Runner1-->22
Runner1-->23
Runner1-->24
Runner1-->25
Runner1-->26
MainThread-->28
Runner1-->27
MainThread-->29
Runner1-->28
MainThread-->30
Runner1-->29
Runner1-->30
Runner1-->31
Runner1-->32
Runner1-->33
Runner1-->34
Runner1-->35
Runner1-->36
Runner1-->37
MainThread-->31
MainThread-->32
MainThread-->33
MainThread-->34
MainThread-->35
MainThread-->36
MainThread-->37
MainThread-->38
MainThread-->39
MainThread-->40
MainThread-->41
MainThread-->42
MainThread-->43
MainThread-->44
MainThread-->45
MainThread-->46
MainThread-->47
MainThread-->48
MainThread-->49
MainThread-->50
MainThread-->51
MainThread-->52
MainThread-->53
MainThread-->54
MainThread-->55
MainThread-->56
MainThread-->57
MainThread-->58
MainThread-->59
MainThread-->60
MainThread-->61
MainThread-->62
MainThread-->63
MainThread-->64
MainThread-->65
MainThread-->66
MainThread-->67
MainThread-->68
MainThread-->69
MainThread-->70
MainThread-->71
MainThread-->72
MainThread-->73
MainThread-->74
MainThread-->75
MainThread-->76
MainThread-->77
MainThread-->78
MainThread-->79
MainThread-->80
MainThread-->81
MainThread-->82
MainThread-->83
MainThread-->84
MainThread-->85
MainThread-->86
MainThread-->87
MainThread-->88
MainThread-->89
MainThread-->90
MainThread-->91
MainThread-->92
MainThread-->93
MainThread-->94
MainThread-->95
MainThread-->96
MainThread-->97
MainThread-->98
MainThread-->99
Runner1-->38
Runner1-->39
Runner1-->40
Runner1-->41
Runner1-->42
Runner1-->43
Runner1-->44
Runner1-->45
Runner1-->46
Runner1-->47
Runner1-->48
Runner1-->49
和普通方法的区别:线程执行体和主线程分时间片运行
java 线程1