android线程的介绍及两种启动方式

直接查看Thread的源码代码,注释里面已经清楚的说是两种,方法已经给出来

There are two ways to create a new thread of execution. One is to
* declare a class to be a subclass of <code>Thread</code>. This
* subclass should override the <code>run</code> method of class
* <code>Thread</code>. An instance of the subclass can then be
* allocated and started. For example, a thread that computes primes
* larger than a stated value could be written as follows:

一种是直接继承Thread类:(代码直接是复制Thread类中官方给的),调用Thread自己的run方法
 

*     class PrimeThread extends Thread {
 *         long minPrime;
 *         PrimeThread(long minPrime) {
 *             this.minPrime = minPrime;
 *         }
 *
 *         public void run() {
 *             // compute primes larger than minPrime
 *             &nbsp;.&nbsp;.&nbsp;.
 *         }
 *     }

调用
 *     PrimeThread p = new PrimeThread(143);
 *     p.start();

public
class Thread implements Runnable {
    // Android-removed: registerNatives() not used on Android.
    /*
    /* Make sure registerNatives is the first thing <clinit> does. *
    private static native void registerNatives();
    static {
        registerNatives();
    }
    */

第二种是实现Runable接口

 *     class PrimeRun implements Runnable {
 *         long minPrime;
 *         PrimeRun(long minPrime) {
 *             this.minPrime = minPrime;
 *         }
 *
 *         public void run() {
 *             // compute primes larger than minPrime
 *             &nbsp;.&nbsp;.&nbsp;.
 *         }
 *     }


 *     PrimeRun p = new PrimeRun(143);
 *     new Thread(p).start();

区别:

         Thread类是真正java语言对线程的一个抽象类(java 是面向对象开发的,万物皆为对象)

        Runnable 类是对任务的抽象

    继承Thread类之后,由于 Java单继承的特点,当前的类就不能继承其他的类。但是实现     Runnable接口,就可以继承其他类。

由于Runnable是一个任务对象,所有他可以实现多个线程共享一个任务对象,而Thread不行

如何停止一个线程:

    stop()方法 ,上添加了一个 @Deprecated 废弃的注解,所以不推荐使用

 @Deprecated    
    public final void stop() {
   
   

   stop方法是一种野蛮的关闭,会导致线程所占用的资源 不会正确的释放资源。

  正确停止线程的方法:

interrupt()不是正真的中止线程,他其实是给当前线程一个中断的标志位,线程不会中止,也可以不用理会,这个由线程自己来决定是否关闭。
isInterrupted() 这个方法就是用来返回当前线程是否设置了中断的标志位,线程可以通过这个方法来判断是是否响应中断

interrupted() 也是检查当前的线程是否有中断标志位,他是一个静态的方法

public static native boolean interrupted();

分析线程的启动:

    线程的启动都是调用start()方法的;

   

 public synchronized void start() {
     
        if (started)  //当start()方法调用了两次的时候,直接抛出异常
            throw new IllegalThreadStateException();
        group.add(this);
        started = false;
        try {

            // start0();
            nativeCreate(this, stackSize, daemon);
            started = true;   //线程调用了start()方法后,  started = true; 避免start()方法多次调用
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

join()方法

  当正在执行的A线程中调用了线程B的jion()方法后,相当于A线程将cpu的执行权让给了B,等B执行完后再执行,join()方法将线程串行了。

package com.example.mylibrary;

import java.util.concurrent.Executors;

public class JVmBean {
    public static void main(String[] args) {
        System.out.println("------main------------");
        ThreadA threadA = new ThreadA();
        ThreadB threadB = new ThreadB();
        threadA.start();
        threadB.start();
//        try {
//            threadB.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("------main----end--------");
    }
  static   class ThreadA extends Thread{
        @Override
        public void run() {
            super.run();
            System.out.println("------A------------");
            System.out.println("------end------------");

        }
    }
    static  class ThreadB extends Thread{
        @Override
        public void run() {
            super.run();
            System.out.println("------B------------");
            try {
                Thread.currentThread().sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("------B-----end-------");
        }
    }






}

输出结果:

------main------------
------main----end--------
------A------------
------end------------
------B------------
------B-----end-------

然后我们让B加入join方法后:很明显主线程得等B线程执行完后再执行

   ------main------------
------A------------
------end------------
------B------------
------B-----end-------
------main----end--------

yield()这是一个静态方法,表示当前线程放弃cpu执行权,进入抢占cpu队列。
public static native void yield();

Guess you like

Origin blog.csdn.net/xueyoubangbang/article/details/125272030