多线程

来源:互联网 发布:张子萱淘宝店月收入 编辑:程序博客网 时间:2024/06/05 09:41

进程:是一个正在执行中的程序。

每一个进程执行都有一个执行顺序。该顺序是一个执行路径。或者叫一个控制单元。

线程:就是进程中的一个独立的控制单元。

线程在控制着进程的执行。

注意:一个进程中至少有一个线程。


JVM 启动的时候会有一个进程 java.exe.

该进程中至少有一个线程负责java程序的执行。而且这个线程运行的代码存在于main方法中。该线程称为主线程。


扩展:其实更细节说明jvm,jvm启动不止一个线程,还有负责垃圾回收机制的线程。


1、如何在自定义的代码中,定义一个线程?

通过对api的查找,java已经提供了对线程这类事物的描述,就Thread类。

创建线程的第一种方法:继承Thread类。(必须在子类中重写Thread中的run()方法)

步骤:

1、定义类继承Thread.

2、复写Thread类中的run方法(目的:将自定义的代码存储在run运行方法,让线程)。

3、用new方式,创建一个线程。

3、调用线程的start方法,该方法有两个作用:(1、启动线程2、调用run方法。)

public class Demo54 {public static void main(String args[]){Xiancheng aa=new Xiancheng();//创建好一个线程aa.start();//启动一个线程}}class Xiancheng extends Thread{public void run(){for(int i=0;i<20;i++){System.out.println("xiancheng run--"+i);}}}

执行结果为:xiancheng run---0
xiancheng run---1
xiancheng run---2
xiancheng run---3
xiancheng run---4
xiancheng run---5
xiancheng run---6
xiancheng run---7
xiancheng run---8
xiancheng run---9
xiancheng run---10
xiancheng run---11
xiancheng run---12
xiancheng run---13
xiancheng run---14
xiancheng run---15
xiancheng run---16
xiancheng run---17
xiancheng run---18
xiancheng run---19
xiancheng run---20

多线程实例:在某一时刻,cpu只能执行一个进程。

public class Demo54 {public static void main(String args[]){Xiancheng aa=new Xiancheng();//创建好一个线程aa.start();//启动一个线程for(int x=0;x<50;x++){ //主线程先执行System.out.println("hello,world---"+x);}}}class Xiancheng extends Thread{public void run(){for(int i=0;i<50;i++){System.out.println("xiancheng run---"+i);}}}

执行结果:

hello,world---0
xiancheng run---0
xiancheng run---1
xiancheng run---2
xiancheng run---3
hello,world---1
xiancheng run---4
hello,world---2
xiancheng run---5
hello,world---3
xiancheng run---6
xiancheng run---7
hello,world---4
hello,world---5
hello,world---6
hello,world---7
hello,world---8
hello,world---9
hello,world---10
hello,world---11
hello,world---12
hello,world---13
hello,world---14
hello,world---15
hello,world---16
hello,world---17
hello,world---18
hello,world---19
hello,world---20
xiancheng run---8
xiancheng run---9
xiancheng run---10
xiancheng run---11
xiancheng run---12
xiancheng run---13
xiancheng run---14
xiancheng run---15
xiancheng run---16
xiancheng run---17
xiancheng run---18
xiancheng run---19
xiancheng run---20
xiancheng run---21
xiancheng run---22
xiancheng run---23
xiancheng run---24
xiancheng run---25
xiancheng run---26
xiancheng run---27
xiancheng run---28
xiancheng run---29
xiancheng run---30
xiancheng run---31
xiancheng run---32
xiancheng run---33
xiancheng run---34
xiancheng run---35
xiancheng run---36
xiancheng run---37
xiancheng run---38
xiancheng run---39
xiancheng run---40
xiancheng run---41
xiancheng run---42
xiancheng run---43
xiancheng run---44
xiancheng run---45
xiancheng run---46
xiancheng run---47
xiancheng run---48
xiancheng run---49
hello,world---21
hello,world---22
hello,world---23
hello,world---24
hello,world---25
hello,world---26
hello,world---27
hello,world---28
hello,world---29
hello,world---30
hello,world---31
hello,world---32
hello,world---33
hello,world---34
hello,world---35
hello,world---36
hello,world---37
hello,world---38
hello,world---39
hello,world---40
hello,world---41
hello,world---42
hello,world---43
hello,world---44
hello,world---45
hello,world---46
hello,world---47

hello,world---48

hello,world---49

结果发现,运行结果每一次都不同,因为多线程都获取cpu的执行权,cpu执行到谁,谁就运行。明确一点,在某一时刻,只能有一个程序在运行。(多核除外,多核也即是有多个cpu),cpu在做着快速的切换,以达到看上去是同时运行的效果。

可以形象的把多线程的运行行为看做为在互相抢夺cpu的执行权。


这就是多线程的一个特性:随机性。(谁抢到谁执行,至于执行多长时间,cpu说了算。)


为什么要覆盖run方法呢?

Thread类用于描述线程。

该类就定义了一个功能,用于存储线程要运行的代码,该存储功能就是run方法。

也就是说Thread类中的run方法,用于存储线程要运行的代码。


Demo d=new Demo();//创建好一个线程

d.start();//开启线程并执行该线程的run方法


Demo d=new Demo();//创建好一个线程

d.run();//仅仅是对象调用方法,而线程创建了,并没有运行











原创粉丝点击