Biblioteca Java - Blame information for rev 2
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mihai | 1 | package colectii.queue; |
2 | |||
3 | import java.util.PriorityQueue; | ||
4 | |||
5 | public class TestQueue { | ||
6 | public static void main(String[] args) { | ||
7 | Job j1 = new Job("chek trains on input rail segments",3); | ||
8 | Job j2 = new Job("chek trains on ouput rail segments",2); | ||
9 | Job j3 = new Job("chek trains on rail station segments",1); | ||
10 | |||
11 | PriorityQueue<Job> que = new PriorityQueue<Job>(); | ||
12 | que.offer(j1); | ||
13 | que.offer(j2); | ||
14 | que.offer(j3); | ||
15 | |||
16 | while(que.size()!=0){ | ||
17 | Job j = (Job)que.poll(); | ||
18 | j.execute(); | ||
19 | } | ||
20 | } | ||
21 | } | ||
22 | |||
23 | class Job implements Comparable{ | ||
24 | |||
25 | int priority; | ||
26 | String name; | ||
27 | |||
28 | public Job(String name,int priority) { | ||
29 | this.priority = priority; | ||
30 | this.name = name; | ||
31 | } | ||
32 | |||
33 | public void execute(){ | ||
34 | System.out.println("Execute job:"+name+" - job priority="+priority); | ||
35 | } | ||
36 | |||
37 | public int compareTo(Object o) { | ||
38 | Job x = (Job)o; | ||
39 | if(priority>x.priority){ | ||
40 | return 1; | ||
41 | }else if(priority==x.priority) | ||
42 | return 0; | ||
43 | else | ||
44 | return -1; | ||
45 | } | ||
46 | } |