杂篇一

2021-03-08  本文已影响0人  狼性代码人

1、静态属性和静态方法是否可以被继承
2、try catch finally 语句块中 return 问题
3、wait 和 sleep 区别
4、死锁产生条件
5、synchronized(this)、synchronized(class)、synchronized method区别

1、静态属性和静态方法是否可以被继承

public class Base {
    public static String staticValue = "Base static value";
    public static String staticMethod() { return "Base static method"; }
}

public class ChildA extends Base { }

public class ChildB extends Base {
    public static String staticValue = "Child static value";
    public static String staticMethod() { return "Child static method"; }
}

public void main(String[] args) {
    ChildA a = new ChildA();
    System.out.println(a.staticValue);
    System.out.println(a.staticMethod());
    ChildB b = new ChildB();
    System.out.println(b.staticValue);
    System.out.println(b.staticMethod());
    Base base = new ChildB();
    System.out.println(base.staticValue);
    System.out.println(base.staticMethod());
}

---结果-----------------------------------------------------------------
Base static value
Base static method
Child static value
Child static method
Base static value
Base static method

2、try catch finally 语句块中 return 问题

// finally语句块是在try或者catch中的return语句之前执行的
public static String tryCatchA(int value) {
    int data = value;
    try {
        System.out.println("Run code in try");
        if (data == 1)
            return "Try return";
        else
            throw new IllegalArgumentException();
    } catch (Exception e) {
        System.out.println("Run code in catch");
        return "catch return";
    } finally {
        System.out.println("Run code in finally");
        return "finally return";
    }
}

public static void main(String[] args) {
    System.out.println(tryCatchA(1));
    System.out.println(tryCatchA(2));
}

---结果-----------------------------------------------------------------
Run code in try
Run code in finally
finally return

Run code in try
Run code in catch
Run code in finally
finally return
// finally 语句不会被执行
public static String tryCatchB() {
    try {
        System.exit(0);
        return "try return";
    } catch (Exception e) {
        return "catch return";
    } finally {
        return "finally return";
    }
}

public static void main(String[] args) {
    System.out.println(tryCatchB());
}

---结果-----------------------------------------------------------------
程序在System.exit(0)之后退出,finally语句没有被执行
// 在finally中return,会导致try或catch中的异常无法正常抛出
public static String tryCatchC() {
    try {
        System.out.println("Run code in try");
        throw new NullPointerException();
    } catch (Exception e) {
        System.out.println("Run code in catch");
        throw new NullPointerException();
    } finally {
        System.out.println("Run code in finally");
        return "Run code in return";
    }
}

public static String tryC() {
    try {
        System.out.println("Run code in try");
        throw new NullPointerException();
    } finally {
        System.out.println("Run code in finally");
        return "Run code in return";
    }
}

public static void main(String[] args) {
    try {
        System.out.println(tryCatchC());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    try {
        System.out.println(tryC());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

---结果-----------------------------------------------------------------
Run code in try
Run code in catch
Run code in finally
Run code in return

Run code in try
Run code in finally
Run code in return
// finally是在return后面的表达式运算后执行的
//(此时并没有返回运算后的值,而是先把要返回的值保存起来,
//  不管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),
// 所以函数返回值是在finally执行前确定的
public static String tryCatchD() {
    int x = 1;
    try {
        x++;
        System.out.println("Try statements x = " + x);
        return "Try return x = " + x;
    } finally {
        System.out.println("Finally statements ++x 前 x = " + x);
        ++x;
        System.out.println("Finally statements ++x 后 x = " + x);
    }
}

public static void main(String[] args) {
    System.out.println(tryCatchD());
}

---结果-----------------------------------------------------------------
Try statements x = 2
Finally statements ++x 前 x = 2
Finally statements ++x 后 x = 3
Try return x = 2

3、wait 和 sleep 区别

4、死锁产生条件

public static class DeadLock implements Runnable {
    private static final Object a = new Object();
    private static final Object b = new Object();

    private boolean flag = false;

    public DeadLock(boolean flag) { this.flag = flag; }

    public void lockA() {
        synchronized (a) {
            System.out.println("lockA ------- synchronized(a)");
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (b) {
                System.out.println("lockA ------- synchronized(b)");
            }
        }
    }

    public void lockB() {
        synchronized (b) {
            try {
                System.out.println("lockB ------- synchronized(b)");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (a) {
                System.out.println("lockB ------- synchronized(a)");
            }
        }
    }

    @Override
    public void run() { if (flag) lockA(); else lockB(); }
}

public static void main(String[] args) {
    new Thread(new DeadLock(true)).start();
    new Thread(new DeadLock(false)).start();
}

5、synchronized(this)、synchronized(class)、synchronized method区别

实例:https://www.cnblogs.com/huansky/p/8869888.html

上一篇 下一篇

猜你喜欢

热点阅读