svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / test / java / org / gvsig / fmap / dal / feature / testmulithread / StoreTask.java @ 40435
History | View | Annotate | Download (4.73 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I. S.A. {{Task}}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.feature.testmulithread; |
29 |
|
30 |
import java.util.Random; |
31 |
|
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
|
34 |
public abstract class StoreTask extends Thread { |
35 |
|
36 |
public static final int STATUS_NONE = -1; |
37 |
public static final int STATUS_WAITING = 0; |
38 |
public static final int STATUS_RUNING = 1; |
39 |
|
40 |
public static final int STATUS_FINISHED_OK = 20; |
41 |
public static final int STATUS_FINISHED_NO_OK = 30; |
42 |
public static final int STATUS_FINISHED_ERROR = 40; |
43 |
public static final int STATUS_FINISHED_CONCURRENT_ERROR = 50; |
44 |
public static final int STATUS_ERROR = 60; |
45 |
|
46 |
public static final int TIME_TO_WAIT_RANDOM = -1; |
47 |
public static final int TIME_TO_WAIT_NO_WAIT = 0; |
48 |
|
49 |
private long startTimer = 0; |
50 |
private long afterWaitTimer = 0; |
51 |
private long finishTimer = 0; |
52 |
|
53 |
private int timeToWait = 0; |
54 |
private int finalTimeToWait = 0; |
55 |
|
56 |
private int timeToOutOfDate = 5000; |
57 |
|
58 |
private int curStatus = STATUS_NONE; |
59 |
|
60 |
protected FeatureStore store;
|
61 |
protected Throwable exception; |
62 |
|
63 |
public StoreTask(String name, FeatureStore store) { |
64 |
super(name);
|
65 |
this.store = store;
|
66 |
this.timeToWait = TIME_TO_WAIT_NO_WAIT;
|
67 |
} |
68 |
|
69 |
public StoreTask(String name, FeatureStore store, int timeToWait) { |
70 |
super(name);
|
71 |
this.store = store;
|
72 |
this.timeToWait = timeToWait;
|
73 |
} |
74 |
|
75 |
public int getTimeToOutOfDate() { |
76 |
return this.timeToOutOfDate; |
77 |
} |
78 |
|
79 |
public void setTimeToOutOfDate(int mlSeconds) { |
80 |
this.timeToOutOfDate = mlSeconds;
|
81 |
} |
82 |
|
83 |
public long getStartTimer() { |
84 |
return this.startTimer; |
85 |
} |
86 |
|
87 |
public long getAfterWaitTimer() { |
88 |
return this.afterWaitTimer; |
89 |
} |
90 |
|
91 |
public long getFinishTimer() { |
92 |
return this.finishTimer; |
93 |
} |
94 |
|
95 |
public int getCurrentStatus() { |
96 |
return this.curStatus; |
97 |
} |
98 |
|
99 |
public boolean isFinishedOk() { |
100 |
return this.curStatus == STATUS_FINISHED_OK; |
101 |
} |
102 |
|
103 |
public Throwable getException() { |
104 |
return exception;
|
105 |
} |
106 |
|
107 |
public boolean isOutOfDate() { |
108 |
if (curStatus < STATUS_RUNING) {
|
109 |
return false; |
110 |
} |
111 |
long curTimer = System.currentTimeMillis(); |
112 |
if ((curTimer - afterWaitTimer) > (finalTimeToWait + 5000)) { |
113 |
return true; |
114 |
} |
115 |
return false; |
116 |
} |
117 |
|
118 |
protected boolean startProcess() { |
119 |
if (this.curStatus != STATUS_NONE) { |
120 |
throw new IllegalStateException(); |
121 |
} |
122 |
this.startTimer = System.currentTimeMillis(); |
123 |
this.curStatus = STATUS_WAITING;
|
124 |
if (timeToWait != TIME_TO_WAIT_NO_WAIT) {
|
125 |
if (timeToWait == TIME_TO_WAIT_RANDOM) {
|
126 |
Random rnd = new Random(); |
127 |
rnd.setSeed(System.currentTimeMillis());
|
128 |
finalTimeToWait = rnd.nextInt(200) + 4; |
129 |
|
130 |
} else {
|
131 |
finalTimeToWait = timeToWait; |
132 |
} |
133 |
try {
|
134 |
for (int x = 0; x < 4; x++) { |
135 |
yield(); |
136 |
sleep(finalTimeToWait / 4);
|
137 |
} |
138 |
} catch (InterruptedException e) { |
139 |
this.curStatus = STATUS_FINISHED_ERROR;
|
140 |
this.exception = e;
|
141 |
return false; |
142 |
} |
143 |
} |
144 |
this.afterWaitTimer = System.currentTimeMillis(); |
145 |
this.curStatus = STATUS_RUNING;
|
146 |
|
147 |
return true; |
148 |
} |
149 |
|
150 |
protected void finishedOk() { |
151 |
if (this.curStatus != STATUS_RUNING) { |
152 |
throw new IllegalStateException(); |
153 |
} |
154 |
this.finishTimer = System.currentTimeMillis(); |
155 |
this.curStatus = STATUS_FINISHED_OK;
|
156 |
|
157 |
} |
158 |
|
159 |
protected void finishedNoOk() { |
160 |
if (this.curStatus != STATUS_RUNING) { |
161 |
throw new IllegalStateException(); |
162 |
} |
163 |
this.finishTimer = System.currentTimeMillis(); |
164 |
this.curStatus = STATUS_FINISHED_NO_OK;
|
165 |
|
166 |
} |
167 |
|
168 |
protected void finishedError(Throwable ex) { |
169 |
if (this.curStatus != STATUS_RUNING) { |
170 |
throw new IllegalStateException(); |
171 |
} |
172 |
this.finishTimer = System.currentTimeMillis(); |
173 |
this.curStatus = STATUS_FINISHED_ERROR;
|
174 |
this.exception = ex;
|
175 |
} |
176 |
|
177 |
protected void finishedConcurrentError(Exception ex) { |
178 |
if (this.curStatus != STATUS_RUNING) { |
179 |
throw new IllegalStateException(); |
180 |
} |
181 |
this.finishTimer = System.currentTimeMillis(); |
182 |
this.curStatus = STATUS_FINISHED_CONCURRENT_ERROR;
|
183 |
this.exception = ex;
|
184 |
} |
185 |
|
186 |
|
187 |
public boolean isFinished() { |
188 |
return curStatus >= STATUS_FINISHED_OK;
|
189 |
|
190 |
} |
191 |
|
192 |
|
193 |
} |