~ubuntu-branches/ubuntu/vivid/gluegen2/vivid-proposed

« back to all changes in this revision

Viewing changes to src/java/com/jogamp/common/util/RunnableTask.java

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-12-26 01:18:19 UTC
  • mfrom: (1.2.1) (9.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20111226011819-tkt9ndkn621bim5v
Tags: 2.0-rc5-1~exp1
* New upstream release
* watch now possible and integrated

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 */
35
35
public class RunnableTask implements Runnable {
36
36
    Runnable runnable;
37
 
    final Object notifyObject;
 
37
    final Object syncObject;
38
38
    boolean catchExceptions;
39
39
    Object attachment;
40
40
 
41
41
    Throwable runnableException;
42
42
    long ts0, ts1, ts2;
43
43
 
 
44
    /**
 
45
     * Create a RunnableTask object w/o synchronization,
 
46
     * ie. not suitable for <code>invokeAndWait()</code>. 
 
47
     * 
 
48
     * @param runnable the user action
 
49
     */
44
50
    public RunnableTask(Runnable runnable) {
45
51
        this(runnable, null, false);
46
52
    }
47
53
 
48
 
    public RunnableTask(Runnable runnable, Object notifyObject) {
49
 
        this(runnable, notifyObject, false);
 
54
    /**
 
55
     * Create a RunnableTask object w/ synchronization,
 
56
     * ie. suitable for <code>invokeAndWait()</code>. 
 
57
     * 
 
58
     * @param runnable the user action
 
59
     * @param syncObject the synchronization object the caller shall wait for in case of <code>invokeAndWait()</code> 
 
60
     */
 
61
    public RunnableTask(Runnable runnable, Object syncObject) {
 
62
        this(runnable, syncObject, false);
50
63
    }
51
64
 
52
 
    public RunnableTask(Runnable runnable, Object notifyObject, boolean catchExceptions) {
 
65
    /**
 
66
     * Create a RunnableTask object w/ synchronization,
 
67
     * ie. suitable for <code>invokeAndWait()</code>. 
 
68
     * 
 
69
     * @param runnable the user action
 
70
     * @param syncObject the synchronization object the caller shall wait for in case of <code>invokeAndWai()t</code> 
 
71
     * @param catchExceptions if true, exception during <code>runnable</code> execution are catched, otherwise not.
 
72
     *                        Use {@link #getThrowable()} to determine whether an exception has been catched. 
 
73
     */
 
74
    public RunnableTask(Runnable runnable, Object syncObject, boolean catchExceptions) {
53
75
        this.runnable = runnable ;
54
 
        this.notifyObject = notifyObject ;
 
76
        this.syncObject = syncObject ;
55
77
        this.catchExceptions = catchExceptions ;
56
78
        ts0 = System.currentTimeMillis();
57
79
        ts1 = 0;
58
80
        ts2 = 0;
59
81
    }
60
82
 
 
83
    /** Return the user action */
61
84
    public Runnable getRunnable() {
62
85
        return runnable;
63
86
    }
64
87
 
65
88
    /** 
 
89
     * Return the synchronization object if any.
 
90
     * @see #RunnableTask(Runnable, Object, boolean) 
 
91
     */
 
92
    public Object getSyncObject() {
 
93
        return syncObject;
 
94
    }
 
95
    
 
96
    /** 
66
97
     * Attach a custom object to this task. 
67
98
     * Useful to piggybag further information, ie tag a task final. 
68
99
     */
70
101
        attachment = o;
71
102
    }
72
103
 
 
104
    /** 
 
105
     * Return the attachment object if any.
 
106
     * @see #setAttachment(Object) 
 
107
     */
73
108
    public Object getAttachment() {
74
109
        return attachment;
75
110
    }
76
111
 
77
112
    public void run() {
78
113
        ts1 = System.currentTimeMillis();
79
 
        if(null == notifyObject) {
 
114
        if(null == syncObject) {
80
115
            try {
81
116
                runnable.run();
82
117
            } catch (Throwable t) {
88
123
                ts2 = System.currentTimeMillis();
89
124
            }
90
125
        } else {
91
 
            synchronized (notifyObject) {
 
126
            synchronized (syncObject) {
92
127
                try {
93
128
                    runnable.run();
94
129
                } catch (Throwable t) {
98
133
                    }
99
134
                } finally {
100
135
                    ts2 = System.currentTimeMillis();
101
 
                    notifyObject.notifyAll();
 
136
                    syncObject.notifyAll();
102
137
                }
103
138
            }
104
139
        }
110
145
    public boolean isExecuted() { return 0 != ts2 ; }
111
146
 
112
147
    /**
113
 
     * @return A Throwable thrown while execution if any
 
148
     * @return True if invoking thread waits until done, 
 
149
     *         ie a <code>notifyObject</code> was passed, otherwise false;
 
150
     */
 
151
    public boolean hasWaiter() { return null != syncObject; }
 
152
 
 
153
    /**
 
154
     * @return A thrown exception while execution of the user action, if any and if catched
 
155
     * @see #RunnableTask(Runnable, Object, boolean)
114
156
     */
115
157
    public Throwable getThrowable() { return runnableException; }
116
158