https://www.cnblogs.com/echo-cheng/p/6814909.html
实现并启动线程有两种方法
1、写一个类继承自Thread类,重写run()方法,用start()方法启动线程。
2、写一个类实现Runnable接口,实现run()方法,用new Thread(Runnable target).start()方法。
多线程原理
start()排队,CPU选中后执行就run(),当CPU的运行的时间片执行完,这个线程就继续排队,等待下一次的run()。
1、start()方法来启动线程,真正实现多线程运行。
2、run()方法当做普通方法的方式调用。
程序还是要顺序执行,要等待run方法体执行完毕后,才可继续执行下面的代码; 程序中只有主线程——这一个线程, 其程序执行路径还是只有一条, 这样就没有达到写线程的目的。
多线程就是分时利用CPU,宏观上让所有线程一起执行 ,也叫并发
class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Runner1 runner1 = new Runner1();
Runner2 runner2 = new Runner2();
//Thread(Runnable target)分配新的对象
Thread thread1 = new Thread(runner1);
Thread thread2 = new Thread(runner2);
thread1.start();
thread2.start();
}
}
class Runner1 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++) {
System.out.println("进入Runner1运行状态----"+i);
}
}
}
class Runner2 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) {
System.out.println("进入Runner2运行状态==========" + i);
}
}
}
运行结果:
进入Runner1运行状态----0
进入Runner2运行状态==========0
进入Runner1运行状态----1
进入Runner2运行状态==========1
进入Runner1运行状态----2
进入Runner2运行状态==========2
进入Runner2运行状态==========3
进入Runner2运行状态==========4
进入Runner1运行状态----3
进入Runner1运行状态----4
进入Runner1运行状态----5
进入Runner1运行状态----6
进入Runner1运行状态----7
进入Runner1运行状态----8
进入Runner1运行状态----9
进入Runner2运行状态==========5
进入Runner1运行状态----10
进入Runner1运行状态----11
进入Runner1运行状态----12
进入Runner1运行状态----13
进入Runner1运行状态----14
进入Runner1运行状态----15
进入Runner1运行状态----16
进入Runner1运行状态----17
进入Runner1运行状态----18
进入Runner1运行状态----19
进入Runner1运行状态----20
进入Runner1运行状态----21
进入Runner1运行状态----22
进入Runner1运行状态----23
进入Runner1运行状态----24
进入Runner1运行状态----25
进入Runner1运行状态----26
进入Runner1运行状态----27
进入Runner1运行状态----28
进入Runner1运行状态----29
进入Runner1运行状态----30
进入Runner1运行状态----31
进入Runner1运行状态----32
进入Runner1运行状态----33
进入Runner1运行状态----34
进入Runner1运行状态----35
进入Runner1运行状态----36
进入Runner1运行状态----37
进入Runner1运行状态----38
进入Runner1运行状态----39
进入Runner1运行状态----40
进入Runner1运行状态----41
进入Runner1运行状态----42
进入Runner1运行状态----43
进入Runner1运行状态----44
进入Runner1运行状态----45
进入Runner1运行状态----46
进入Runner1运行状态----47
进入Runner1运行状态----48
进入Runner1运行状态----49
进入Runner1运行状态----50
进入Runner1运行状态----51
进入Runner1运行状态----52
进入Runner1运行状态----53
进入Runner1运行状态----54
进入Runner1运行状态----55
进入Runner1运行状态----56
进入Runner1运行状态----57
进入Runner1运行状态----58
进入Runner1运行状态----59
进入Runner1运行状态----60
进入Runner1运行状态----61
进入Runner1运行状态----62
进入Runner1运行状态----63
进入Runner1运行状态----64
进入Runner1运行状态----65
进入Runner1运行状态----66
进入Runner1运行状态----67
进入Runner1运行状态----68
进入Runner1运行状态----69
进入Runner1运行状态----70
进入Runner1运行状态----71
进入Runner1运行状态----72
进入Runner1运行状态----73
进入Runner1运行状态----74
进入Runner1运行状态----75
进入Runner1运行状态----76
进入Runner1运行状态----77
进入Runner1运行状态----78
进入Runner1运行状态----79
进入Runner1运行状态----80
进入Runner1运行状态----81
进入Runner1运行状态----82
进入Runner1运行状态----83
进入Runner1运行状态----84
进入Runner1运行状态----85
进入Runner1运行状态----86
进入Runner1运行状态----87
进入Runner1运行状态----88
进入Runner1运行状态----89
进入Runner2运行状态==========6
进入Runner2运行状态==========7
进入Runner2运行状态==========8
进入Runner2运行状态==========9
进入Runner2运行状态==========10
进入Runner2运行状态==========11
进入Runner2运行状态==========12
进入Runner2运行状态==========13
进入Runner2运行状态==========14
进入Runner2运行状态==========15
进入Runner2运行状态==========16
进入Runner2运行状态==========17
进入Runner2运行状态==========18
进入Runner2运行状态==========19
进入Runner2运行状态==========20
进入Runner2运行状态==========21
进入Runner2运行状态==========22
进入Runner2运行状态==========23
进入Runner2运行状态==========24
进入Runner2运行状态==========25
进入Runner2运行状态==========26
进入Runner2运行状态==========27
进入Runner2运行状态==========28
进入Runner2运行状态==========29
进入Runner2运行状态==========30
进入Runner2运行状态==========31
进入Runner2运行状态==========32
进入Runner2运行状态==========33
进入Runner2运行状态==========34
进入Runner2运行状态==========35
进入Runner2运行状态==========36
进入Runner2运行状态==========37
进入Runner1运行状态----90
进入Runner1运行状态----91
进入Runner1运行状态----92
进入Runner1运行状态----93
进入Runner1运行状态----94
进入Runner1运行状态----95
进入Runner1运行状态----96
进入Runner1运行状态----97
进入Runner1运行状态----98
进入Runner1运行状态----99
进入Runner2运行状态==========38
进入Runner2运行状态==========39
进入Runner2运行状态==========40
进入Runner2运行状态==========41
进入Runner2运行状态==========42
进入Runner2运行状态==========43
进入Runner2运行状态==========44
进入Runner2运行状态==========45
进入Runner2运行状态==========46
进入Runner2运行状态==========47
进入Runner2运行状态==========48
进入Runner2运行状态==========49
进入Runner2运行状态==========50
进入Runner2运行状态==========51
进入Runner2运行状态==========52
进入Runner2运行状态==========53
进入Runner2运行状态==========54
进入Runner2运行状态==========55
进入Runner2运行状态==========56
进入Runner2运行状态==========57
进入Runner2运行状态==========58
进入Runner2运行状态==========59
进入Runner2运行状态==========60
进入Runner2运行状态==========61
进入Runner2运行状态==========62
进入Runner2运行状态==========63
进入Runner2运行状态==========64
进入Runner2运行状态==========65
进入Runner2运行状态==========66
进入Runner2运行状态==========67
进入Runner2运行状态==========68
进入Runner2运行状态==========69
进入Runner2运行状态==========70
进入Runner2运行状态==========71
进入Runner2运行状态==========72
进入Runner2运行状态==========73
进入Runner2运行状态==========74
进入Runner2运行状态==========75
进入Runner2运行状态==========76
进入Runner2运行状态==========77
进入Runner2运行状态==========78
进入Runner2运行状态==========79
进入Runner2运行状态==========80
进入Runner2运行状态==========81
进入Runner2运行状态==========82
进入Runner2运行状态==========83
进入Runner2运行状态==========84
进入Runner2运行状态==========85
进入Runner2运行状态==========86
进入Runner2运行状态==========87
进入Runner2运行状态==========88
进入Runner2运行状态==========89
进入Runner2运行状态==========90
进入Runner2运行状态==========91
进入Runner2运行状态==========92
进入Runner2运行状态==========93
进入Runner2运行状态==========94
进入Runner2运行状态==========95
进入Runner2运行状态==========96
进入Runner2运行状态==========97
进入Runner2运行状态==========98
进入Runner2运行状态==========99
class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Runner1 runner1 = new Runner1();
Runner2 runner2 = new Runner2();
//Thread(Runnable target)分配新的对象
Thread thread1 = new Thread(runner1);
Thread thread2 = new Thread(runner2);
thread1.run();
thread2.run();
}
}
class Runner1 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<100;i++) {
System.out.println("进入Runner1运行状态----"+i);
}
}
}
class Runner2 implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 100; i++) {
System.out.println("进入Runner2运行状态==========" + i);
}
}
}
运行结果:
进入Runner1运行状态----0
进入Runner1运行状态----1
进入Runner1运行状态----2
进入Runner1运行状态----3
进入Runner1运行状态----4
进入Runner1运行状态----5
进入Runner1运行状态----6
进入Runner1运行状态----7
进入Runner1运行状态----8
进入Runner1运行状态----9
进入Runner1运行状态----10
进入Runner1运行状态----11
进入Runner1运行状态----12
进入Runner1运行状态----13
进入Runner1运行状态----14
进入Runner1运行状态----15
进入Runner1运行状态----16
进入Runner1运行状态----17
进入Runner1运行状态----18
进入Runner1运行状态----19
进入Runner1运行状态----20
进入Runner1运行状态----21
进入Runner1运行状态----22
进入Runner1运行状态----23
进入Runner1运行状态----24
进入Runner1运行状态----25
进入Runner1运行状态----26
进入Runner1运行状态----27
进入Runner1运行状态----28
进入Runner1运行状态----29
进入Runner1运行状态----30
进入Runner1运行状态----31
进入Runner1运行状态----32
进入Runner1运行状态----33
进入Runner1运行状态----34
进入Runner1运行状态----35
进入Runner1运行状态----36
进入Runner1运行状态----37
进入Runner1运行状态----38
进入Runner1运行状态----39
进入Runner1运行状态----40
进入Runner1运行状态----41
进入Runner1运行状态----42
进入Runner1运行状态----43
进入Runner1运行状态----44
进入Runner1运行状态----45
进入Runner1运行状态----46
进入Runner1运行状态----47
进入Runner1运行状态----48
进入Runner1运行状态----49
进入Runner1运行状态----50
进入Runner1运行状态----51
进入Runner1运行状态----52
进入Runner1运行状态----53
进入Runner1运行状态----54
进入Runner1运行状态----55
进入Runner1运行状态----56
进入Runner1运行状态----57
进入Runner1运行状态----58
进入Runner1运行状态----59
进入Runner1运行状态----60
进入Runner1运行状态----61
进入Runner1运行状态----62
进入Runner1运行状态----63
进入Runner1运行状态----64
进入Runner1运行状态----65
进入Runner1运行状态----66
进入Runner1运行状态----67
进入Runner1运行状态----68
进入Runner1运行状态----69
进入Runner1运行状态----70
进入Runner1运行状态----71
进入Runner1运行状态----72
进入Runner1运行状态----73
进入Runner1运行状态----74
进入Runner1运行状态----75
进入Runner1运行状态----76
进入Runner1运行状态----77
进入Runner1运行状态----78
进入Runner1运行状态----79
进入Runner1运行状态----80
进入Runner1运行状态----81
进入Runner1运行状态----82
进入Runner1运行状态----83
进入Runner1运行状态----84
进入Runner1运行状态----85
进入Runner1运行状态----86
进入Runner1运行状态----87
进入Runner1运行状态----88
进入Runner1运行状态----89
进入Runner1运行状态----90
进入Runner1运行状态----91
进入Runner1运行状态----92
进入Runner1运行状态----93
进入Runner1运行状态----94
进入Runner1运行状态----95
进入Runner1运行状态----96
进入Runner1运行状态----97
进入Runner1运行状态----98
进入Runner1运行状态----99
进入Runner2运行状态==========0
进入Runner2运行状态==========1
进入Runner2运行状态==========2
进入Runner2运行状态==========3
进入Runner2运行状态==========4
进入Runner2运行状态==========5
进入Runner2运行状态==========6
进入Runner2运行状态==========7
进入Runner2运行状态==========8
进入Runner2运行状态==========9
进入Runner2运行状态==========10
进入Runner2运行状态==========11
进入Runner2运行状态==========12
进入Runner2运行状态==========13
进入Runner2运行状态==========14
进入Runner2运行状态==========15
进入Runner2运行状态==========16
进入Runner2运行状态==========17
进入Runner2运行状态==========18
进入Runner2运行状态==========19
进入Runner2运行状态==========20
进入Runner2运行状态==========21
进入Runner2运行状态==========22
进入Runner2运行状态==========23
进入Runner2运行状态==========24
进入Runner2运行状态==========25
进入Runner2运行状态==========26
进入Runner2运行状态==========27
进入Runner2运行状态==========28
进入Runner2运行状态==========29
进入Runner2运行状态==========30
进入Runner2运行状态==========31
进入Runner2运行状态==========32
进入Runner2运行状态==========33
进入Runner2运行状态==========34
进入Runner2运行状态==========35
进入Runner2运行状态==========36
进入Runner2运行状态==========37
进入Runner2运行状态==========38
进入Runner2运行状态==========39
进入Runner2运行状态==========40
进入Runner2运行状态==========41
进入Runner2运行状态==========42
进入Runner2运行状态==========43
进入Runner2运行状态==========44
进入Runner2运行状态==========45
进入Runner2运行状态==========46
进入Runner2运行状态==========47
进入Runner2运行状态==========48
进入Runner2运行状态==========49
进入Runner2运行状态==========50
进入Runner2运行状态==========51
进入Runner2运行状态==========52
进入Runner2运行状态==========53
进入Runner2运行状态==========54
进入Runner2运行状态==========55
进入Runner2运行状态==========56
进入Runner2运行状态==========57
进入Runner2运行状态==========58
进入Runner2运行状态==========59
进入Runner2运行状态==========60
进入Runner2运行状态==========61
进入Runner2运行状态==========62
进入Runner2运行状态==========63
进入Runner2运行状态==========64
进入Runner2运行状态==========65
进入Runner2运行状态==========66
进入Runner2运行状态==========67
进入Runner2运行状态==========68
进入Runner2运行状态==========69
进入Runner2运行状态==========70
进入Runner2运行状态==========71
进入Runner2运行状态==========72
进入Runner2运行状态==========73
进入Runner2运行状态==========74
进入Runner2运行状态==========75
进入Runner2运行状态==========76
进入Runner2运行状态==========77
进入Runner2运行状态==========78
进入Runner2运行状态==========79
进入Runner2运行状态==========80
进入Runner2运行状态==========81
进入Runner2运行状态==========82
进入Runner2运行状态==========83
进入Runner2运行状态==========84
进入Runner2运行状态==========85
进入Runner2运行状态==========86
进入Runner2运行状态==========87
进入Runner2运行状态==========88
进入Runner2运行状态==========89
进入Runner2运行状态==========90
进入Runner2运行状态==========91
进入Runner2运行状态==========92
进入Runner2运行状态==========93
进入Runner2运行状态==========94
进入Runner2运行状态==========95
进入Runner2运行状态==========96
进入Runner2运行状态==========97
进入Runner2运行状态==========98
进入Runner2运行状态==========99