Statistics
| Revision:

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 @ 40596

History | View | Annotate | Download (4.71 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
package org.gvsig.fmap.dal.feature.testmulithread;
26

    
27
import java.util.Random;
28

    
29
import org.gvsig.fmap.dal.feature.FeatureStore;
30

    
31
public abstract class StoreTask extends Thread {
32

    
33
        public static final int STATUS_NONE = -1;
34
        public static final int STATUS_WAITING = 0;
35
        public static final int STATUS_RUNING = 1;
36

    
37
        public static final int STATUS_FINISHED_OK = 20;
38
        public static final int STATUS_FINISHED_NO_OK = 30;
39
        public static final int STATUS_FINISHED_ERROR = 40;
40
        public static final int STATUS_FINISHED_CONCURRENT_ERROR = 50;
41
        public static final int STATUS_ERROR = 60;
42

    
43
        public static final int TIME_TO_WAIT_RANDOM = -1;
44
        public static final int TIME_TO_WAIT_NO_WAIT = 0;
45

    
46
        private long startTimer = 0;
47
        private long afterWaitTimer = 0;
48
        private long finishTimer = 0;
49

    
50
        private int timeToWait = 0;
51
        private int finalTimeToWait = 0;
52

    
53
        private int timeToOutOfDate = 5000;
54

    
55
        private int curStatus = STATUS_NONE;
56

    
57
        protected FeatureStore store;
58
        protected Throwable exception;
59

    
60
        public StoreTask(String name, FeatureStore store) {
61
                super(name);
62
                this.store = store;
63
                this.timeToWait = TIME_TO_WAIT_NO_WAIT;
64
        }
65

    
66
        public StoreTask(String name, FeatureStore store, int timeToWait) {
67
                super(name);
68
                this.store = store;
69
                this.timeToWait = timeToWait;
70
        }
71

    
72
        public int getTimeToOutOfDate() {
73
                return this.timeToOutOfDate;
74
        }
75

    
76
        public void setTimeToOutOfDate(int mlSeconds) {
77
                this.timeToOutOfDate = mlSeconds;
78
        }
79

    
80
        public long getStartTimer() {
81
                return this.startTimer;
82
        }
83

    
84
        public long getAfterWaitTimer() {
85
                return this.afterWaitTimer;
86
        }
87

    
88
        public long getFinishTimer() {
89
                return this.finishTimer;
90
        }
91

    
92
        public int getCurrentStatus() {
93
                return this.curStatus;
94
        }
95

    
96
        public boolean isFinishedOk() {
97
                return this.curStatus == STATUS_FINISHED_OK;
98
        }
99

    
100
        public Throwable getException() {
101
                return exception;
102
        }
103

    
104
        public boolean isOutOfDate() {
105
                if (curStatus < STATUS_RUNING) {
106
                        return false;
107
                }
108
                long curTimer = System.currentTimeMillis();
109
                if ((curTimer - afterWaitTimer) > (finalTimeToWait + 5000)) {
110
                        return true;
111
                }
112
                return false;
113
        }
114

    
115
        protected boolean startProcess() {
116
                if (this.curStatus != STATUS_NONE) {
117
                        throw new IllegalStateException();
118
                }
119
                this.startTimer = System.currentTimeMillis();
120
                this.curStatus = STATUS_WAITING;
121
                if (timeToWait != TIME_TO_WAIT_NO_WAIT) {
122
                        if (timeToWait == TIME_TO_WAIT_RANDOM) {
123
                                Random rnd = new Random();
124
                                rnd.setSeed(System.currentTimeMillis());
125
                                finalTimeToWait = rnd.nextInt(200) + 4;
126

    
127
                        } else {
128
                                finalTimeToWait = timeToWait;
129
                        }
130
                        try {
131
                                for (int x = 0; x < 4; x++) {
132
                                        yield();
133
                                        sleep(finalTimeToWait / 4);
134
                                }
135
                        } catch (InterruptedException e) {
136
                                this.curStatus = STATUS_FINISHED_ERROR;
137
                                this.exception = e;
138
                                return false;
139
                        }
140
                }
141
                this.afterWaitTimer = System.currentTimeMillis();
142
                this.curStatus = STATUS_RUNING;
143

    
144
                return true;
145
        }
146

    
147
        protected void finishedOk() {
148
                if (this.curStatus != STATUS_RUNING) {
149
                        throw new IllegalStateException();
150
                }
151
                this.finishTimer = System.currentTimeMillis();
152
                this.curStatus = STATUS_FINISHED_OK;
153

    
154
        }
155

    
156
        protected void finishedNoOk() {
157
                if (this.curStatus != STATUS_RUNING) {
158
                        throw new IllegalStateException();
159
                }
160
                this.finishTimer = System.currentTimeMillis();
161
                this.curStatus = STATUS_FINISHED_NO_OK;
162

    
163
        }
164

    
165
        protected void finishedError(Throwable ex) {
166
                if (this.curStatus != STATUS_RUNING) {
167
                        throw new IllegalStateException();
168
                }
169
                this.finishTimer = System.currentTimeMillis();
170
                this.curStatus = STATUS_FINISHED_ERROR;
171
                this.exception = ex;
172
        }
173

    
174
        protected void finishedConcurrentError(Exception ex) {
175
                if (this.curStatus != STATUS_RUNING) {
176
                        throw new IllegalStateException();
177
                }
178
                this.finishTimer = System.currentTimeMillis();
179
                this.curStatus = STATUS_FINISHED_CONCURRENT_ERROR;
180
                this.exception = ex;
181
        }
182

    
183

    
184
        public boolean isFinished() {
185
                return curStatus >= STATUS_FINISHED_OK;
186

    
187
        }
188

    
189

    
190
}