ComponentWithBrowseButton - optional remove listener on hide
[fedora-idea.git] / platform / platform-impl / src / com / intellij / concurrency / PrioritizedFutureTask.java
blobad7855f2002e088640d590ca443969d61b1a43d1
1 /*
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.
18 * @author max
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) {
44 super(callable);
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;
62 return 0;
65 public long getJobIndex() {
66 return myJobIndex;
69 public int getTaskIndex() {
70 return myTaskIndex;
73 public int getPriority() {
74 return myPriority;
77 public void signalStarted() {
78 myLock.lock();
79 try {
80 myDoneCondition = myLock.newCondition();
82 finally {
83 myLock.unlock();
87 public void signalDone() {
88 myLock.lock();
89 try {
90 myDoneCondition.signalAll();
91 myDoneCondition = null;
93 finally {
94 myLock.unlock();
98 public void awaitTermination() {
99 myLock.lock();
100 try {
101 if (myDoneCondition == null) return;
102 myDoneCondition.await();
104 catch (InterruptedException e) {
105 throw new RuntimeInterruptedException(e);
107 finally {
108 myLock.unlock();
112 @Override
113 protected void done() {
114 if (myReportExceptions) {
115 // let exceptions during execution manifest themselves
116 try {
117 get();
119 catch (CancellationException e) {
120 //ignore
122 catch (InterruptedException e) {
123 LOG.error(e);
125 catch (ExecutionException e) {
126 LOG.error(e);