走自己的路
路漫漫其修遠(yuǎn)兮,吾將上下而求索
BlogJava
::
首頁
::
新隨筆
::
聯(lián)系
::
聚合
::
管理
::
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 限定有關(guān)吧?
--呵呵
2.?re: 在Spring基礎(chǔ)上實現(xiàn)自己的異常處理框架
評論內(nèi)容較長,點擊標(biāo)題查看
--最代碼
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”的外衣(34955)
2.?蛋疼的PooledConnectionFactory(activemq-pool)(11604)
3.?Unit test學(xué)習(xí)總結(jié)(11537)
4.?JAXB vs XStream(11009)
5.?在Spring基礎(chǔ)上實現(xiàn)自己的異常處理框架(7757)
評論排行榜
1.?《Head First Design Pattern 單例模式》中double check有問題嗎?(22)
2.?oc4j+toplink+spring使用jta外部事務(wù)的一點心得(11)
3.?剝下“java.lang.OutOfMemoryError: unable to create new native thread”的外衣(10)
4.?JAXB vs XStream(9)
5.?在Spring基礎(chǔ)上實現(xiàn)自己的異常處理框架(8)
java實現(xiàn)的同步循環(huán)鏈表
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
叱咤紅人
閱讀(539)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發(fā)表評論。
網(wǎng)站導(dǎo)航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 叱咤紅人
主站蜘蛛池模板:
穆棱市
|
呼图壁县
|
淅川县
|
拜城县
|
临西县
|
仁化县
|
太保市
|
资兴市
|
云霄县
|
达日县
|
阳谷县
|
晋中市
|
红河县
|
西安市
|
娱乐
|
盐池县
|
六安市
|
德州市
|
呼图壁县
|
景德镇市
|
张北县
|
永兴县
|
龙泉市
|
贞丰县
|
汝城县
|
全椒县
|
同江市
|
盐边县
|
南安市
|
阜阳市
|
达尔
|
库尔勒市
|
乌恰县
|
崇义县
|
黎平县
|
盖州市
|
平昌县
|
三江
|
明光市
|
陆川县
|
夏河县
|