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 @ 40559
History | View | Annotate | Download (4.78 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/*
|
25 |
* AUTHORS (In addition to CIT):
|
26 |
* 2009 IVER T.I. S.A. {{Task}}
|
27 |
*/
|
28 |
|
29 |
package org.gvsig.fmap.dal.feature.testmulithread; |
30 |
|
31 |
import java.util.Random; |
32 |
|
33 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
34 |
|
35 |
public abstract class StoreTask extends Thread { |
36 |
|
37 |
public static final int STATUS_NONE = -1; |
38 |
public static final int STATUS_WAITING = 0; |
39 |
public static final int STATUS_RUNING = 1; |
40 |
|
41 |
public static final int STATUS_FINISHED_OK = 20; |
42 |
public static final int STATUS_FINISHED_NO_OK = 30; |
43 |
public static final int STATUS_FINISHED_ERROR = 40; |
44 |
public static final int STATUS_FINISHED_CONCURRENT_ERROR = 50; |
45 |
public static final int STATUS_ERROR = 60; |
46 |
|
47 |
public static final int TIME_TO_WAIT_RANDOM = -1; |
48 |
public static final int TIME_TO_WAIT_NO_WAIT = 0; |
49 |
|
50 |
private long startTimer = 0; |
51 |
private long afterWaitTimer = 0; |
52 |
private long finishTimer = 0; |
53 |
|
54 |
private int timeToWait = 0; |
55 |
private int finalTimeToWait = 0; |
56 |
|
57 |
private int timeToOutOfDate = 5000; |
58 |
|
59 |
private int curStatus = STATUS_NONE; |
60 |
|
61 |
protected FeatureStore store;
|
62 |
protected Throwable exception; |
63 |
|
64 |
public StoreTask(String name, FeatureStore store) { |
65 |
super(name);
|
66 |
this.store = store;
|
67 |
this.timeToWait = TIME_TO_WAIT_NO_WAIT;
|
68 |
} |
69 |
|
70 |
public StoreTask(String name, FeatureStore store, int timeToWait) { |
71 |
super(name);
|
72 |
this.store = store;
|
73 |
this.timeToWait = timeToWait;
|
74 |
} |
75 |
|
76 |
public int getTimeToOutOfDate() { |
77 |
return this.timeToOutOfDate; |
78 |
} |
79 |
|
80 |
public void setTimeToOutOfDate(int mlSeconds) { |
81 |
this.timeToOutOfDate = mlSeconds;
|
82 |
} |
83 |
|
84 |
public long getStartTimer() { |
85 |
return this.startTimer; |
86 |
} |
87 |
|
88 |
public long getAfterWaitTimer() { |
89 |
return this.afterWaitTimer; |
90 |
} |
91 |
|
92 |
public long getFinishTimer() { |
93 |
return this.finishTimer; |
94 |
} |
95 |
|
96 |
public int getCurrentStatus() { |
97 |
return this.curStatus; |
98 |
} |
99 |
|
100 |
public boolean isFinishedOk() { |
101 |
return this.curStatus == STATUS_FINISHED_OK; |
102 |
} |
103 |
|
104 |
public Throwable getException() { |
105 |
return exception;
|
106 |
} |
107 |
|
108 |
public boolean isOutOfDate() { |
109 |
if (curStatus < STATUS_RUNING) {
|
110 |
return false; |
111 |
} |
112 |
long curTimer = System.currentTimeMillis(); |
113 |
if ((curTimer - afterWaitTimer) > (finalTimeToWait + 5000)) { |
114 |
return true; |
115 |
} |
116 |
return false; |
117 |
} |
118 |
|
119 |
protected boolean startProcess() { |
120 |
if (this.curStatus != STATUS_NONE) { |
121 |
throw new IllegalStateException(); |
122 |
} |
123 |
this.startTimer = System.currentTimeMillis(); |
124 |
this.curStatus = STATUS_WAITING;
|
125 |
if (timeToWait != TIME_TO_WAIT_NO_WAIT) {
|
126 |
if (timeToWait == TIME_TO_WAIT_RANDOM) {
|
127 |
Random rnd = new Random(); |
128 |
rnd.setSeed(System.currentTimeMillis());
|
129 |
finalTimeToWait = rnd.nextInt(200) + 4; |
130 |
|
131 |
} else {
|
132 |
finalTimeToWait = timeToWait; |
133 |
} |
134 |
try {
|
135 |
for (int x = 0; x < 4; x++) { |
136 |
yield(); |
137 |
sleep(finalTimeToWait / 4);
|
138 |
} |
139 |
} catch (InterruptedException e) { |
140 |
this.curStatus = STATUS_FINISHED_ERROR;
|
141 |
this.exception = e;
|
142 |
return false; |
143 |
} |
144 |
} |
145 |
this.afterWaitTimer = System.currentTimeMillis(); |
146 |
this.curStatus = STATUS_RUNING;
|
147 |
|
148 |
return true; |
149 |
} |
150 |
|
151 |
protected void finishedOk() { |
152 |
if (this.curStatus != STATUS_RUNING) { |
153 |
throw new IllegalStateException(); |
154 |
} |
155 |
this.finishTimer = System.currentTimeMillis(); |
156 |
this.curStatus = STATUS_FINISHED_OK;
|
157 |
|
158 |
} |
159 |
|
160 |
protected void finishedNoOk() { |
161 |
if (this.curStatus != STATUS_RUNING) { |
162 |
throw new IllegalStateException(); |
163 |
} |
164 |
this.finishTimer = System.currentTimeMillis(); |
165 |
this.curStatus = STATUS_FINISHED_NO_OK;
|
166 |
|
167 |
} |
168 |
|
169 |
protected void finishedError(Throwable ex) { |
170 |
if (this.curStatus != STATUS_RUNING) { |
171 |
throw new IllegalStateException(); |
172 |
} |
173 |
this.finishTimer = System.currentTimeMillis(); |
174 |
this.curStatus = STATUS_FINISHED_ERROR;
|
175 |
this.exception = ex;
|
176 |
} |
177 |
|
178 |
protected void finishedConcurrentError(Exception ex) { |
179 |
if (this.curStatus != STATUS_RUNING) { |
180 |
throw new IllegalStateException(); |
181 |
} |
182 |
this.finishTimer = System.currentTimeMillis(); |
183 |
this.curStatus = STATUS_FINISHED_CONCURRENT_ERROR;
|
184 |
this.exception = ex;
|
185 |
} |
186 |
|
187 |
|
188 |
public boolean isFinished() { |
189 |
return curStatus >= STATUS_FINISHED_OK;
|
190 |
|
191 |
} |
192 |
|
193 |
|
194 |
} |