【多线程学习笔记整理】001_多线程技能

一.线程和进程的区别

  首先我们引入百科上对进程的解释

  

  进程粗暴一点的理解可以理解为一个程序,每个进程都有自己的内存空间,用户每启动一个进程,操作系统就会给它分配一个独立的内存空间,而线程呢可以说是进程里面的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程序计数器,一组寄存器和栈),但是它可与同属一个进程的其他的线程共享进程所拥有的全部资源。

二.使用多线程

  java中使用多线程有两种方式,一种是继承Thread类,另一种是实现Runnable接口,实际上Thread是实现了Runnable接口的。对于这两种方式,我认为通过实现Runnable接口来使用多线程比较好,毕竟java不支持多继承,当你继承Thread类来使用多线程的时候便不能继承别的类了,而通过接口方式来使用多线程的话,还能继续实现别的接口或者继承别的类。

  1.使用继承Thread方式来使用多线程,代码如下

  static class ThreadA extends Thread{
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "通过继承Thread类来使用多线程!");
        }
    }

  //调用
  ThreadA a1 = new ThreadA();
  a1.start();

  2.使用Runnable方式来使用多线程,代码如下

  static class ThreadB implements Runnable{
        public void run() {
            System.out.println(Thread.currentThread().getName() + "通过实现Runnable来使用多线程!");
        }
    }
   //调用
   ThreadB runnable = new ThreadB();
   Thread b1 = new Thread(runnable);
   b1.start();

 

 三.Thread.currentThread().getName()和this.getName()的区别

  我们直接通过几个栗子来说明,首先定义一个Thread类

  public class TestCurrentThread extends Thread{

      public TestCurrentThread(){
          System.out.println("构造方法中currentThread.getName():" + Thread.currentThread().getName());
          System.out.println("构造方法中this.getName():" + this.getName());
      }

      @Override
      public void run() {
          System.out.println("run方法中currentThread.getName():" + Thread.currentThread().getName());
          System.out.println("run方法中this.getName():" + this.getName());
      }
  }

  在测试类中进行测试

  1.第一种

  TestCurrentThread testCurrentThread = new TestCurrentThread();
  testCurrentThread.start();
  
  运行结果

    构造方法中currentThread.getName():main
    构造方法中this.getName():Thread-0
    run方法中currentThread.getName():Thread-0
    run方法中this.getName():Thread-0

  2.第二种

  TestCurrentThread testCurrentThread = new TestCurrentThread();
  Thread t1 = new Thread(testCurrentThread);
t1.setName("A");   t1.start();
  运行结果

    构造方法中currentThread.getName():main
    构造方法中this.getName():Thread-0
    run方法中currentThread.getName():A
    run方法中this.getName():Thread-0

       这两种情况中因为构造方法的调用都是通过main方法调用的,所以构造方法中的currentThread()是主线程,getName()就是main了,而run方法,第一种情况中调用run()的是testCurrentThread,所以currentThread().getName()是Thread-0,而第二种情况中调用run()方法的是t1线程,t1线程的名字是A,所以结果是A。

       至于this.getName()方法,都是获取当前testCurrentThread对象的名字,所以都是Thread-0

四、Thread.sleep()方法

    Thread.sleep()方法,作用是让当前正在执行的线程,即this.currentThread()线程睡眠指定的毫秒数。 栗子如下:

    public class SleepThread extends Thread {
        @Override
        public void run() {
            try {
                System.out.println("SleepThread run begin at: " + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("SleepThread run end at: " + System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public class TestClass {
        public static void main(String[] args) {
            System.out.println("main begin at : " + System.currentTimeMillis());
            Thread sleepThread = new SleepThread();
            sleepThread.start();
            System.out.println("main end at : " + System.currentTimeMillis());
        }
    }

    运行结果:
    main begin at : 1537415184639
    main end at : 1537415184640
    SleepThread run begin at: 1537415184685
    SleepThread run end at: 1537415186686

  

原文地址:https://www.cnblogs.com/carryLess/p/9643304.html