Revision 11042 trunk/libraries/libUIComponent/src-test/org/gvsig/gui/beans/incrementableTask/TestIncrementableTask.java
TestIncrementableTask.java | ||
---|---|---|
25 | 25 |
import org.gvsig.gui.beans.progresspanel.LogControl; |
26 | 26 |
|
27 | 27 |
public class TestIncrementableTask { |
28 |
class ClassProcess extends Thread implements IIncrementable, IncrementableListener {
|
|
28 |
class ClassProcess implements Runnable, IIncrementable, IncrementableListener {
|
|
29 | 29 |
int i = 0; |
30 | 30 |
long j = 0; |
31 | 31 |
LogControl log = new LogControl(); |
32 |
IncrementableTask incrementableTask = null; |
|
32 | 33 |
|
34 |
private volatile Thread blinker; |
|
35 |
private boolean ended = false; |
|
36 |
private boolean threadSuspended = false; |
|
37 |
|
|
33 | 38 |
public ClassProcess() { |
34 |
this.start(); |
|
35 | 39 |
} |
36 | 40 |
|
41 |
public void start() { |
|
42 |
blinker = new Thread(this); |
|
43 |
blinker.start(); |
|
44 |
} |
|
45 |
|
|
46 |
public synchronized void stop() { |
|
47 |
ended = true; |
|
48 |
blinker = null; |
|
49 |
notify(); |
|
50 |
} |
|
51 |
|
|
52 |
public boolean isAlive() { |
|
53 |
return blinker.isAlive(); |
|
54 |
} |
|
55 |
|
|
56 |
|
|
37 | 57 |
public synchronized void run(){ |
38 | 58 |
for (j=0; j<=65535; j++) { |
59 |
if (ended) break; |
|
39 | 60 |
for (long i=0; i<=65535; i++); |
40 | 61 |
log.replaceLastLine(j + ""); |
41 | 62 |
if ((j%1000)==0) { |
42 | 63 |
log.addLine(j + ""); |
43 | 64 |
} |
65 |
synchronized(this) { |
|
66 |
while (threadSuspended && !ended) |
|
67 |
try { |
|
68 |
wait(500); |
|
69 |
} catch (InterruptedException e) { |
|
70 |
e.printStackTrace(); |
|
71 |
} |
|
72 |
} |
|
44 | 73 |
} |
74 |
while (incrementableTask.isAlive()); |
|
75 |
incrementableTask.Hide(); |
|
76 |
incrementableTask = null; |
|
45 | 77 |
} |
46 | 78 |
|
47 | 79 |
public String getLabel() { |
... | ... | |
60 | 92 |
return "TestIncrementTask"; |
61 | 93 |
} |
62 | 94 |
|
63 |
public void actionCanceled(IncrementableEvent e) {
|
|
64 |
this.suspend();
|
|
95 |
public void setIncrementableTask(IncrementableTask value) {
|
|
96 |
incrementableTask = value;
|
|
65 | 97 |
} |
66 | 98 |
|
67 |
public void actionClosed(IncrementableEvent e) {
|
|
68 |
this.suspend();
|
|
99 |
public void actionCanceled(IncrementableEvent e) {
|
|
100 |
ended = true;
|
|
69 | 101 |
} |
70 | 102 |
|
71 | 103 |
public void actionResumed(IncrementableEvent e) { |
72 |
this.resume();
|
|
104 |
threadSuspended = false;
|
|
73 | 105 |
} |
74 | 106 |
|
75 | 107 |
public void actionSuspended(IncrementableEvent e) { |
76 |
this.suspend();
|
|
108 |
threadSuspended = true;
|
|
77 | 109 |
} |
78 | 110 |
} |
79 | 111 |
|
80 |
ClassProcess ncp = new ClassProcess();
|
|
112 |
ClassProcess classProcess = null;
|
|
81 | 113 |
|
82 | 114 |
public TestIncrementableTask() { |
83 | 115 |
super(); |
... | ... | |
85 | 117 |
} |
86 | 118 |
|
87 | 119 |
private void initialize() { |
88 |
IncrementableTask it = new IncrementableTask(ncp); |
|
89 |
it.showWindow(); |
|
90 |
it.addIncrementableListener(ncp); |
|
120 |
classProcess = new ClassProcess(); |
|
121 |
IncrementableTask incrementableTask = new IncrementableTask(classProcess); |
|
122 |
classProcess.setIncrementableTask(incrementableTask); |
|
123 |
incrementableTask.showWindow(); |
|
124 |
incrementableTask.addIncrementableListener(classProcess); |
|
125 |
|
|
126 |
incrementableTask.start(); |
|
127 |
classProcess.start(); |
|
128 |
while (classProcess.isAlive()) { |
|
129 |
try { |
|
130 |
Thread.sleep(500); |
|
131 |
} catch (InterruptedException e) { |
|
132 |
e.printStackTrace(); |
|
133 |
} |
|
134 |
} |
|
135 |
classProcess.stop(); |
|
136 |
classProcess = null; |
|
137 |
incrementableTask = null; |
|
91 | 138 |
} |
92 | 139 |
|
93 | 140 |
public static void main(String[] args) { |
Also available in: Unified diff