线程创建的方式之(继承Thread类和实现Runnable接口)

1.继承Thread

步骤:

  1. 定义Thread类的子类,并重写该类的run()方法,该run()方法的方法体就代表了线程需要完成的任务,因此把run()方法称为线程执行体。
  2. 创建Thread子类的实例,即创建了线程对象。
  3. 调用线程对象的start()方法来启动该线程。
public class TextThead1 extends Thread {
    @Override
    public void run() {
        //run方法方法体
        for (int i = 0; i < 60; i++) {
            System.out.println("run方法"+i);
        }
    }

    public static void main(String[] args) {
        TextThead1 th1 = new TextThead1();
        th1.start();
        for (int i = 0; i < 500; i++) {
            System.out.println("主线程"+i);
        }
    }
}

2.实现Runnable接口

步骤:

  1. 定义Runnable接口的实现类,并重写该接口的run()方法,该run()方法的方法体同样是该线程的线程执行体。
  2. 创建Runnable实现类的实例,并以此实例作为Thread的target来创建Thread对象,Thread对象才是真正的线程对象。
  3. 调用线程对象的start()方法来启动线程。

public class implRunnable implements Runnable{
    @Override
    public void run() {
        //run方法方法体
        for (int i = 0; i < 60; i++) {
            System.out.println("run方法"+i);
        }
    }

    public static void main(String[] args) {
        implRunnable implrunnable =new implRunnable();
/*        Thread thread =new Thread(implrunnable);
        thread.start();*/
        new Thread(implrunnable).start();
        for (int i = 0; i < 500; i++) {
            System.out.println("主线程"+i);
        }
    }
}

***继承Thread与实现Runnable两种方式的比较

Thread类本质上是实现了Runnable接口的一个实例,代表一个线程的实例。

 * Unless otherwise noted, passing a {@code null} argument to a constructor
 * or method in this class will cause a {@link NullPointerException} to be
 * thrown.
 *
 * @author  unascribed
 * @see     Runnable
 * @see     Runtime#exit(int)
 * @see     #run()
 * @see     #stop()
 * @since   JDK1.0
 */
public
class Thread implements Runnable {
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
        registerNatives();
    }

实现Runnable接口比继承Thread类所具有的优势

  1. 适合多个相同的程序代码的线程去共享同一个资源。
  2. 可以避免java中的单继承的局限性。
  3. 增加程序的健壮性,实现解耦操作,代码可以被多个线程共享,代码和数据独立。
  4. 线程池只能放入实现Runable或callable类线程,不能直接放入继承Thread的类。

在线程的生命周期一共有6种状态

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,(新建) 线程刚被创建,但是并未启动。

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,(可运行)
线程可以在java虚拟机中运行的状态,可能正在运行自己代码,也可能没有,这取决于操作系统处理器。


        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,阻塞当一个线程试图获取一个对象锁,而该对象锁被其他的线程持有,
        则该线程进入Blocked状态;当该线程持有锁时,该线程将变成Runnable状态。
          *线程的线程状态被阻塞,等待监视器锁定。
          *处于阻塞状态的线程正在等待监视器锁定
          *输入同步块/方法或
          *调用后重新输入同步块/方法

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,(无限等待)
 一个线程在等待另一个线程执行一个(唤醒)动作时,该线程进入Waiting状态。
进入这个状态后是不能自动唤醒的,必须等待另一个线程调用notify或者notifyAll方法才能够唤醒。

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,(计时等待)
同waiting状态,有几个方法有超时参数,调用他们将进入Timed Waiting状态。
这一状态将一直保持到超时期满或者接收到唤醒通知。带有超时参数的
常用方法有Thread.sleep 、Object.wait。
        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;(被终止)
  因为run方法正常退出而死亡,或者因为没有捕获的异常终止了run方法而死亡
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值