2006年7月17日

一直有留意Firefox对IE的冲击,不过以前用IE也并没觉得有什么大问题,就一直用下来。

直到最近的IE老出问题,公司派的本本装的是win2000,用久了里面的IE经常会弹出一个crash的对话框(似乎在XP里就很少见),然后所有打开的IE窗口都只能关闭,解决无方,于是想到了Firefox,试用一下,第一感觉是非常快,不管是load firefox还是打开新的网页,基本上都是文字刷一下就出来,图片则延迟了点,不过还是比IE做得好:) 很多小的细节都考虑得比较符合用户的操作习惯,比如工具栏有一个下载的管理器,管理最近下载的咚咚,不用第三方软件,而且Firefox比IE小巧多了(4.8M),Simple is beautiful,呵呵,I like it.
posted @ 2006-08-20 23:26 响 阅读(153) | 评论 (1)编辑 收藏
 

Q If Java uses the pass-by reference, why won't a swap function work?

AYour question demonstrates a common error made by Java language newcomers. Indeed, even seasoned veterans find it difficult to keep the terms straight.

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

Take the badSwap() method for example:


public void badSwap(int var1, int var2)
{
  int temp = var1;
  var1 = var2;
  var2 = temp;
}

When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type from int to Object, since Java passes object references by value as well.

Now, here is where it gets tricky:


public void tricky(Point arg1, Point arg2)
{
  arg1.x = 100;
  arg1.y = 100;

  Point temp = arg1;
  arg1 = arg2;
  arg2 = temp;
}

public static void main(String [] args)
{
  Point pnt1 = new Point(0,0);
  Point pnt2 = new Point(0,0);
  System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);
  System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
  System.out.println(" ");
  tricky(pnt1,pnt2);
  System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);
  System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);  
}

If we execute this main() method, we see the following output:


X: 0 Y: 0
X: 0 Y: 0

X: 100 Y: 100
X: 0 Y: 0

The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to a method.


Figure 1. After being passed to a method, an object will have at least two references

Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.


Figure 2. Only the method references are swapped, not the original ones

O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" As a result, you cannot write a standard swap method to swap objects.

Resources

posted @ 2006-08-17 22:00 响 阅读(279) | 评论 (0)编辑 收藏
 
http://www.physics.orst.edu/~rubin/nacphy/brian/socket.html
另外关于Java network的一些基础的教程:
http://java.sun.com/docs/books/tutorial/networking/index.html
posted @ 2006-08-17 10:01 响 阅读(682) | 评论 (0)编辑 收藏
 
http://www-128.ibm.com/developerworks/cn/java/j-jtp05273/
不过翻译得比较烂,还是看英文吧:
http://www-128.ibm.com/developerworks/library/j-jtp05273.html
posted @ 2006-08-10 11:02 响 阅读(216) | 评论 (0)编辑 收藏
 
 很老的文章,不过关于事务的阐述还是比较全面的。全文可见:http://www.javaworld.com/jw-07-2000/jw-0714-transaction.html
一个事务可以定义为由一组操作组成的不可分割的单元,它们要么全部被执行,要么全部不执行以保持事数据完整性。比如,从你的支票帐户到你的储蓄账户进行100美元的转帐包含以下两步操作:从支票账户登记借出100美元,然后在储蓄账户添加100美元。这两步必须同时被执行或者不执行以保护数据的完整性和一致性,以及银行和客户之间的利息核算。因此,这两步的操作组成了一个事务。


事务的属性

所有的事务都具备以下几个属性:原子性、一致性、独立性以及持久性(缩写为ACID)。

原子性:意味着不可分割,一组操作是不可分割的,我们称之为原子的。

一致性:一个事务的转换必须是把持久化的数据从一个一致的状态转换到另一个一致的状态,任何发生在其间的转换失败,都必须保证数据能够返回转换之前的那个状态。

独立性:  事务之间应互不影响,也就是说,一个处于未完成状态的事务(未commit或rollback),必须独立于其他的事务;即使几个事务同时运行,对其中每一个事务来讲,其他的事务也必须在它之前或者之后完成,所有的并发事务都必须按照实际的顺序依次完成。

持久性:一旦一个事务成功地提交完成之后(commit),由该事务所带来的状态的改变必须是稳定和持久的,即使在提交完成之后发生了其他的错误也不受影响。

因此,一个事务运行的最终结果只有两个:commit ——每一步操作的成功执行;rollback——由于执行过程中出现了错误,rollback保证没有一步操作执行。



事务独立性的等级

事务的独立性用来衡量并发事务中一个事务查看另外一个事务已经update但尚未commit的数据的能力,如果某些事务允许其他事务读取它已经upadte但尚未commit的数据,这些事务可能会以存在不一致的数据而roll back,或者结束不必要的等待而commit成功。( those transactions could end up with inconsistent data were the transaction to roll back, or end up waiting unnecessarily were the transaction to commit successfully. )

越高等级的独立性意味着越少的并发性,并且越有可能成为系统性能的瓶颈,但它也减少了读取到不一致数据的机会。一个好的应用原则是在可接受的系统性能基础上选择尽可能高的独立性。以下是通常的独立性等级划分,从最低到最高如下:

ReadUncommitted:已经update但尚未commit的数据可以被其他事务读取

ReadCommitted:一个事务只有commit了的数据才可被其他事务读取

RepeatableRead: 一个事务只有commit了的数据才可被其他事务读取,重复的读取则会导致和数据尚未提交状态时一样的结果

Serializable: 这是最高等级的独立性等级,保证了一个事务对数据排他性的读写访问,它包含了ReadCommitted和RepeatableRead级别的所有限制,同时保证所有的事务必须逐个执行,以获取最大程度的数据完整性,这导致了最低的运行性能以及最少的并发性,注意,这里的Serializable和Java里面的java.io.Serializable毫无联系。



J2EE支持的事务

J2EE平台包括了规范、兼容性test suite,application开发蓝图,以及参考实现。基于相同的规范,众多的开发商提供了不同的应用服务器和实现。J2EE组件的设计是基于规范而非基于特定的产品(比如基于某个特定的应用服务器)。J2EE应用程序里的组件利用了J2EE容器和服务器所提供的基础服务,因此只需要将关注点放在处理业务逻辑上,J2EE通过部署描述符,使用声明式的属性,支持可伸缩的部署以及在目标环境中可定制的能力。J2EE的目标是保护IT投资以及降低application开发的成本,J2EE的组件可以自己开发也可从外面的代理商处获得,这样可以使得你的IT部门的成本降低并提高了可伸缩性。

J2EE所提供的基础服务中一个重要部分便是对事务的支持,在J2EE规范中,描述了Java事务的API(JTA,Java Transaction API),JTA主要包括了几个接口:javax.transaction.UserTransaction和javax.transaction.TransactionManager。UserTransaction接口提供给application组件,对application组件而言,底下的J2EE服务器和TransactionManager之间的交互是透明的。TransactionManager的实现提供给应用服务器对事务边界的控制。J2EE的application组件提供了对JTA的UserTransaction和JDBC的事务的支持。

J2EE平台支持了两种类型的事务管理范式:声明式的事务和编程式的事务(declarative transaction demarcation and programmatic transaction demarcation.

...(to be continued)
posted @ 2006-07-17 23:38 响 阅读(267) | 评论 (0)编辑 收藏