synchronized 关键字

[ JAVA ]

它包括两种用法:synchronized 方法和 synchronized 块。
1. synchronized 方法:通过在方法声明中加入 synchronized关键字来声明 synchronized 方法。如:
public synchronized void accessVal(int newVal);
synchronized 方法控制对类成员变量的访问:每个类实例对应一把锁,每个 synchronized 方法都必须

获得调用该方法的类实例的锁方能执行,否则所属线程阻塞,方法一旦执行,就独占该锁,直到从该方法

返回时才将锁释放,此后被阻塞的线程方能获得该锁,重新进入可执行状态。这种机制确保了同一时刻对

于每一个类实例,其所有声明为 synchronized 的成员函数中至多只有一个处于可执行状态(因为至多只...

so help me to get where i belong......i love u...forever....

java类 构造,初始化 执行顺序

[ JAVA ]

给几个例子:

第一个如下:

---有继承存在的情况下

class A {
        public A() {
                System.out.println("enter A()");
                init();
                System.out.println("exit A()");
        }
        public void init() {
                System.out.println("enter A.init");
                System.out.println("exit A.init");
        }
}
public class B extends A {
        public B() {
                System.out.println("enter B()");
                System.out.println("exit B()");
        }
        int i;
        int s = inits();
        public static int inits() {
                System.out.println("enter B.inits");
                System.out.println("exit B.inits");
                return 0;
        }
        public void init() {
                System.out.println("enter B.init");
                i = 100;
                s = 100;
                System.out.println("exit B.init");
        }
        public void println() {
                System.out.println("enter B.println");
                System.out.println(i);
                System.out.println(s);
                System.out.println("exit B.println");
        }
        public static void main(String[] arg) {
                new B().println();
        }
}

 运行结果:

enter A()
enter B.init
exit B.init
exit A()
enter B.inits
exit B.inits
enter B()
exit B()
enter B.println
100
0
exit B.println

由此可以看出大致执行顺序如下:
main的new B()
->class B的public B()的第一行(首先调用基类构造函数,隐含的super()调用),第二行还没执行又
->class A的public A()第一行,第二行init()去调用class B的init()而不是class A的init()所以
  这里i=100,s=100(运行时多态性),public A()完了之后
->public B()的第一行,下面先执行实例变量的初始化。(此处在下面继续讨论)
  下来是s=inits()结果s=0,i没变还是100,最后才执行public B()的两条输出,到这里new B()才算完,
  下面就是B的println()。

第二个例子:

-------int i; 和 int i = 0;的区别

class Base {
 Base() {
  System.out.println("Base() before print()");
  print();
  System.out.println("Base() after print()");
 }

 public void print() {
  System.out.println("Base.print()");
 }
}

class Derived extends Base {
 int value = 100;

 Derived() {
  System.out.println("Derived() With " + value);
 }

 public void print() {
  System.out.println("Derived.print() with " + value);
 }
}

public class Main {
 public static void main(String[] args) {
  new Derived();
 }
}

运行结果如下:

Base() before print()
Derived.print() with 0
Base() after print()
Derived() With 100

int i; != int i = 0;
一般的初学者都会认为两者是相同的。
但是实际上不但是在顺序上不一样,而且javac对两者的编译是完全不一样。
前者只是申明一个变量,在初始化对象变量(这里指int i = 0;)的时候并不会编译成初始化指令。
而这些初始化对象变量的指令,会在本类构造函数里面的第一条指令(注意不是构造函数之前)
之前执行,而在此之前可能已经执行了父类的构造函数。
所以我们不难推出最开始那个例子的结果为什么一个是100,一个是0。

还有要注意的是构造函数实际上并没有分配空间(尽管我们通常都会认为)。
对于一般的对象生成(用new关键字,其他情况要另外分析)。
javac会把它编译成new #number 这个指令,#number指向的是类在常数池的索引。
这个new指令就是分配对象空间,并根据类里面所声明的变量进行空间分配,
并把他们赋值成初始化的值(就是大家都知道的,int(0),objct(null))。

举个简单的例子。对于一般的语句:比如说new A();
实际上执行顺序如下:
        new #A的索引
//然后是下面大括号的指令,它们都是A的构造函数(这里的构造函数并不等同于我们代码
                                里面的public A() {.. },实际上是大于,然后
                                根据里面的代码生成A的构造函数字节代码段。)
        {
         执行父类构造函数字节代码段
         本类对象变量的初始化指令(比如int i = 10;这些指令是在编译时确定的)
         然后下面的指令就是public A() {...}里面代码的指令
              {
                ...
                ...
              }
        }

实际上,假如你只是在类申明了int i;而在以后的代码都不引用它的话,
javac是不会把它编译到class里面的。这也许是javac的优化结果。

第三个例子:

------一个恶心的例子

public class Singleton{

/*1)*/ private static Singleton obj = new Singleton();
/*2)*/ public static int counter1;
/*3)*/ public static int counter2 = 0;
 
/*4)*/ private Singleton(){
  counter1++;
  counter2++;
 }
 
/*5)*/ public static Singleton getInstance(){
  return obj;
 }
 
 public static void main(String[] args) {
  // TODO 自动生成方法存根
  Singleton  obj = Singleton.getInstance();
  System.out.println("obj.counter1=="+obj.counter1);
  System.out.println("obj.counter2=="+obj.counter2);
 }

}

执行顺序:4   2 3 1 5

解释:

这个例子第一次接触是一个公司的面试题,第一次看百思不得其解。在网上搜了下,才发现。。。

如果你不知道,就看其他人写的吧。链接如下,

http://www.cnjsp.org/view.jsp?column=2&id=717

言简意赅的阐述(摘):

      当class具有static field,且直接在声明处透过「=...」的方式设定其值时, 
编译器会自动将这些叙述依序搬到class constructor内。同样地,当class具有instance 
 field
,且直接在声明处透过「=...」的方式设定其值时,编译器会自动将这些叙述依序 
搬到instance constructor内。  

--什么是class constructor和instance constructor?

+ class constructor是我们常用的静态块,如:

    static{

    }

+ instance constructor就是我们的构造方法。


so help me to get where i belong......i love u...forever....

try-catch-finally

[ JAVA ]

Why Finally?

A Finally block will be executed after a try block if no exception has been thrown or after a catch if an exception was thrown. This means that a finally block can be used as 'clean up' - a place to close files or connections etc, whether an exception is thrown or not.

If a try block throws an exception and the catch block propagates the exception (throws it again), the finally clause will still execute. If the finally clause executes a return statement, it overides a thrown exception (so the exception will not be thrown; instead the return will occur).

1。finally块在以下情况下执行:

     try块执行完毕且没有抛出异常;

     try块执行,抛出异常,catch块自己处理该异常后;

     try块执行,抛出异常,catch块将该异常向上抛出;(如果fanally块中有返回的声明,那么该异常将不被抛出,相反,将该声明返回)

 

    注意:无论try块,还是catch块中是否有语句 return; fanally块都会在return到主程序之前,执行。


so help me to get where i belong......i love u...forever....

关于java基本数据类型

[ JAVA ]

1。共8种:

byte , short , int , long, float, double, char, boolean

数据类型 大小 范围/精度

float 4 字节 32位IEEE 754单精度

double 8 字节 64位IEEE 754双精度

byte 1字节 -128到127

short 2 字节 -32,768到32,767

int 4 字节 -2,147,483,648到2,147,483,647

long 8 字节 -9,223,372,036,854,775,808到9,223,372,036, 854,775,807

char 2 字节 整个Unico...

so help me to get where i belong......i love u...forever....

java中父类和子类的强转问题。。。

[ JAVA ]

如下代码:

public class A {
 public static void main(String[] args){
  A a = new A();
  B b = (B)a;  //执行该语句会抛ClassCastException异常。
  
  B b2 = new B();
  a = (A)b2;
  System.out.println("success");
 }
}
class B extends A{ 
}

 

-...

so help me to get where i belong......i love u...forever....

n=n++;

[ JAVA ]

1。 

public static void cycleTest(){

       int n...

so help me to get where i belong......i love u...forever....

java中的四舍五入实现

[ JAVA ]

 public static double round(double v, int scale) {
  if (scale < 0) {
   throw new IllegalArgumentException(
     "The scale must be a positive integer or zero! ");
  }
  BigDecimal b = new BigDecimal(v);
  BigDecimal one = new BigDecimal("1");
 &nb...

so help me to get where i belong......i love u...forever....

java中的集合

[ JAVA ]

 

以上是常见的接口和类的关系图。

----集合类存放的是对象的引用,而非对象本身。

so help me to get where i belong......i love u...forever....

类与类的关系

[ JAVA ]

与类之间存在以下关系:
(1)泛化(Generalization)
(2)关联(Association)
(3)依赖(Dependency)
(4)聚合(Aggregation)
               &...

so help me to get where i belong......i love u...forever....

类的设计原则

[ JAVA ]

1。开闭原则(Open-Close Principle)

    --软件应该对扩展开放,对修改关闭。

2。里氏代换原则(Liskov Substitution Principle)

    --任何基类出现的地方,都可以用子类去替换,反之则不成立。

3。依赖倒转原则(Dependence Inversion Principle)

    --要依赖于抽象,不依赖于具体。

4。接口隔离原则(Interface Segregation Principle)

    --使用多个专门的接口要比使用单一的总接口好。

    --一个类对另一个类的依...

so help me to get where i belong......i love u...forever....

分页共1页 1