2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 package com
.intellij
.concurrency
;
22 import com
.intellij
.openapi
.application
.RuntimeInterruptedException
;
23 import com
.intellij
.openapi
.diagnostic
.Logger
;
25 import java
.util
.concurrent
.Callable
;
26 import java
.util
.concurrent
.FutureTask
;
27 import java
.util
.concurrent
.ExecutionException
;
28 import java
.util
.concurrent
.CancellationException
;
29 import java
.util
.concurrent
.locks
.Condition
;
30 import java
.util
.concurrent
.locks
.Lock
;
31 import java
.util
.concurrent
.locks
.ReentrantLock
;
33 public class PrioritizedFutureTask
<T
> extends FutureTask
<T
> implements Comparable
<PrioritizedFutureTask
> {
34 private static final Logger LOG
= Logger
.getInstance("#com.intellij.concurrency.PrioritizedFutureTask");
35 private final long myJobIndex
;
36 private final int myTaskIndex
;
37 private final int myPriority
;
38 private final boolean myParentThreadHasReadAccess
;
39 private final boolean myReportExceptions
;
40 private final Lock myLock
;
41 private volatile Condition myDoneCondition
;
43 public PrioritizedFutureTask(final Callable
<T
> callable
, long jobIndex
, int taskIndex
, int priority
, final boolean parentThreadHasReadAccess
, boolean reportExceptions
) {
45 myJobIndex
= jobIndex
;
46 myTaskIndex
= taskIndex
;
47 myPriority
= priority
;
48 myParentThreadHasReadAccess
= parentThreadHasReadAccess
;
49 myReportExceptions
= reportExceptions
;
51 myLock
= new ReentrantLock();
54 public boolean isParentThreadHasReadAccess() {
55 return myParentThreadHasReadAccess
;
58 public int compareTo(final PrioritizedFutureTask o
) {
59 if (getPriority() != o
.getPriority()) return getPriority() - o
.getPriority();
60 if (getTaskIndex() != o
.getTaskIndex()) return getTaskIndex() - o
.getTaskIndex();
61 if (getJobIndex() != o
.getJobIndex()) return getJobIndex() < o
.getJobIndex() ?
-1 : 1;
65 public long getJobIndex() {
69 public int getTaskIndex() {
73 public int getPriority() {
77 public void signalStarted() {
80 myDoneCondition
= myLock
.newCondition();
87 public void signalDone() {
90 myDoneCondition
.signalAll();
91 myDoneCondition
= null;
98 public void awaitTermination() {
101 if (myDoneCondition
== null) return;
102 myDoneCondition
.await();
104 catch (InterruptedException e
) {
105 throw new RuntimeInterruptedException(e
);
113 protected void done() {
114 if (myReportExceptions
) {
115 // let exceptions during execution manifest themselves
119 catch (CancellationException e
) {
122 catch (InterruptedException e
) {
125 catch (ExecutionException e
) {