java面试题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java面试题相关的知识,希望对你有一定的参考价值。
最近看到一份面试题,觉得写的还不错,虽然作者只给出了题目没有答案,但是它既是一份面试题同样作者其实很好的总结了java程序员应该具备的技术体系,希望能够对初入java和迷茫不知学什么的同学以指引
由于某些原因正打算换工作,所以试着整理解答,答案只是我的想法,如有不对之处欢迎大神批评指正,
由于题目太多,我会持续添加
1. Java线程的状态
1 public enum State { 2 /** 3 * Thread state for a thread which has not yet started. 4 */ 5 NEW, 6 7 /** 8 * Thread state for a runnable thread. A thread in the runnable 9 * state is executing in the Java virtual machine but it may 10 * be waiting for other resources from the operating system 11 * such as processor. 12 */ 13 RUNNABLE, 14 15 /** 16 * Thread state for a thread blocked waiting for a monitor lock. 17 * A thread in the blocked state is waiting for a monitor lock 18 * to enter a synchronized block/method or 19 * reenter a synchronized block/method after calling 20 * {@link Object#wait() Object.wait}. 21 */ 22 BLOCKED, 23 24 /** 25 * Thread state for a waiting thread. 26 * A thread is in the waiting state due to calling one of the 27 * following methods: 28 * <ul> 29 * <li>{@link Object#wait() Object.wait} with no timeout</li> 30 * <li>{@link #join() Thread.join} with no timeout</li> 31 * <li>{@link LockSupport#park() LockSupport.park}</li> 32 * </ul> 33 * 34 * <p>A thread in the waiting state is waiting for another thread to 35 * perform a particular action. 36 * 37 * For example, a thread that has called <tt>Object.wait()</tt> 38 * on an object is waiting for another thread to call 39 * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on 40 * that object. A thread that has called <tt>Thread.join()</tt> 41 * is waiting for a specified thread to terminate. 42 */ 43 WAITING, 44 45 /** 46 * Thread state for a waiting thread with a specified waiting time. 47 * A thread is in the timed waiting state due to calling one of 48 * the following methods with a specified positive waiting time: 49 * <ul> 50 * <li>{@link #sleep Thread.sleep}</li> 51 * <li>{@link Object#wait(long) Object.wait} with timeout</li> 52 * <li>{@link #join(long) Thread.join} with timeout</li> 53 * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> 54 * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> 55 * </ul> 56 */ 57 TIMED_WAITING, 58 59 /** 60 * Thread state for a terminated thread. 61 * The thread has completed execution. 62 */ 63 TERMINATED; 64 }
1)New (新建)
当用new操作符创建一个新线程时,如 new Thread (runnable);此时该线程还没有开始
2)Runnable (可运行状态)
当线程有资格运行,但调度程序还没有把它选定为运行线程时线程所处的状态。当start()方法调用时,线程首先进入可运行状态。在线程运行之后或者从阻塞、等待或睡眠状态回来后,也返回到可运行状态。
3) Blocked (被阻塞)
当线程调用sleep()或者join()方法就会进入Blocked状态,但是要注意的是阻塞的线程是不释放当前所占有的系统资源,当sleep()结束或者join()等待其他线程来到,当前线程则进入Runnable状态等待JVM分配资源。
4)Waiting(等待)
如果当前线程调用wait()方法,则当前线程进入Time waiting但是这个时候当前线程会释放所占有的JVM资源,进入这个状态过后是不能自动唤醒的,必须调用notify()或者notifyAll()方法,线程进入Waiting。
5)Timed Waiting (计时等待)
当线程进入Runnable状态,但是还没有开始运行的时候,此时发现需要的资源处于同步状态synchronized,这个时候线程将会进入Time waiting,JVM会使用队列对这些线程进行控制,既先进行Time waiting的线程会先得到JVM资源进行执行进入Waiting
6)Terminated (被终止)
线程如果执行run() main()方法结束后,完成逻辑,线程就进入Terminated
以上是关于java面试题的主要内容,如果未能解决你的问题,请参考以下文章
经验总结:Java高级工程师面试题-字节跳动,成功跳槽阿里!