ZT 从雅虎看互联网的世纪轮回

中国IT界资深媒体人 冀勇庆 为英国《金融时报》中文网撰稿 2011-09-08 (www.ftchinese.com)

发表评论

Filed under Uncategorized

try – catch – finally

    private void test(){
        try{
            TestException te = this.new TestException();
            te.test();
            System.out.println("trytrytry");
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("catchcatchcatch");
        } finally {
            System.out.println("finallyfinallyfinally");
        }
        System.out.println("endendend");
    }

    private class TestException {
        void test() throws Exception{
            throw new Exception("illegal invoked");
        }
    }

这个程序返回的结果为:

繼續閱讀

发表评论

Filed under Uncategorized

the order of execution for the multi-condition – if – Java

如果if有多个条件,且为“与”的关系如,

if(a!=null&&a==b)

如果第一个条件为false,那么第二个条件不会被检测。直接返回false

|| 同理

 

应用在需要首先检测instance是否为空的情况。不会因为条件2的操作而抛出null exception

发表评论

Filed under Uncategorized

10 Points about Java Heap Space

1. Java Heap Memory is part of Memory allocated to JVM by Operating System.
2. Whenever we create objects they are created inside Heap in Java.
3. Java Heap space is divided into three regions or generation for sake of garbage collection called New Generation, Old or tenured Generation or Perm Space.
4. You can increase or change size of Java Heap space by using JVM command line option -Xms, -Xmx and -Xmn. don’t forget to add word “M” or “G” after specifying size to indicate Mega or Giga. for example you can set java heap size to 258MB by executing following command java -Xmx256m HelloWord.
5. You can use either JConsole or Runtime.maxMemory(), Runtime.totalMemory(), Runtime.freeMemory() to query about Heap size programmatic in Java.
6. You can use command “jmap” to take Heap dump in Java and “jhat” to analyze that heap dump.
7. Java Heap space is different than Stack which is used to store call hierarchy and local variables.
8. Java Garbage collector is responsible for reclaiming memory from dead object and returning to Java Heap space.
9. Don’t panic when you get java.lang.outofmemoryerror, sometimes its just matter of increasing heap size but if it’s recurrent then look for memory leak in Java.

10. Use Profiler and Heap dump Analyzer tool to understand Java Heap space and how much memory is allocated to each object.

This article is in continuation of my previous articles How Classpath works in Java , How to write Equals method in java , How HashMap works in Java  and difference between HashMap and Hashtable in Java  and How Synchronization works in Java if you haven’t read already you may find some useful information based on my experience in Java .

发表评论

Filed under Uncategorized

ZT: Determining if any active HTTP Sessions exist for deployed applications in OAS 10.1.3.x

MONDAY, 11 AUGUST 2008

Recently I was asked how to determine if any current HTTP sessions exist for an application. The reason for needing this information was so that the application could be redeployed only when no HTTP sessions existed and the application was currently not being used. It turns out there is an MBean which can give you that information.

The Mbean is as follows:

oc4j:j2eeType=ClassLoading,name=singleton,J2EEServer=standalone

His how to access it from the MBean browser in ASC and get the information on HTTP sessions for your deployed applications.

1. Log into ASC
2. Click on the container you wish to use.
3. Click on the link “Administration“.
4. Click on the “Go To Task” icon as follows.

JMX -> System MBean Browser

5. Expand the ClassLoading node in the navigation pane, then select the singleton MBean instance.
6. Click the Operations tab in the right-hand pane, then click the executeQuery operation.

Three versions of the executeQuery operation are exposed. Click the version that takes one parameter.

7. For the value parameter enter in the following and press the “Invoke Operation” button

HttpSessions(details)

You will end up with output as follows which will give you the amount of HTTP sessions which currently exist for each deployed application.

** Summary at Aug 11, 2008 9:57:43 AM **

Total Sessions: 0
Total Attributes: 0
Total Session Size: 0 bytes

Largest Session: N/A
Youngest Session: N/A
Idlest Session: N/A
Oldest Session: N/A

** Details **

[ADFBC-HA:adfhaprofile]

No active sessions

[SimpleWS:WebServices]

No active sessions

[WebServiceDemo-SimpleWS-WS:WebServices]

No active sessions

发表评论

Filed under Uncategorized

堆和栈的区别

堆和栈是两个不同概念

堆和栈的区别 

一、预备知识—程序的内存分配
一个由c/C++编译的程序占用的内存分为以下几个部分
1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。
2、堆区(heap) — 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。
3、全局区(静态区)(static)—,全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 - 程序结束后有系统释放
4、文字常量区 —常量字符串就是放在这里的。 程序结束后由系统释放
5、程序代码区—存放函数体的二进制代码。 

二、例子程序
这是一个前辈写的,非常详细
//main.cpp
int a = 0; 全局初始化区
char *p1; 全局未初始化区
main()
{
int b; 栈
char s[] = "abc"; 栈
char *p2; 栈
char *p3 = "123456"; 123456在常量区,p3在栈上。
static int c =0; 全局(静态)初始化区
p1 = (char *)malloc(10);
p2 = (char *)malloc(20);
分配得来得10和20字节的区域就在堆区。
strcpy(p1, "123456"); 123456放在常量区,编译器可能会将它与p3所指向的"123456"优化成一个地方。
}
二、堆和栈的理论知识
2.1申请方式
stack:
由系统自动分配。 例如,声明在函数中一个局部变量 int b; 系统自动在栈中为b开辟空间
heap:
需要程序员自己申请,并指明大小,在c中malloc函数
如p1 = (char *)malloc(10);
在C++中用new运算符
如p2 = (char *)malloc(10);
但是注意p1、p2本身是在栈中的。
2.2
申请后系统的响应
栈:只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出。
堆:首先应该知道操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时,
会遍历该链表,寻找第一个空间大于所申请空间的堆结点,然后将该结点从空闲结点链表中删除,并将该结点的空间分配给程序,另外,对于大多数系统,会在这块内存空间中的首地址处记录本次分配的大小,这样,代码中的delete语句才能正确的释放本内存空间。另外,由于找到的堆结点的大小不一定正好等于申请的大小,系统会自动的将多余的那部分重新放入空闲链表中。
2.3申请大小的限制
栈:在Windows下,栈是向低地址扩展的数据结构,是一块连续的内存的区域。这句话的意思是栈顶的地址和栈的最大容量是系统预先规定好的,在 WINDOWS下,栈的大小是2M(也有的说是1M,总之是一个编译时就确定的常数),如果申请的空间超过栈的剩余空间时,将提示overflow。因此,能从栈获得的空间较小。
堆:堆是向高地址扩展的数据结构,是不连续的内存区域。这是由于系统是用链表来存储的空闲内存地址的,自然是不连续的,而链表的遍历方向是由低地址向高地址。堆的大小受限于计算机系统中有效的虚拟内存。由此可见,堆获得的空间比较灵活,也比较大。
2.4申请效率的比较:
栈由系统自动分配,速度较快。但程序员是无法控制的。
堆是由new分配的内存,一般速度比较慢,而且容易产生内存碎片,不过用起来最方便.
另外,在WINDOWS下,最好的方式是用VirtualAlloc分配内存,他不是在堆,也不是在栈是直接在进程的地址空间中保留一快内存,虽然用起来最不方便。但是速度快,也最灵活
2.5堆和栈中的存储内容
栈: 在函数调用时,第一个进栈的是主函数中后的下一条指令(函数调用语句的下一条可执行语句)的地址,然后是函数的各个参数,在大多数的C编译器中,参数是由右往左入栈的,然后是函数中的局部变量。注意静态变量是不入栈的。
当本次函数调用结束后,局部变量先出栈,然后是参数,最后栈顶指针指向最开始存的地址,也就是主函数中的下一条指令,程序由该点继续运行。
堆:一般是在堆的头部用一个字节存放堆的大小。堆中的具体内容有程序员安排。
2.6存取效率的比较 

char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
aaaaaaaaaaa是在运行时刻赋值的;
而bbbbbbbbbbb是在编译时就确定的;
但是,在以后的存取中,在栈上的数组比指针所指向的字符串(例如堆)快。
比如:
#include
void main()
{
char a = 1;
char c[] = "1234567890";
char *p ="1234567890";
a = c[1];
a = p[1];
return;
}
对应的汇编代码
10: a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4],cl
11: a = p[1];
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]
00401070 8A 42 01 mov al,byte ptr [edx+1]
00401073 88 45 FC mov byte ptr [ebp-4],al
第一种在读取时直接就把字符串中的元素读到寄存器cl中,而第二种则要先把指针值读到edx中,在根据edx读取字符,显然慢了。
? 

2.7小结:
堆和栈的区别可以用如下的比喻来看出:
使用栈就象我们去饭馆里吃饭,只管点菜(发出申请)、付钱、和吃(使用),吃饱了就走,不必理会切菜、洗菜等准备工作和洗碗、刷锅等扫尾工作,他的好处是快捷,但是自由度小。
使用堆就象是自己动手做喜欢吃的菜肴,比较麻烦,但是比较符合自己的口味,而且自由度大。 

堆和栈的区别主要分:
操作系统方面的堆和栈,如上面说的那些,不多说了。
还有就是数据结构方面的堆和栈,这些都是不同的概念。这里的堆实际上指的就是(满足堆性质的)优先队列的一种数据结构,第1个元素有最高的优先权;栈实际上就是满足先进后出的性质的数学或数据结构。
虽然堆栈,堆栈的说法是连起来叫,但是他们还是有很大区别的,连着叫只是由于历史的原因。

发表评论

Filed under Uncategorized

JSF selectItems Tag (ZT)

This tag is used to add a set of items to the nearest enclosing parent (select one or select many) component.

JSF selectItems Tag

This tag is used to add a set of items to the nearest enclosing parent (select one or select many) component. This tag can be used to get the list of choices from the list of objects from backing bean. So instead of writing  many selectItem tag for choices, you can use selectItems tag to get the options list in the form of list of objects from backing bean.

Code Description :

<%@ taglib uri=”http://java.sun.com/jsf/html” prefix=”h”%>
<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f”%>

<f:view>
<html>
<body>
<h:form>
<h:outputText value=”Select choices given below :”/><br><br>
<h:selectManyListbox id=”subscriptions” value=”#{SItemsBean.options}” size=”3″>
<f:selectItems value=”#{SItemsBean.options}” />
</h:selectManyListbox>
</h:form>
</body>
</html>
</f:view>

Backing Bean (SItemsBean.java) : In the code below, we have taken array list of SelectItem objects. It has 5 constructors. One of its constructor accepts value as Object and label as String parameter while the another one  accepts value as Object, label as String, description as String  disabled as boolean parameters. description  parameter is used for own purpose to describe something and disabled is used to make the option enabled or disabled by setting it “true” and “false” respectively. You can use them according to the need. You can understand it better by the code given below :

import javax.faces.model.SelectItem;
import java.util.*;

public class SItemsBean
{
private List options;
public SItemsBean()
{
options = new ArrayList();
SelectItem option = new SelectItem(“ch1″, “choice1″, “This bean is for selectItems tag”, true);
options.add(option);
option = new SelectItem(“ch2″, “choice2″);
options.add(option);
option = new SelectItem(“ch3″, “choice3″);
options.add(option);
option = new SelectItem(“ch4″, “choice4″);
options.add(option);
option = new SelectItem(“ch5″, “choice5);
options.add(option);
}

public void setOptions(List opt)
{
options = opt;
}

public List getOptions()
{
return options;
}
}

Rendered Output :

Html Source Code :

<html>
<body>
<form id=”_id0″ method=”post” action=”/coretag/pages/selectItems.jsf” enctype=”application/x-www-form-urlencoded”>
Select choices given below :<br><br>
<select id=”_id0:subscriptions” name=”_id0:subscriptions” multiple size=”3″> <option value=”ch1″ disabled=”disabled”>choice1</option>
<option value=”ch2″>choice2</option>
<option value=”ch3″>choice3</option>
<option value=”ch4″>choice4</option>
<option value=”ch5″>choice5</option>

</select>
<input type=”hidden” name=”_id0″ value=”_id0″ /></form>
</body>
</html>

This tag contains some attributes :

id : This is used to uniquely identify the table component. This must be unique within the closest parent component.
binding : It is a value binding expression that is used to link component to a property in a backing bean.
value : This is the value binding expression that indicates to the list or array of  selectItem instances which contains the information about the option.

发表评论

Filed under Uncategorized

Multiple mapping table – SQL

The problem is that now there are two ways of updating the foreign key columns in the Customer entity, because the foreign key columns are present in the referenced District entity, but also present as fields in the entity in order to satisfy the requirements for a composite primary key. If you try to execute this code, you will encounter an Exception such as this (in Glassfish):

Multiple writable mappings exist for the field [TPCC.CUSTOMER.C_W_ID]. Only one may be defined as writable, all others must be specified read-only.

Multiple writable mappings exist for the field [TPCC.CUSTOMER.C_D_ID]. Only one may be defined as writable, all others must be specified read-only.

To resolve this problem, you need to modify the Customer Entity definition and ensure that the columns TPCC.CUSTOMER.C_W_ID and TPCC.CUSTOMER.C_D_ID are marked as readonly, by setting insertable=false and updatable=false:

EX. – OneToOne

tabA:
    @OneToOne
    @JoinColumn(name = "COMPANY_ID", referencedColumnName = "COMPANY_ID", unique=true, insertable=false, updatable=false)
    private PlLogofile logofile;
tabB:
    @Column(name="COMPANY_ID", unique=true)
    private Long companyId;

EX: oneToMany-manyToOne
tabA:
    @OneToMany(mappedBy = "partnerlocatorCompany")
    private List<PlCompanyCrdChild> crdChilds;
tabB:
    @ManyToOne
    @JoinColumn(name = "COMPANY_ID", referencedColumnName = "COMPANY_ID", nullable=false,
          insertable=false, updatable=false)
    private PartnerlocatorCompany partnerlocatorCompany;

发表评论

Filed under Uncategorized

charactor speciaux in SQL

update Partnerlocator_company set fax =     chr(43)||’33′||chr(45)||’467300513′     where company_id =    8    ;

+ ->    chr(43)

- ->  chr(45)

 

发表评论

Filed under Uncategorized

游街女和烈士刘胡兰的不等式

英国《金融时报》中文网专栏作家 老愚 2011-01-13 (www.ftchinese.com)

 

发表评论

Filed under Uncategorized