一、线程状态和创建方式

一、线程状态

  通过Thread类的STATE可以看到,线程状态公分为6个,分别为:

public enum State {
    NEW,
    RUNNABLE,
    BLOCKED,
    WAITING,
    TIMED_WAITING,
    TERMINATED;
}

  初始(NEW):新创建一个线程对象,但还没有调用start方法

  运行(RUNNABLE):处于可运行状态的线程正在JVM中执行,但它可能正在等待来自操作系统的其它资源,例如处理器

  阻塞(BLOCKED):线程阻塞于synchronized锁,等待获取synchronized锁的状态

  等待(WAITING):Object.wait()、join()、LockSupport.park(),进入该状态的线程需要等待其它线程作出一些特定动作(通知或中断)

  超时等待(TIMED_WAITING):Object.wait(long)、Thread.join()、LockSupport.parkNanos,LockSupport.parkUntil,该状态不同于WAITING,它可以在指定的时间内自行返回

  终止(TERMINATED):表示线程已经执行完毕

  可以通过自带工具jconsole 打开界面管理 -> 线程  找到对应的线程,查看状态。

// RUNNABLE状态的线程
@Test
public void f2() throws InterruptedException {
    Thread thread = new Thread(() -> {
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }, "RUNNABLE状态的线程");
    thread.start();
    thread.join();
}

/**
 * BLOCKED、WAITING 状态的线程
 * thread拥有锁一直在睡眠,所以是TIMED_WAITING状态
 * thread1一直在等待锁,所以是BLOCKED状态
 */
@Test
public void f3() throws InterruptedException {
    Object obj = new Object();
    Thread thread = new Thread(() -> {
        synchronized (obj){
            try {
                TimeUnit.MINUTES.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }, "持有锁的线程");
    thread.start();
    TimeUnit.SECONDS.sleep(2);

    Thread thread1 = new Thread(() -> {
        synchronized (obj) {
            //...
        }
    }, "等待锁的线程");
    thread1.start();

    TimeUnit.HOURS.sleep(1);
}

/**
 * WAITING状态的线程
 */
@Test
public void f4() throws InterruptedException {
    Object obj = new Object();
    new Thread(() -> {
        synchronized (obj) {
            try {
                obj.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }, "WAITING线程").start();

    Thread.sleep(1000000);
}
View Code

二、创建方式

  创建线程方法比较多,可以通过继承、实现接口等方式创建。

public class Test3 {
    // 通过线程池创建线程
    @Test
    public void f5(){
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(() -> {
            System.out.println(Thread.currentThread().getName());
        });
    }

    // lambda表达式创建
    @Test
    public void f4() throws InterruptedException {
        Thread thread = new Thread(() -> {
            System.out.println(Thread.currentThread().getName());
        });
        thread.start();
        thread.join();
    }

    // 匿名类方式创建线程
    @Test
    public void f3() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
            }
        });
        thread.start();
        thread.join();
    }

    // 通过接口方式创建线程
    @Test
    public void f2() throws InterruptedException {
        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable, "runnable demo");
        thread.start();
        thread.join();
    }

    // 通过继承 Thread类实现线程
    @Test
    public void f1() throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.setName("thread demo");
        myThread.start();
        myThread.join();
    }
}

class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/wange/p/11809458.html