第一章01 Java多线程编程

一、主要内容


A. 线程与进程

B. 多线程的实现(继承Thread类实现)

C. 多线程的实现(Runnable接口实现)

D. 多线程的实现(两种线程实现方式的区别)

E. 多线程的实现(Callable接口实现)

二、分块讲解

A. 线程与进程

最早的时候DOS系统:只要电脑中毒,则电脑就死机。传统的DOS系统属于单进程操作系统,即在同一个时间段内,只允许有一个程序运行。

Windows时代的改变:电脑即使中毒,也可以使用。但是运行速度会变慢。在一个CPU、一块资源的情况下,程序利用一些轮转算法,可以让一个资源在一个时间段上同时处理多个程序(进程),在一个时间点上只允许一个进程去执行。

进程与线程的关系:一个进程可以划分为多个线程,线程的操作快于进程,多线程的操作性能优于多进程操作。所有的线程都一定在进程的基础之上进行划分。进程消失,则线程一定消失(线程永远依附于进程存在)。

B. 多线程的实现(继承Thread类实现)

范例1:

//定义Mythread类继承Thread类
class Mythread extends Thread{
    String name;
    public Mythread(String name){
        this.name = name;
    }
    @Override
    public void run(){
        for(int i = 0;i<10;i++){
            System.out.println(name+i);
        }
    }
}
//在主类中生成Mythread对象并调用start()启用线程
public class mainDemo{

    public static void main(String[] args) {
        Mythread mt1 = new Mythread("张三");
        Mythread mt2 = new Mythread("李四");
        Mythread mt3 = new Mythread("王五");
        mt1.start();
        mt2.start();
        mt3.start();
    }
}
//执行结果(不唯一,只要张三 李四 王五 交替出现即为正确):
/*
李四0
王五0
张三0
张三1
张三2
张三3
张三4
张三5
张三6
张三7
张三8
张三9
王五1
李四1
李四2
王五2
王五3
王五4
王五5
李四3
王五6
李四4
李四5
李四6
李四7
李四8
王五7
王五8
李四9
王五9
*/

范例2:直接调用run方法会出现什么结果呢?

//如果在主方法中直接调用run方法,会出现什么结果呢?
public class mainDemo{

    public static void main(String[] args) {
        Mythread mt1 = new Mythread("张三");
        Mythread mt2 = new Mythread("李四");
        Mythread mt3 = new Mythread("王五");
        mt1.run();
        mt2.run();
        mt3.run();
    }
}
//运行结果(唯一):
/*
张三0
张三1
张三2
张三3
张三4
张三5
张三6
张三7
张三8
张三9
李四0
李四1
李四2
李四3
李四4
李四5
李四6
李四7
李四8
李四9
王五0
王五1
王五2
王五3
王五4
王五5
王五6
王五7
王五8
王五9
*/

C. 多线程的实现(Runnable接口实现)

Runnable接口的定义

@FunctionalInterface
public interface Runnable{
    public void run();
}

范例1:

//定义Mythread类实现Runnable接口
class Mythread implements Runnable{
    String name;
    public Mythread(String name){
        this.name = name;
    }
    @Override
    public void run(){
        for(int i = 0;i<10;i++){
            System.out.println(name+i);
        }
    }
}
//由于Thread的构造方法为:public Thread(Runnable target),因此,可以通过传入Runnable接口并调用Thread的start方法启用线程

public class mainDemo{

    public static void main(String[] args) {
        Mythread mt1 = new Mythread("张三");
        Mythread mt2 = new Mythread("李四");
        Mythread mt3 = new Mythread("王五");
        new Thread(mt1).start();
        new Thread(mt2).start();
        new Thread(mt3).start();
    }
}
//执行结果(不唯一,只要张三 李四 王五 交替出现即为正确):
/*
李四0
王五0
张三0
张三1
张三2
张三3
张三4
张三5
张三6
张三7
张三8
张三9
王五1
李四1
李四2
王五2
王五3
王五4
王五5
李四3
王五6
李四4
李四5
李四6
李四7
李四8
王五7
王五8
李四9
王五9
*/

范例2: 使用匿名对象实现多线程

public class mainDemo{

    public static void main(String[] args) {
        String name = "张三";
        new Thread(new Runnable(){
            @Override
            public void run(){
                for(int i = 0;i<10;i++){
                System.out.println(name+i);
                }
            }
        }).start();
    }
}
//运行结果
/*
张三0
张三1
张三2
张三3
张三4
张三5
张三6
张三7
张三8
张三9
*/

范例3: 使用Lamda表达式实现多线程(JDK1.8)

public class mainDemo{

    public static void main(String[] args) {
        String name = "张三";
        new Thread(()->{
            for(int i = 0;i<10;i++){
            System.out.println(name+i);
            }
        }).start();
    }
}
//运行结果
/*
张三0
张三1
张三2
张三3
张三4
张三5
张三6
张三7
张三8
张三9
*/

猜你喜欢

转载自blog.csdn.net/guxunshe5199/article/details/81142042