开发者

Javaee多线程之进程和线程之间的区别和联系(最新整理)

目录
  • 进程和线程
    • 进程
    • 线程
    • 进程和线程的区别
  • 创建线程的五种写法
    • 继承Thread,重写run
    • 实现Runnable(接口),重写run
    • 继承Thread,重写run,但是使用匿名内部类
    • 实现Runnable(接口),重写run,但是使用匿名内部类
    • 使用lambda表达式
  • 请说明Thread类中run和start的区别

    进程和线程

    进程

    1. 进程:是正在执行的程序,是资源分配的基本单位,具有独立的地址空间
    2. 操作系统会为其分配CPU和内存

    线程

    1. 线程:引入线程是为了解决进程开销大,浪费http://www.devze.com资源的情景,并且多进程并发效率比较低
    2. 线程是调度执行的基本单位
    3. 线程之间会相互影响,一个线程挂了,会影响到整个进程都异常结束,线程也自然会结束

    进程和线程的区别

    1. 进程包含线程,一个进程里面有多个线程或者是一个线程
    2. 进程和线程都是用来实现并发编程场景的,但是线程比进程更轻量和高效
    3. 同一个进程的线程之间共用同一份资源(内存和硬盘),省去了申请资源的开销
    4. 进程和进程之间都是独立存在的,不会相互影响,同一个进程中,线程和线程之间会相互影响(线程安全问题 + 线程出现异常)
    5. 进程是分配资源的基本单位,线程是调度执行的基本单位

    创建线程的五种写法

    继承Thread,重写run

    package Thread;
    class MyThread extends Thread{
        public void run(){
            // 这个是线程的入口方法
            while(true) {
                System.out.println("hello Thread!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    // 创建线程
    public class Demo1 {
        public static void main(String[] args) throws InterruptedException {
            Thread thread = new MyThread();
            // 使用start方法可以间接调用run方法
            // start和 run都时Thread的成员
            // run 只是线程的入口(描述了线程要做什么事情)
            // start才是真正调用了系统的API,在系统中创建出了线程,让线程调用 run
            thread.start();
            // 从这句开始程序就并发执行,一边执行hello main,一边执行hello Thread
            // 兵分两路进行执行
            // 并发 == 并行 + 并发
            while(true){
                System.out.println("hello main!");
                Thread.sleep(1000);
            }
            // 先执行main,再执行的是Thread,先执行主线程
        }
    }

    实现Runnable(接口),重写run

    package Thread;
    class MyRunable implements Runnable{
        public void run(){
            while(true){
                System.out.println("hello thread!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    public class Demo2 {
        public static void main(String[] args) throws InterruptedException {
            // 这个接口就是用来实现多态的
            Runnable myRunable = new MyRunable();
            Thread thread = new Thread(myRunable);
            thread.start();
            while(true){
                System.out.println("hello main!");
                Thread.sleep(1000);
            }
        }
    }

    继承Thread,重写run,但是使用匿名内部类

    1. 使用匿名内部类的方式创建出线程
    package Thread;
    public class Demo3 {
        public static void main(String[] args) {
            Thread thread = new Thread(){
                public void run(){
                    while(true){
                        System.out.println("hello Thread!");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            };
            thread.start();
            while(true){
                System.out.println("hello main!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    实现Runnable(接口),重写run,但是使用匿名内部类

    package Thread;
    public class Demo4 {
        public static void main(String[] args) {
            // 法一:创建实例
           Runnable runnable = new Runnable(){
                public void run(){
                    System.out.println("hello Thread!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            };
    js        // 法二:创建匿名对象
           Thread thread = new Thread(nesHIOSRGfPTw Runnable(){
                public void run(){
                    System.out.println("hello Thread!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            // Thread thread = new Thread(runnable);
            thread.start();
            while(true){
                System.out.println("hello main!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    使用lambda表达式

    1. lambda表达式相当于是匿名内部类的替换写法
    package Thread;
    public class Demo5 {
        public static void main(String[] args) {
          php  Thread thread = new Thread(()->{
               while(true){
                   System.out.println("hello Thread!");
                   try {
                       Thread.sleep(1000);
                   } catch (InterruptedException e) {
                       throw new RuntimeException(e);
                   }
               }
            });
            thread.start();
            while(true){
                System.out.println("hello main!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimandroideException(e);
                }
            }
        }
    }

    请说明Thread类中run和start的区别

    从方法的区别,及运行结果的区别分别说明

    1. start方法可以用来启动一个新的线程,run方法只是一个普通的方法,在主线程中执行

    2.start方法只能调用一次,run方法可以调用多次

    3.调用start方法会执行新的线程,新线程和主线程并发执行,run方法只是线程的入口,start方法调用了系统API,start方法创建出了线程,让线程再调用run方法

    4.run方法和主线程同步执行,start方法启动的线程和主线程异步执行

    5.run方法按顺序执行,start方法调用的线程中的代码执行顺序由线程调度器决定,顺序不确定

    到此这篇关于Javaee多线程之进程和线程之间的区别和联系(最新整理)的文章就介绍到这了,更多相关java进程和线程区别内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜