Runnable与Thread类详解

2020-03-05  本文已影响0人  eliteTyc

Runnable类是一个接口,包含一个抽象方法,run

public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}

Thread类实现了Runnable接口同时包含以下方法

/**
    * The minimum priority that a thread can have.
    * 最小优先级
    */
   public final static int MIN_PRIORITY = 1;

  /**
    * The default priority that is assigned to a thread.
     * 默认优先级
    */
   public final static int NORM_PRIORITY = 5;

   /**
    * The maximum priority that a thread can have.
     * 最大优先级
    */
   public final static int MAX_PRIORITY = 10;
public enum State {
        // 线程新建了,但是还没有start
        NEW,
       // 线程执行起来了,就是调用了线程的start方法之后
        RUNNABLE,
      // 阻塞状态,当多个线程执行同一个同步代码块儿时,同步代码快被其他线程占用,当前线程就为阻塞态
        BLOCKED,
      // 等待 wait(),join()等方法调用了之后,即不知道等待多久
        WAITING,
      // 带时间的等待,wait(time),join(time)方法调用了之后,即知道等待多久
        TIMED_WAITING,
      // 线程执行完成
        TERMINATED;
    }

创建线程的方式
1.继承Thread方式实现,重写run方法

class MyThread extends Thread {

    @Override
    public void run() {
        super.run();
        for (int i = 0; i < 100; i++) {
            if (i%2==0){
                System.out.println(i);
            }
        }
    }
}

2.实现Runnable接口的方式实现

/**
 * create by elitetyc
 * date:2020/3/5
 * desc:
 * 1.创建类实现Runnable接口
 * 2.实现run方法
 * 3.创建实例对象
 * 4.将实例对象传递给Thread构造函数,新建Thread对象,并调用start方法
 **/

//步骤1
public class RunnableTest implements Runnable {

//    步骤2
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i%2==0){
                System.out.println(i);
            }

        }
    }

    public static void main(String[] args) {
//        步骤3
        RunnableTest runnableTest = new RunnableTest();
//        步骤4
        new Thread(runnableTest).start();
    }
}

比较两种创建线程的方式
开发中优先选择实现Runnable方式来创建线程

/**
 * create by elitetyc
 * date:2020/3/5
 * desc:创建3个窗口卖票,一共100张票
 **/


public class RunnableWindowTest{
    public static void main(String[] args) {
        RunnableWindow runnableWindow = new RunnableWindow();
        new Thread(runnableWindow,"窗口1").start();
        new Thread(runnableWindow,"窗口2").start();
        new Thread(runnableWindow,"窗口3").start();

    }
}


class RunnableWindow implements Runnable {

    private int ticket=100;

    @Override
    public void run() {
//        同步锁,防止多线程安全问题
        synchronized (this){
            while (true){
                if (ticket>0){
                    System.out.println(Thread.currentThread().getName()+":卖出票号"+ticket);
                    ticket--;
                }else {
                    break;
                }
            }
        }
    }


}

上一篇 下一篇

猜你喜欢

热点阅读