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
- finally语句块是在try或者catch中的return语句之前执行的
- 执行 System.exit 语句后,程序退出,所有finally中的代码也不会被执行
- 在finally中return,会导致try或catch中的异常无法正常抛出
- finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来, 不管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的
3、wait 和 sleep 区别
- sleep 来自 thread类,wait 来自 object类
- 调用 sleep 过程中,线程不会释放对象锁,而调用 wait 方法线程会释放对象锁
- sleep 睡眠后不会让出系统资源,而 wait 会让出系统资源(CPU)给其它线程执行
- sleep 需要指定一个睡眠时间,并在时间到的时候自动唤醒,而 wait 需要配合 notify 或 notifyAll 使用
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
- 对于静态方法,由于此时对象还未生成,所以只能采用类锁;
即:静态方法是class锁(类锁),非静态方法是this锁(对象锁) - 只要采用类锁,就会拦截所有线程,只能让一个线程访问。
- 对于对象锁(this),如果是同一个实例,就会按顺序访问,但是如果是不同实例,就可以同时访问。
- 如果对象锁跟访问的对象没有关系,那么就会都同时访问。
网友评论