走自己的路
路漫漫其修远兮,吾将上下而求索
BlogJava
::
首页
::
新随笔
::
联系
::
聚合
::
管理
::
50 随笔 :: 4 文章 :: 118 评论 :: 0 Trackbacks
<
2009年4月
>
日
一
二
三
四
五
六
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
7
8
9
留言簿
(7)
给我留言
查看公开留言
查看私人留言
随笔分类
(81)
Design and Analysis Pattern (5)
(rss)
J2SE and JVM (11)
(rss)
Java Persistence, Transaction and ORM (7)
(rss)
JDBC JNDI JMS RMI EJB and Other J2EE Tech (12)
(rss)
JSF Facelets and Ajax
(rss)
JSP, Servlet and JSTL (2)
(rss)
Life(8)
(rss)
Lucene(1)
(rss)
Oracle (6)
(rss)
Other Java and J2EE frameworks (16)
(rss)
Ruby On Rails
(rss)
RUP Scrum and XP(2)
(rss)
Security
(rss)
SOA, SCA, JBI, BPEL and OSGI (2)
(rss)
Spring(2)
(rss)
Test(4)
(rss)
UML, OOAD
(rss)
XML(3)
(rss)
随笔档案
(54)
2011年4月 (1)
2011年2月 (2)
2010年12月 (1)
2010年11月 (2)
2010年8月 (1)
2010年5月 (1)
2010年3月 (1)
2010年2月 (1)
2010年1月 (3)
2009年12月 (1)
2009年11月 (1)
2009年9月 (3)
2009年8月 (2)
2009年7月 (2)
2009年5月 (4)
2009年4月 (3)
2009年3月 (1)
2009年2月 (1)
2009年1月 (1)
2008年12月 (4)
2008年11月 (1)
2008年10月 (2)
2008年9月 (1)
2008年8月 (3)
2008年7月 (4)
2008年6月 (3)
2008年5月 (2)
2008年3月 (2)
文章分类
Agile: RUP Scrum and XP
(rss)
Design and Analysis Pattern
(rss)
J2SE and JVM
(rss)
Java Persistence, Transaction and ORM
(rss)
JDBC JNDI JMS RMI EJB and Other J2EE Tech
(rss)
JSF Facelets and Ajax
(rss)
JSP, Servlet and JSTL
(rss)
Oracle
(rss)
Other Java and J2EE frameworks
(rss)
Ruby On Rails
(rss)
Security
(rss)
SOA and OSGI
(rss)
Spring
(rss)
Test
(rss)
XML
(rss)
搜索
最新评论
1. re: 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣[未登录]
除了公式外,还和max user processes 限定有关吧?
--呵呵
2. re: 在Spring基础上实现自己的异常处理框架
评论内容较长,点击标题查看
--最代码
3. re: 蛋疼的PooledConnectionFactory(activemq-pool)
我还以为就我一个人觉得这个玩意写的操蛋!遇到知音了!
--吕春龙
4. re: 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣[未登录]
很好,呵呵有价值的文章。
--Ryan
5. re: 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣
good job
--wxylion1
阅读排行榜
1. 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣(34909)
2. 蛋疼的PooledConnectionFactory(activemq-pool)(11565)
3. Unit test学习总结(11506)
4. JAXB vs XStream(10956)
5. 在Spring基础上实现自己的异常处理框架(7734)
评论排行榜
1. 《Head First Design Pattern 单例模式》中double check有问题吗?(22)
2. oc4j+toplink+spring使用jta外部事务的一点心得(11)
3. 剥下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣(10)
4. JAXB vs XStream(9)
5. 在Spring基础上实现自己的异常处理框架(8)
java实现的同步循环链表
public
class
CircularLinkedList
<
E
>
{
private
Entry
<
E
>
head;
//
Last element of the list. tail.next = head
private
Entry
<
E
>
tail;
private
Entry
<
E
>
next;
private
Entry
<
E
>
lastReturned;
private
int
size
=
0
;
/** */
/**
* Class to hold the individual elements.
*
*
@param
<E>
*/
private
static
class
Entry
<
E
>
{
E element;
Entry
<
E
>
next;
Entry(E element, Entry
<
E
>
next)
{
this
.element
=
element;
this
.next
=
next;
}
Entry(E element)
{
this
.element
=
element;
}
}
public
CircularLinkedList()
{
head
=
tail
=
null
;
}
@SuppressWarnings(
"
unchecked
"
)
public
CircularLinkedList(Collection
<?
extends
E
>
c)
{
Object[] eles
=
c.toArray();
int
nums
=
eles.length;
for
(
int
i
=
0
; i
<
nums; i
++
)
{
this
.add((E) eles[i]);
}
}
/** */
/**
* Circular iterator
*
*
@return
*/
public
synchronized
E next()
{
if
(head
==
null
)
{
throw
new
NoSuchElementException();
}
else
{
//
Entry<E> rtned = next;
lastReturned
=
next;
next
=
next.next;
return
lastReturned.element;
}
}
public
synchronized
boolean
hasNext()
{
return
size
>
0
;
}
public
synchronized
boolean
isEmpty()
{
return
size
<=
0
;
}
/** */
/**
* Remove obj from the circular linked list and return the removed object
*
*
@param
obj
*
@return
*/
public
synchronized
E remove(E obj)
{
if
(head
==
null
||
tail
==
null
)
throw
new
NoSuchElementException();
Entry
<
E
>
p
=
head, temp
=
head, found
=
null
;
if
(head.element.equals(obj))
{
if
(head.next
==
head)
{
found
=
head;
head
=
null
;
tail
=
null
;
size
--
;
return
found.element;
}
else
{
found
=
head;
head
=
found.next;
temp
=
tail;
}
}
else
{
p
=
head.next;
while
(p
!=
head)
{
if
(p.element.equals(obj))
{
found
=
p;
break
;
}
temp
=
p;
p
=
p.next;
}
if
(found
==
tail)
{
tail
=
temp;
}
}
if
(found
==
null
)
{
throw
new
NoSuchElementException();
}
E result
=
found.element;
temp.next
=
found.next;
if
(found
==
next)
{
next
=
found.next;
}
found.next
=
null
;
found.element
=
null
;
size
--
;
return
result;
}
/** */
/**
* Contains the specified object or not
*
@param
obj
*
@return
*/
public
synchronized
boolean
contains(E obj)
{
if
(head
==
null
||
tail
==
null
)
return
false
;
Entry
<
E
>
found
=
null
;
if
(head.element.equals(obj))
{
found
=
head;
}
else
{
Entry
<
E
>
p
=
head.next;
while
(p
!=
head)
{
if
(p.element.equals(obj))
{
found
=
p;
break
;
}
p
=
p.next;
}
}
if
(found
==
null
)
{
return
false
;
}
return
true
;
}
/** */
/**
* Add obj to the circular linked list.
*
*
@param
obj
*/
public
synchronized
void
add(E obj)
{
Entry
<
E
>
e
=
new
Entry
<
E
>
(obj);
if
(head
==
null
)
{
size
++
;
head
=
e;
head.next
=
head;
tail
=
head;
next
=
head;
return
;
}
if
(lastReturned
==
tail)
{
next
=
e;
}
size
++
;
tail.next
=
e;
tail
=
e;
tail.next
=
head;
}
/** */
/**
* Size of the list.
*
*
@return
*/
public
synchronized
int
size()
{
return
size;
}
/** */
/**
*
@return
the head
*/
public
synchronized
E getHead()
{
if
(
null
==
head)
{
throw
new
NoSuchElementException();
}
return
head.element;
}
/** */
/**
*
@return
the tail
*/
public
synchronized
E getTail()
{
if
(
null
==
tail)
{
throw
new
NoSuchElementException();
}
return
tail.element;
}
}
Test:
@RunWith(JDaveRunner.
class
)
public
class
CircularLinkedListSpec
extends
Specification
<
CircularLinkedList
<
String
>>
{
public
class
EmptyCLL
{
private
CircularLinkedList
<
String
>
cll;
public
CircularLinkedList
<
String
>
create()
{
return
this
.cll
=
new
CircularLinkedList
<
String
>
();
}
public
void
getHead()
{
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.getHead();
}
}
, must.raiseExactly(NoSuchElementException.
class
));
}
public
void
getTail()
{
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.getTail();
}
}
, must.raiseExactly(NoSuchElementException.
class
));
}
public
void
size()
{
specify(cll.size(), must.equal(
0
));
}
public
void
next()
{
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.next();
}
}
, must.raiseExactly(NoSuchElementException.
class
));
}
public
void
remove()
{
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.remove(
"
gavin
"
);
}
}
, must.raiseExactly(NoSuchElementException.
class
));
}
public
void
add()
{
this
.cll.add(
"
gavin
"
);
specify(cll.size(),
1
);
specify(cll.getHead(),
"
gavin
"
);
specify(cll.getTail(),
"
gavin
"
);
}
}
public
class
OnlyHeadCLL
{
private
CircularLinkedList
<
String
>
cll;
public
CircularLinkedList
<
String
>
create()
{
List
<
String
>
list
=
new
ArrayList
<
String
>
();
list.add(
"
gavin
"
);
return
this
.cll
=
new
CircularLinkedList
<
String
>
(list);
}
public
void
getHead()
{
specify(cll.getHead(), must.equal(
"
gavin
"
));
}
public
void
getTail()
{
specify(cll.getHead(), must.equal(
"
gavin
"
));
}
public
void
size()
{
specify(cll.size(), must.equal(
1
));
}
public
void
next()
{
String current
=
cll.next();
specify(current, must.equal(
"
gavin
"
));
}
public
void
remove()
{
String removed
=
cll.remove(
"
gavin
"
);
specify(removed,
"
gavin
"
);
specify(cll.size(), must.equal(
0
));
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.getHead();
}
}
, must.raiseExactly(NoSuchElementException.
class
));
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.getTail();
}
}
, must.raiseExactly(NoSuchElementException.
class
));
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.next();
}
}
, must.raiseExactly(NoSuchElementException.
class
));
}
public
void
removeTwice()
{
String removed
=
cll.remove(
"
gavin
"
);
specify(removed,
"
gavin
"
);
specify(cll.size(), must.equal(
0
));
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.remove(
"
gavin
"
);
}
}
, must.raiseExactly(NoSuchElementException.
class
));
}
public
void
add()
{
this
.cll.add(
"
afka
"
);
specify(cll.size(), must.equal(
2
));
specify(cll.getHead(),
"
gavin
"
);
specify(cll.getTail(),
"
afka
"
);
}
}
public
class
NormalCLL
{
private
CircularLinkedList
<
String
>
cll;
public
CircularLinkedList
<
String
>
create()
{
List
<
String
>
list
=
new
ArrayList
<
String
>
();
list.add(
"
gavin
"
);
list.add(
"
afka
"
);
list.add(
"
eddie
"
);
return
this
.cll
=
new
CircularLinkedList
<
String
>
(list);
}
public
void
add()
{
specify(cll.size(),
3
);
specify(
this
.cll.getHead(),
"
gavin
"
);
specify(
this
.cll.getTail(),
"
eddie
"
);
this
.cll.add(
"
rex
"
);
specify(cll.size(),
4
);
specify(
this
.cll.getHead(),
"
gavin
"
);
specify(
this
.cll.getTail(),
"
rex
"
);
}
public
void
remove()
{
specify(cll.size(),
3
);
this
.cll.remove(
"
afka
"
);
specify(cll.size(),
2
);
specify(
this
.cll.getHead(),
"
gavin
"
);
specify(
this
.cll.getTail(),
"
eddie
"
);
}
public
void
removeHead()
{
specify(cll.size(),
3
);
this
.cll.remove(
"
gavin
"
);
specify(cll.size(),
2
);
specify(
this
.cll.getHead(),
"
afka
"
);
}
public
void
removeTail()
{
specify(cll.size(),
3
);
this
.cll.remove(
"
eddie
"
);
specify(cll.size(),
2
);
specify(
this
.cll.getTail(),
"
afka
"
);
}
public
void
removeNotExist()
{
specify(
new
Block()
{
public
void
run()
throws
Throwable
{
cll.remove(
"
rex
"
);
}
}
, must.raiseExactly(NoSuchElementException.
class
));
}
}
public
class
Iteration
{
private
CircularLinkedList
<
String
>
cll;
public
CircularLinkedList
<
String
>
create()
{
List
<
String
>
list
=
new
ArrayList
<
String
>
();
list.add(
"
gavin
"
);
list.add(
"
afka
"
);
list.add(
"
eddie
"
);
return
this
.cll
=
new
CircularLinkedList
<
String
>
(list);
}
public
void
next()
{
String step1
=
this
.cll.next();
specify(
3
, cll.size());
specify(step1,
"
gavin
"
);
specify(cll.getHead(),
"
gavin
"
);
specify(cll.getTail(),
"
eddie
"
);
}
public
void
addWhenNext()
{
String step1
=
this
.cll.next();
specify(step1,
"
gavin
"
);
String step2
=
this
.cll.next();
specify(step2,
"
afka
"
);
this
.cll.add(
"
rex
"
);
String step3
=
this
.cll.next();
specify(step3,
"
eddie
"
);
specify(cll.getTail(),
"
rex
"
);
String step4
=
this
.cll.next();
specify(step4,
"
rex
"
);
specify(cll.getTail(),
"
rex
"
);
specify(cll.getHead(),
"
gavin
"
);
}
public
void
removeSubWhenNext()
{
String step1
=
this
.cll.next();
specify(step1,
"
gavin
"
);
this
.cll.remove(
"
eddie
"
);
specify(
this
.cll.next(),
"
afka
"
);
specify(
this
.cll.next(),
"
gavin
"
);
}
public
void
removePreWhenNext()
{
specify(
this
.cll.next(),
"
gavin
"
);
specify(
this
.cll.next(),
"
afka
"
);
this
.cll.remove(
"
gavin
"
);
specify(
this
.cll.next(),
"
eddie
"
);
specify(
this
.cll.next(),
"
afka
"
);
}
public
void
nextIsJustRemoved()
{
String step1
=
this
.cll.next();
specify(step1,
"
gavin
"
);
this
.cll.remove(
"
afka
"
);
String step2
=
this
.cll.next();
specify(step2,
"
eddie
"
);
}
public
void
nextIsJustAdded()
{
String step1
=
this
.cll.next();
specify(step1,
"
gavin
"
);
String step2
=
this
.cll.next();
specify(step2,
"
afka
"
);
String step3
=
this
.cll.next();
specify(step3,
"
eddie
"
);
specify(cll.getTail(),
"
eddie
"
);
this
.cll.add(
"
rex
"
);
String step4
=
this
.cll.next();
specify(step4,
"
rex
"
);
specify(cll.getTail(),
"
rex
"
);
specify(cll.getHead(),
"
gavin
"
);
}
public
void
iterationCycle()
{
specify(
3
, cll.size());
specify(
this
.cll.next(),
"
gavin
"
);
specify(
this
.cll.next(),
"
afka
"
);
specify(
this
.cll.next(),
"
eddie
"
);
specify(
this
.cll.next(),
"
gavin
"
);
specify(cll.getHead(),
"
gavin
"
);
specify(cll.getTail(),
"
eddie
"
);
}
}
}
posted on 2009-04-01 12:42
叱咤红人
阅读(527)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
Powered by:
BlogJava
Copyright © 叱咤红人