15
15
16
16
import org .eclipse .core .runtime .Assert ;
17
17
import org .eclipse .core .runtime .IProgressMonitor ;
18
+ import org .eclipse .core .runtime .IStatus ;
18
19
import org .eclipse .core .runtime .NullProgressMonitor ;
20
+ import org .eclipse .core .runtime .Status ;
21
+ import org .eclipse .core .runtime .jobs .Job ;
19
22
20
23
import org .eclipse .jface .text .DocumentEvent ;
21
24
import org .eclipse .jface .text .IDocument ;
@@ -53,7 +56,7 @@ abstract public class AbstractReconciler implements IReconciler {
53
56
/**
54
57
* Background thread for the reconciling activity.
55
58
*/
56
- class BackgroundThread extends Thread {
59
+ private class BackgroundThread extends Job {
57
60
58
61
/** Has the reconciler been canceled. */
59
62
private boolean fCanceled = false ;
@@ -64,6 +67,10 @@ class BackgroundThread extends Thread {
64
67
/** Is a reconciling strategy active. */
65
68
private boolean fIsActive = false ;
66
69
70
+ private volatile boolean fIsAlive ;
71
+
72
+ private boolean started ;
73
+
67
74
/**
68
75
* Creates a new background thread. The thread
69
76
* runs with minimal priority.
@@ -72,8 +79,8 @@ class BackgroundThread extends Thread {
72
79
*/
73
80
public BackgroundThread (String name ) {
74
81
super (name );
75
- setPriority (Thread . MIN_PRIORITY );
76
- setDaemon (true );
82
+ setPriority (Job . DECORATE );
83
+ setSystem (true );
77
84
}
78
85
79
86
/**
@@ -98,7 +105,7 @@ public synchronized boolean isDirty() {
98
105
/**
99
106
* Cancels the background thread.
100
107
*/
101
- public void cancel () {
108
+ public void doCancel () {
102
109
fCanceled = true ;
103
110
IProgressMonitor pm = fProgressMonitor ;
104
111
if (pm != null )
@@ -153,7 +160,9 @@ public void reset() {
153
160
fDirtyRegionQueue .notifyAll ();
154
161
}
155
162
}
156
-
163
+ synchronized (this ) {
164
+ started = false ;
165
+ }
157
166
informNotFinished ();
158
167
reconcilerReset ();
159
168
}
@@ -167,12 +176,12 @@ public void reset() {
167
176
* </p>
168
177
*/
169
178
@ Override
170
- public void run () {
171
-
179
+ public IStatus run (IProgressMonitor monitor ) {
180
+ fIsAlive = true ;
172
181
delay ();
173
182
174
183
if (fCanceled )
175
- return ;
184
+ return Status . CANCEL_STATUS ;
176
185
177
186
initialProcess ();
178
187
@@ -217,6 +226,24 @@ public void run() {
217
226
218
227
fIsActive = false ;
219
228
}
229
+ fIsAlive = false ;
230
+ return Status .OK_STATUS ;
231
+ }
232
+
233
+ public boolean isAlive () {
234
+ return fIsAlive ;
235
+ }
236
+
237
+ public synchronized void start () {
238
+ if (!started ) {
239
+ started = true ;
240
+ schedule ();
241
+ }
242
+ }
243
+
244
+ @ Override
245
+ public boolean belongsTo (Object family ) {
246
+ return family == fViewer || AbstractReconciler .class == family ;
220
247
}
221
248
}
222
249
@@ -233,7 +260,7 @@ public void documentAboutToBeChanged(DocumentEvent e) {
233
260
public void documentChanged (DocumentEvent e ) {
234
261
235
262
if (fThread .isActive () || !fThread .isDirty () && fThread .isAlive ()) {
236
- if (!fIsAllowedToModifyDocument && Thread . currentThread () == fThread )
263
+ if (!fIsAllowedToModifyDocument && isRunningInReconcilerThread () )
237
264
throw new UnsupportedOperationException ("The reconciler thread is not allowed to modify the document" ); //$NON-NLS-1$
238
265
aboutToBeReconciledInternal ();
239
266
}
@@ -485,7 +512,7 @@ public void uninstall() {
485
512
// http://dev.eclipse.org/bugs/show_bug.cgi?id=19135
486
513
BackgroundThread bt = fThread ;
487
514
fThread = null ;
488
- bt .cancel ();
515
+ bt .doCancel ();
489
516
}
490
517
}
491
518
}
@@ -614,14 +641,7 @@ protected synchronized void startReconciling() {
614
641
return ;
615
642
616
643
if (!fThread .isAlive ()) {
617
- try {
618
- fThread .start ();
619
- } catch (IllegalThreadStateException e ) {
620
- // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=40549
621
- // This is the only instance where the thread is started; since
622
- // we checked that it is not alive, it must be dead already due
623
- // to a run-time exception or error. Exit.
624
- }
644
+ fThread .start ();
625
645
} else {
626
646
fThread .reset ();
627
647
}
@@ -640,7 +660,10 @@ protected void reconcilerReset() {
640
660
* @return <code>true</code> if running in this reconciler's background thread
641
661
* @since 3.4
642
662
*/
643
- protected boolean isRunningInReconcilerThread () {
644
- return Thread .currentThread () == fThread ;
663
+ protected synchronized boolean isRunningInReconcilerThread () {
664
+ if (fThread == null ) {
665
+ return false ;
666
+ }
667
+ return Job .getJobManager ().currentJob () == fThread ;
645
668
}
646
- }
669
+ }
0 commit comments