Java学习【经典面试题: 写出你见过的运行时异常。】

来源:互联网 发布:ubuntu安装wps office 编辑:程序博客网 时间:2024/06/05 10:33

运行时异常:(运行时异常都是RuntimeException的子类)
异常的中文名,以及对应的例子
1.NullPointerException 空指针异常
这里写图片描述

2.ArithmeticException 数学异常
这里写图片描述

3.ClassCastException 类型转换异常
这里写图片描述

4.ArrayIndexOutOfBoundsException 数组下标越界异常
这里写图片描述

5.StringIndexOutOfBoundsException 字符串下标越界
这里写图片描述

6.IndexOutOfBoundsException 下标越界异常
这里写图片描述

7.NumberFormatException 数字格式化异常
这里写图片描述

8.InputMismatchException 输入不匹配异常
这里写图片描述

9.ArrayStoreException 数组协变异常
这里写图片描述

10.ConcurrentModificationException 快速报错异常
略: 当打开一个迭代器以后,一直到迭代完毕之前,不能给集合中删除、增加任何元素,否则会抛出该异常。

11.UnsupportedOperationException 不支持该操作异常
这里写图片描述

12.NoSuchElementException 没有这么一个元素异常
这里写图片描述

13.IllegealMonitorStateException 非法监听器状态异常

package com.westos.test;class A implements Runnable {    public void run() {        while(true) {            System.out.println("AAAA");            System.out.println("BBBB");            System.out.println("CCCC");            System.out.println("DDDD");        }    }}class B implements Runnable {    public void run() {        while(true) {            System.out.println("1111");            System.out.println("2222");            System.out.println("3333");            System.out.println("4444");        }    }}public class App {    public static void main(String[] args) {        Thread th = new Thread(new A());        Thread th2 = new Thread(new B());        th.start();        th2.start();    }}

此时看到的结果是,ABCD与1234,互相打断对方。 打断对方,这样做不好,不应该这样。比如,洗碗机,洗碗功能,烘干功能。 这两个功能一定不能打断对方。

我们要保证,ABCD和1234不能从中间打断对方,要打断也是在ABCD输出完毕以后,或者1234输出完毕以后才打断! 这就要使用线程的同步了! 同步就保证了某段代码的原子性。

package com.westos.test;class A implements Runnable {    private Object obj;    public A(Object obj) {        this.obj = obj;    }    public void run() {        while(true) {            synchronized (obj) {  // 进入同步块,就会获取obj的锁, 锁只能被一个线程占有                System.out.println("AAAA");                System.out.println("BBBB");                System.out.println("CCCC");                System.out.println("DDDD");            } // 出了同步块,就会自动释放锁        }    }}class B implements Runnable {    private Object obj;    public B(Object obj) {        this.obj = obj;    }    public void run() {        while(true) {            synchronized (obj) {  // 进入同步块,就会获取obj的锁, 锁只能被一个线程占有                System.out.println("1111");                System.out.println("2222");                System.out.println("3333");                System.out.println("4444");            } // 出了同步块,就会自动释放锁        }    }}public class App {    public static void main(String[] args) {        String str = new String();        Thread th = new Thread(new A(str));        Thread th2 = new Thread(new B(str));        th.start();        th2.start();    }}

以上代码保证了原子性,也就是加上了同步! 结果就是ABCD和1234不会从中间打断对方,但是仍然能看见连续的ABCD和1234。

我们还要保证,一个线程轮流执行一次! 一次ABCD ,一次1234 , 线程直接的协调/交互。

package com.westos.test;class A implements Runnable {    private Object obj;    public A(Object obj) {        this.obj = obj;    }    public void run() {        while(true) {            synchronized (obj) {  // 进入同步块,就会获取obj的锁, 锁只能被一个线程占有                System.out.println("AAAA");                System.out.println("BBBB");                System.out.println("CCCC");                System.out.println("DDDD");                try {                    obj.notify();                    // 立即释放锁和cpu,线程进入阻塞状态                    obj.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }              } // 出了同步块,就会自动释放锁        }    }}class B implements Runnable {    private Object obj;    public B(Object obj) {        this.obj = obj;    }    public void run() {        while(true) {            synchronized (obj) {  // 进入同步块,就会获取obj的锁, 锁只能被一个线程占有                System.out.println("1111");                System.out.println("2222");                System.out.println("3333");                System.out.println("4444");                try {                    obj.notify();                    // 立即释放锁和cpu,线程进入阻塞状态                    obj.wait();                } catch (InterruptedException e) {                    e.printStackTrace();                }              } // 出了同步块,就会自动释放锁        }    }}public class App {    public static void main(String[] args) {        String str = new String();        Thread th = new Thread(new A(str));        Thread th2 = new Thread(new B(str));        th.start();        th2.start();    }}

此时保证了协调。

当在同步块之外,使用锁时,就会抛出,IllegealMonitorStateExecption.

14.EmptyStackException 空栈异常
这里写图片描述

原创粉丝点击