七段

无论怎样,请让我先感谢一下国家。

BlogJava 首页 新随笔 联系 聚合 管理
  35 Posts :: 2 Stories :: 7 Comments :: 0 Trackbacks

2012年3月7日 #

有25匹马,每匹马都以一个固定不变的速度奔跑,每匹马的速度都不一样,如果让你找出跑的最快的5匹马,最少需要组织多少场比赛?注:每场比赛最多只能5匹马参赛。
re:悲观10场,乐观7场。
posted @ 2012-03-07 21:17 sevenduan 阅读(824) | 评论 (1)编辑 收藏

2010年8月7日 #

Eclipse:
ctrl+o
ctrl+space
ctrl+t
ctrl+k
ctrl+f8/f9/f10

alt+shift+x , t/j
alt+shift+d , t/j

Command:
cd -
tab
ctrl+a/e
ctrl+u/k/w

Vim
shift+g
m+'mark'
`+'mark'
. (repeat)



posted @ 2010-08-07 23:14 sevenduan 阅读(240) | 评论 (0)编辑 收藏

2010年7月18日 #

Framework Supported log levels Standard appenders Popularity Cost / licence
Log4J FATAL ERROR WARN INFO DEBUG TRACE AsyncAppender, JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender, NullAppender, SMTPAppender, SocketAppender, SocketHubAppender, SyslogAppender, TelnetAppender, WriterAppender Widely used in many project and platforms Apache License, Version 2.0
Java Logging API SEVERE WARNING INFO CONFIG FINE FINER FINEST Depends on the underlying framework; Sun's default Java Virtual Machine (JVM) has the following: ConsoleHandler, FileHandler, SocketHandler, MemoryHandler Not widely used[citation needed] Comes with the JRE
Apache Commons Logging FATAL ERROR WARN INFO DEBUG TRACE Depends on the underlying framework Widely used, in conjunction with log4j Apache License, Version 2.0
SLF4J ERROR WARN INFO DEBUG TRACE Depends on the underlying framework, which is pluggable Probably small but growing MIT License
posted @ 2010-07-18 22:15 sevenduan 阅读(480) | 评论 (0)编辑 收藏

2010年4月25日 #

1 definition:

“A transaction is a complete unit of work. It may comprise many computational tasks,which may include user interface, data retrieval, and communications. A typicaltransaction modifies shared resources.”

2 transaction features:
ACID (atomicity, consistency, isolation, durability)

3 java spec
JTA, JTS
 1interface javax.transaction.TransactionManager
 2{
 3public abstract void begin();
 4public abstract void commit();
 5public abstract int getStatus();
 6public abstract Transaction getTransaction();
 7public void resume(Transaction tobj);
 8public abstract void rollback();
 9public abstract void setRollbackOnly();
10public abstract void setTransactionTimeout(intseconds);
11public abstract Transaction suspend() ;
12}

4 Common XAResource
JDBC 2.0:
A JDBC driver that supports distributed transactions implements the javax.transaction.xa.XAResource interface, the javax.sql.XAConnectioninterface, and the  javax.sql.XADataSource interface.

JMS 1.0:

a JMS provider javax.transaction.xa.XAResource interface, the implements the javax.jms.XAConnection and the javax.jms.XASession interface.

5 Common TransactionManager

5.1 EJB Transaction Options:
NotSupported
    If the method is called within a transaction, this transaction is suspended during the time of the method execution.
Required
    If the method is called within a transaction, the method is executed in the scope of this transaction; otherwise, a new transaction is started for the execution of the method and committed before the method result is sent to the caller.
RequiresNew
    The method will always be executed within the scope of a new transaction. The new transaction is started for the execution of the method, and committed before the method result is sent to the caller. If the method is called within a transaction, this transaction is suspended before the new one is started and resumed when the new transaction has completed.
Mandatory
    The method should always be called within the scope of a transaction, else the container will throw the TransactionRequired exception.
Supports
    The method is invoked within the caller transaction scope; if the caller does not have an associated transaction, the method is invoked without a transaction scope.
Never
    The client is required to call the bean without any transaction context; if it is not the case, a java.rmi.RemoteException is thrown by the container.

5.2 Spring transaction:
      Transaction isolation: The degree of isolation this transaction has from the work of other transactions. For example, can this transaction see uncommitted writes from other transactions? avaliable options:
ISOLATION_DEFAULT
ISOLATION_READ_UNCOMMITTED
ISOLATION_READ_COMMITTED
ISOLATION_REPEATABLE_READ
ISOLATION_SERIALIZABLE

      Transaction propagation: Normally all code executed within a transaction scope will run in that transaction. However, there are several options specifying behavior if a transactional method is executed when a transaction context already exists: For example, simply running in the existing transaction (the most common case); or suspending the existing transaction and creating a new transaction. Spring offers the transaction propagation options familiar from EJB CMT. avaliable options:
PROPAGATION_MANDATORY
PROPAGATION_NESTED
PROPAGATION_NEVER
PROPAGATION_NOT_SUPPORTED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRES_NEW
PROPAGATION_SUPPORTS

      Transaction timeout: How long this transaction may run before timing out (automatically being rolled back by the underlying transaction infrastructure).
      Read-only status: A read-only transaction does not modify any data. Read-only transactions can be a useful optimization in some cases (such as when using Hibernate).


6 transaction for web service
Protocol specifications:
WS-Transaction
OASIS Business Transaction Protocol (BTP)
Java API
JAXTX (JSR-156)

 

posted @ 2010-04-25 16:44 sevenduan 阅读(605) | 评论 (0)编辑 收藏

2010年4月17日 #

JavaScript里有两个容易让初学者混淆的概念:scope chain and closure。
比如说,我想创建10个函数,每个函数依次返回0-9.
 1 //wrong: all function refer to global variable i=10 
 2 var fn_list=[];
 3 for(var i=0;i<10;i++){
 4  var _tempFn =function(){
 5         return i;
 6  }
 7  fn_list.push(_tempFn);    
 8 }
 9 //right: every function refer to its closure scope variable a
10 var fn_list=[];
11 for(var i=0;i<10;i++){
12  var _tempFn =function(a){
13         return function(){
14          return a;
15         };
16  }
17  fn_list.push(_tempFn(i));    
18 }
19 

Java里也有两个让初学者容易混淆的概念:nest class and inner class。
nest class就是static inner class,
而inner class就是no-static inner class。没有为什么,sun就是这么定义的。
还是上面得例子,创建10个对象,每个对象的getValue接口依次返回0-9.
 1 public class Test {
 2     private int noStaticValue;
 3     private static int staticValue;
 4 
 5     public Test(int noSV, int sv) {
 6         this.noStaticValue = noSV;
 7         this.staticValue = sv;
 8     }
 9 
10     public Test(int noSV) {
11         this.noStaticValue = noSV;
12     }
13 
14     interface valueHolder {
15         int getValue();
16     }
17 
18     class innerClass implements valueHolder {
19         public int getValue() {
20             return noStaticValue;
21         }
22     }
23 
24     static class nestClass implements valueHolder {
25         public nestClass(int i) {
26             staticValue = i;
27         }
28 
29         public int getValue() {
30             return staticValue;
31         }
32     }
33 
34     public static void main(String[] args) {
35         Test context1 = new Test(00);
36         valueHolder[] list = new valueHolder[10];
37         for (int i = 0; i < 10; i++) {
38             list[i] = new Test.nestClass(i);
39         }
40         for (valueHolder obj : list) {
41             System.out.println(obj.getValue());// always print 9
42         }
43         for (int i = 0; i < 10; i++) {
44             list[i] = new Test(i).new innerClass();
45         }
46         for (valueHolder obj : list) {
47             System.out.println(obj.getValue());// print 0-9
48         }
49     }
50 }
可见用inner class可以模拟closure的特性,就是运行时定义class的某些状态。
inner class和nest class之间的区别就是后者是静态类。前者必须通过wrap class的实例来调用new,e.g. new Test().new innerClass。
因为nest class是静态类,所以可以添加static member 或者static method,而inner class 不行。
匿名内部类是inner class的一种特殊形式,所以也不能添加static member 或者static method。



posted @ 2010-04-17 23:07 sevenduan 阅读(2778) | 评论 (5)编辑 收藏

2010年4月14日 #

先看一段代码:
byte [] b = new byte[]{1,-1,2,-2};
        System.out.println(Arrays.toString(
new String(b).getBytes()));

输出:
[1, -17, -65, -67, 2, -17, -65, -67]
解释:
byte decode to String,String encode to byte 默认用UTF-8 charset.
decode遇到不支持的字符 输出 char � , encode � 就是 -17, -65, -67.
实现细节可见ByteToCharUTF8.java

解决办法: 使用 ISO8859_1 charset。

教训: 注意charset的范围。



posted @ 2010-04-14 23:14 sevenduan 阅读(2028) | 评论 (0)编辑 收藏

2010年4月13日 #

java bitwise operator:
~ The unary bitwise complement operator "~" inverts a bit pattern.
<<The signed left shift
>>The signed right shift
>>>the unsigned right shift

& The bitwise & operator performs a bitwise AND operation.

^ The bitwise ^ operator performs a bitwise exclusive OR operation.

| The bitwise | operator performs a bitwise inclusive OR operation.



Usage:
1,
  • ^ can swap two variables without using an intermediate, temporary variable which is useful if you are short on available RAM or want that sliver of extra speed.

    Usually, when not using ^, you will do:

    temp = a;

    a = b;

    b = temp;

    Using ^, no "temp" is needed:

    a ^= b;

    b ^= a;

    a ^= b;

    This will swap "a" and "b" integers. Both must be integers.

2,
an example of using an integer to maintain state flags (common usage):
// These are my masks

private static final int MASK_DID_HOMEWORK  = 0x0001;

private static final int MASK_ATE_DINNER    = 0x0002;

private static final int MASK_SLEPT_WELL    = 0x0004;



// This is my current state

private int m_nCurState;

To set my state, I use the bitwise OR operator:

// Set state for'ate dinner' and 'slept well' to 'on'

m_nCurState
= m_nCurState | (MASK_ATE_DINNER | MASK_SLEPT_WELL);

Notice how I 'or' my current state in with the states that I want to turn 'on'. Who knows what my current state is and I don't want to blow it away.

To unset my state, I use the bitwise AND operator with the complement operator:

// Turn off the 'ate dinner' flag

m_nCurState
= (m_nCurState & ~MASK_ATE_DINNER);

To check my current state, I use the AND operator:

// Check if I did my homework

if (0 != (m_nCurState & MASK_DID_HOMEWORK)) {

   
// yep

} else {

   
// nope...

}

Why do I think this is interesting? Say I'm designing an interface that sets my state. I could write a method that accepts three booleans:

void setState( boolean bDidHomework, boolean bAteDinner, boolean bSleptWell);

Or, I could use a single number to represent all three states and pass a single value:

void setState( int nStateBits);

If you choose the second pattern you'll be very happy when decide to add another state - you won't have to break existing impls of your interface.


posted @ 2010-04-13 14:39 sevenduan 阅读(406) | 评论 (0)编辑 收藏

2010年4月5日 #

什么才是激励你工作的动力?
这个问题问自己的往往很少。经常思考这些问题的往往是管理者。当管理者想进行一些管理工作的时候,管理本身就成了最大的问题。所以,一个项目的成败往往在很大程度上取决于受到管理层的负面影响的大小。
大多数人和大多数管理者有着共同的认识,钱才是激励我们工作的动力。理由很充分,盈利是企业的唯一目标,说起来就好像人活着就是为了吃饭一样。所以,各种绩效考核与薪酬挂钩了,各种项目奖、季度奖、年终奖诞生了。为了响应上级政策,我们开始加班加点了,开始赶进度赶业绩了。试想,在这种驱动下,如果手头的事情没有和奖金挂钩,你还有动力继续么?
结果唯一导向对于重复性的体力劳动来说是有效的。但是对于脑力劳动者来说,自发、专精和目标性才是内在的能够持续激励我们工作的动力所在。
自发:
对于脑力劳动,命令式的分配任务很难奏效,很显然,你不能强迫别人的想法。就像强迫学生学习一样。只有自发的工作,才能培养兴趣激发工作热情。正如敏捷实践里强调任务应该由个人自发选择领取一样。
专精:
在自发的前提下,无论我们做什么事情,我们总是希望自己可以做得更好。这就是专精。对专精的向往表现为对工作的痴迷。要想更为专精,必须不断的练习实践和分析思考。
目标性:
自发和专精也许能在中短期维持动力,但是长期下去,当我们不知道自己为什么工作的时候,动力也会衰竭。目标性要求目标不能太低而没有成就感,或者太高而很难达到。最好的目标是比你现在的成就更高,且需要一定的努力才能达到。
Drive: The Surprising Truth About What Motivates Us


posted @ 2010-04-05 23:43 sevenduan 阅读(488) | 评论 (0)编辑 收藏

2010年4月1日 #

简便个人项目管理:
1,一周一个iteration,一个iteration共计30个man hours:
只要可以小迭代总是更好。
story point优势在于消除个体差异,个人项目man hour更准确直接。
2, 每周日做iteration plan,计划下周任务,用gmail tasks来做story wall。
3,每周五晚做retrospective,总结一周工作,直接用gmail来保存回顾总结。

posted @ 2010-04-01 00:54 sevenduan 阅读(237) | 评论 (0)编辑 收藏

2010年3月28日 #

Ref: http://community.jboss.org/wiki/JBossCacheOfficialDocumentation

Cache的目的是为了以空间换时间,一次计算结果为多次重用。
空间可以是实时内存空间、持久化的硬盘空间。时间可以是运算时间、连接时间、传输时间等。

Cache可以分为LocalCache和DistributedCache。
最简单的LocalCache可以通过维护一个ConcurrentHashMap实现。
缺点是:
1,内存有限,容易out of memory (定期清除?持久化?)
2, 需要对全map做concurrency维护,粗粒度的锁定争用会影响性能(树结构维护?)

在一个专业的企业级应用中,cache除了高性能和线程安全的要求,还要支持事务、高可用性、持久化、容错、集群同步等。
JBossCache是一个典型的企业级cache实现,他采用树结构且支持集群和事务特性。
虽然JBossCache这把牛刀也可以在standalone的JS2E应用中用来杀鸡,但我们应该更关心用他在集群环境中怎么杀牛。
JBossCache分为非集群模式(Local)和集群模式。
集群模式根据实现策略又分为replication和invalidation。
1 replication:通过拷贝改变的cache对象来保证与集群中其他cache同步。replication又可细分为同步replication和异步repliation两种,异步replication较快,put以后马上返回,但是replication出错了,事务还是算完成了不回回滚。同步replication要花时间等待其他的cache完成replication的通知才能结束。

2 invalidation: 如果cache状态改变,仅仅是给其他cache发个通知,收到通知的cache把脏数据清除掉。invalidation也可分为同步和异步两种,区别是发送通知的广播方式一个是同步一个是异步。

在jboss cluster中,我们最好通过MBean来部署jboss cache。这样又几个好处:
1,JBoss NS支持Cluster
我们就可以通过JBoss NamingService来访问cache。如果在local NS中查不到cache,jbss NS还会去查cluster中其他的cache。
2,利用MBean的特性
通过CacheMBean, 我们可以方便的管理Cache Service,实时的启动、停止或者改变一些配置,还可以监控到一些cache统计数据。
3,利用microcontainer的特性
我们可以通过配置XML文件来完成cache相关的所有对象声明。简而言之,就是利用java reflection和AOP的技术就不用写声明cache的代码了。


<?xml version="1.0" encoding="UTF-8"?>



<deployment xmlns="urn:jboss:bean-deployer:2.0">



   
<!-- First we create a Configuration object for the cache -->

   
<bean name="ExampleCacheConfig"

         class
="org.jboss.cache.config.Configuration">

      

       build up the Configuration

      

   
</bean>

   

   
<!-- Factory to build the Cache. -->

   
<bean name="DefaultCacheFactory" class="org.jboss.cache.DefaultCacheFactory">      

      
<constructor factoryClass="org.jboss.cache.DefaultCacheFactory"

                   factoryMethod
="getInstance" />

   
</bean>

   

   
<!-- The cache itself -->

   
<bean name="ExampleCache" class="org.jboss.cache.CacheImpl">

      

      
<constructor factoryMethod="createnewInstance">

          
<factory bean="DefaultCacheFactory"/>

          
<parameter><inject bean="ExampleCacheConfig"/></parameter>

          
<parameter>false</parameter>

      
</constructor>

          

   
</bean>

   

   
<!-- JMX Management -->

   
<bean name="ExampleCacheJmxWrapper" class="org.jboss.cache.jmx.CacheJmxWrapper">

      

      
<annotation>@org.jboss.aop.microcontainer.aspects.jmx.JMX(name="jboss.cache:service=ExampleTreeCache", 

                         exposedInterface=org.jboss.cache.jmx.CacheJmxWrapperMBean.class, 

                         registerDirectly=true)
</annotation>

      

      
<constructor>

          
<parameter><inject bean="ExampleCache"/></parameter>

      
</constructor>

          

   
</bean>



</deployment> 

后记:
1,jboss cache的naga版中,采用Multi-versioned concurrency control来实现并发。下次再从中总结一下多线程的学习。
2,jboss cache通过结合visitor pattern和command pattern,把对cache node的操作与访问从中隔离出来,不用改变或者扩展node对象就可以添加新的node行为。也就是开闭原则。下次再从中总结一下几种设计模式的经典应用。



posted @ 2010-03-28 23:10 sevenduan 阅读(2492) | 评论 (0)编辑 收藏

仅列出标题  下一页