|
发表于 1-23-2015 06:57 PM
|
显示全部楼层
这题用下线程安全的priority queue就行了吧 价格wrapper class 我大致写了下框架 欢迎讨论% B4 g, r7 T2 \; B& o4 v
class mythread {) g9 M1 ?9 D! e4 x
int time;
3 Y& l" \$ L! r. i1 p2 O- V Thread t;
; W$ h v* s! @
: d7 m! G1 H0 b( ]/ C1 F public mythread(int time, Thread t) {, ?$ `$ e* t2 G, I4 `$ i
this.t = t;
) N7 u5 g& y, [ this.time = time;
! Z5 I& M7 `# P& M# z% E) Y7 w* E }
! ]9 b0 _$ }. \+ L+ _/ m$ e) E}
9 V2 ?1 |1 f! I! x
1 z) U8 R8 l& Y" Z6 V0 |- y- Npublic class multi_thread {- K$ l- e3 j$ f; S# f8 V
PriorityBlockingQueue<mythread> queue = new PriorityBlockingQueue<mythread>(100,5 B; s) S. B, u; A* U* O8 s
new Comparator<mythread>() {8 W: b, A; C+ D) _; T) L2 s
@Override: X- j' q; l# ^! B$ e% I" Z- N L+ j
public int compare(mythread o1, mythread o2) {7 p( p, k( C- b+ S% T: u
if (o1.time < o2.time)' I: L$ i8 W6 i) l2 ~$ Z5 X5 ~7 N
return -1;
' E" X+ b, M: \* ^' f0 A4 h& o o else if (o1.time > o2.time)* @6 E# C' d. l; f6 ?7 _
return 1;
% v* [9 U* O# v! F- T; E // TODO Auto-generated method stub2 r/ j& M. ^5 C6 h% s4 f& \ G
return 0;3 S" ?1 K+ S# E' Z$ V! w# S$ h
}
9 x8 O; B8 W$ w. f" D3 X0 R- h
- Z9 E" ` ~: e6 X' t I' n6 d });" w' b3 T6 A# y* T8 X: e. K
0 {$ \ v' @4 G- w2 D2 f public Thread pop() {
( w" x# b3 k3 @; j' A9 m$ R return queue.poll().t;
) N& H0 J% o8 w+ `* A }
" r& ]: f' h0 ]2 _, P( Q A7 i; \" h
public void add(int time, Thread t) {$ z3 v! Z4 K' P: ?
mythread newthread = new mythread(time, t);* l. M+ o a' F, u) H3 ?9 j8 b- N
queue.add(newthread);
2 S8 V9 C- J3 { |0 T l }
) A5 W. T3 z y} |
|