茫茫人海千千万万,感谢这一秒你看到这里。希望我的面试题系列能对你的有所帮助!共勉!
愿你在未来的日子,保持热爱,奔赴山海!
每日三道面试题,成就更好自我今天我们继续聊聊多线程的话题吧!
1. 昨天你讲到创建线程后使用start方法去调用线程,为什么run方法不行呢?有什么区别?这道题也是非常经典的一道题,虽然难度不大,但是突然忘了,也就答不上来了。
我们先来看看代码吧。
public class ThreadDemo { public static void main(String[] args) { MyThread myThread = new MyThread(); MyThread myThead2 = new MyThread(); // myThread.start(); // myThead2.start(); myThread.run(); myThead2.run(); } } class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 6; i++) { System.out.println(Thread.currentThread().getName() + " :" + i); try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }这里我们创建了MyThread继承了Thread类,这种方法是一种可以创建线程的方式。接着我们在main方法中创建了两个线程,都调用了start方法和run方法。让我们先看看结果吧!
// 注释掉两个run方法 开启start方法得到的结果 Thread-0 :0 Thread-1 :0 Thread-1 :1 Thread-0 :1 Thread-1 :2 Thread-0 :2 Thread-1 :3 Thread-0 :3 Thread-1 :4 Thread-0 :4 Thread-1 :5 Thread-0 :5 // 注释掉两个start方法 开启run方法得到的结果 main :0 main :1 main :2 main :3 main :4 main :5 main :0 main :1 main :2 main :3 main :4 main :5接下来我们讲一下:
start方法的作用:
启动线程,相当于开启一个线程调用我们重写的run方法里面的逻辑,此时相当于有两个线程,一个main的主线程和开启的子线程。可以看到我们的代码,相当于有三个线程,一个主线程、一个Thread-0线程和一个Thread-1线程。并且线程之间是没有顺序的,他们是抢占cpu的资源来回切换的。
run方法的作用:
执行线程的运行时代码,相当于我们只是单纯的调用一个普通方法。然后通过主线程的顺序调用的方式,从myThread调用run方法结束后到myThread2去调用run方法结束,并且我们也可以看到我们控制台中的线程名字就是main主线程。
run方法我们可以重复调用,而start方法在一个线程中只能调用一次。即myThread这个实例对象只能调用一次start方法,如果再调用一次start方法的话,就会抛出IllegalThreadStateException 的异常。
我们调用start方法算是真正意义上的多线程,因为它是额外开启一个子线程去调用我们的run方法了。如果我们是调用run方法,就需要等待上一次的run方法执行完毕才能调用下一次。所以我们要调用start方法充分挥多核CPU的优势,采用多线程的方式去同时完成几件事情而不互相干扰。
妙啊,妙花种子妙妙秒啊!
2. 你知道你开启一个线程后,它的状态有那些吗?我们可以通过查看Thread的源码中State枚举发现有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, /** * 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, /** * 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, /** * 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, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }接下来我们具体来说说吧:
NEW(新建)
线程刚被创建,还只是一个实例对象,并未调用start方法启动。。MyThread myThread = new MyThread只有线程对象,没有线程特征。
Runnable(可运行)