zoukankan      html  css  js  c++  java
  • 线程实现的两种方式的共同点探究

    一、前言

            我们在学习Java语法基础的时候,会学到实现多线程的两种方式,在面试的时候也会被问到实现线程有哪两种方式,一种是通过实践Runnable接口方法来实现,另外一种是通过继承Thread的方法来实现。这两种方法是有共同点的。

    二、实现Runnable接口

    public class NewRun implements Runnable{
    
    	@Override
    	public void run() {
    		// TODO Auto-generated method stub
    		System.out.println("Hello");
    	}
    
    }
    public class ThreadTest {
    
    	public static void main(String[] args) {
    		NewRun newRun = new NewRun();
    		Thread t = new Thread(newRun);
    		t.start();
    	}
    }

    三、继承Thread

    public class ThreadTest extends Thread{
    
    	@Override
    	public void run() {
    		System.out.println("world");
    	}
    
    	public static void main(String[] args) {
    		Thread t = new ThreadTest();
    		t.start();
    	}
    }

    四、Thread本来就是实现Runnable接口

           如果我们查看一下Thread的源码,我们会发现Thread就是实现Runnable接口

    public
    class Thread implements Runnable {
        /* Make sure registerNatives is the first thing <clinit> does. */
        private static native void registerNatives();
        static {
            registerNatives();
        }
    
        private volatile String name;
        private int            priority;
        private Thread         threadQ;
        private long           eetop;
    
        /* Whether or not to single_step this thread. */
        private boolean     single_step;
    
        /* Whether or not the thread is a daemon thread. */
        private boolean     daemon = false;
    
        /* JVM state */
        private boolean     stillborn = false;
    
        /* What will be run. */
        private Runnable target;
    
    

           在继承Thread的时候,我们重写了run()方法;实现Runnable接口的时候,我们也是重写了run()方法,然后再将重写后的Runnable放入new Thread(runnable)中,这个构造方法会将这个对象设置到自己的private Runnable target属性中。两种实现线程的方法的本质都是重写将要运行线程的run()方法。

           因此,我们还可以不分离创建线程与实现方法的代码,统一直接编写匿名内部类中

    public class ThreadTest{
    
    	public static void main(String[] args) {
    		Thread t = new Thread(new Runnable() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				System.out.println("Hello");
    			}
    			
    		});
    		t.start();
    	}
    }

          当然,写多了之后,还可以这样写:

    public class ThreadTest{
    
    	public static void main(String[] args) {
    		new Thread(new Runnable() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				System.out.println("Hello");
    			}
    			
    		}).start();;
    		
    	}
    }
    /**
     * Java8 Lambda表达式写法
     */
    public class ThreadTest{
    
    	public static void main(String[] args) {
    		new Thread(()-> {
    			System.out.println("Hello");
    		}).start();
    	}
    }

    当然,用Java8的写法比较简略,熟练的话,可以减少难么一点点开发时间,但是过度的抽象使得代码的可读性下降,如果又没有注释的话,比较影响日后代码的维护,因此不太建议使用Java8的写法。

    五、后言

          线程的实现方法很简单,但是也很基础,很重要。是学习多线程并发的基础。在面向消费者的项目、互联网的项目,多线程、高并发是家常便饭,线程并发编程尤为重要。

    Reference:

    [1] 滥好人, java多线程实现方式主要有两种:继承Thread类、实现Runnable接口, https://www.cnblogs.com/hq233/p/6278944.html

  • 相关阅读:
    从docker容器拷贝文件出来
    R csv数据集资源下载
    使用docker镜像搭建Python3 jupyter notebook环境
    用Python合并多个Excel文件
    vscode保存文件时自动删除行尾空格
    js判断数组是否包含某元素
    数据库锁问题
    协程
    进程间的八种通信方式
    进程与线程的区别
  • 原文地址:https://www.cnblogs.com/ryelqy/p/10104049.html
Copyright © 2011-2022 走看看