source: java/main/src/main/java/com/framsticks/util/dispatching/Thread.java @ 97

Last change on this file since 97 was 97, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

  • add proper exception passing between communication sides:

if exception occur during handling client request, it is
automatically passed as comment to error response.

it may be used to snoop communication between peers

  • fix algorithm choosing text controls in GUI
  • allow GUI testing in virtual frame buffer (xvfb)

FEST had some problem with xvfb but workaround was found

supports tab-completion based on requests history

CHANGELOG:
Further improve handling of exceptions in GUI.

Add StatusBar? implementing ExceptionResultHandler?.

Make completion processing asynchronous.

Minor changes.

Improve completion in console.

Improve history in InteractiveConsole?.

First working version of DirectConsole?.

Minor changes.

Make Connection.address non final.

It is more suitable to use in configuration.

Improvement of consoles.

Improve PopupMenu? and closing of FrameJoinable?.

Fix BrowserTest?.

Found bug with FEST running under xvfb.

JButtonFixture.click() is not working under xvfb.
GuiTest? has wrapper which uses JButton.doClick() directly.

Store CompositeParam? param in TreeNode?.

Simplify ClientSideManagedConnection? connecting.

There is now connectedFunctor needed, ApplicationRequests? can be
send right after creation. They are buffered until the version
and features are negotiated.

Narow down interface of ClientSideManagedConnection?.

Allow that connection specialization send only
ApplicationRequests?.

Improve policy of text control choosing.

Change name of Genotype in BrowserTest?.

Make BrowserTest? change name of Genotype.

Minor change.

First working draft of TrackConsole?.

Simplify Consoles.

More improvements with gui joinables.

Unify initialization on gui joinables.

More rework of Frame based entities.

Refactorize structure of JFrames based entities.

Extract GuiTest? from BrowserBaseTest?.

Reorganize Console classes structure.

Add Collection view to JoinableCollection?.

Configure timeout in testing.

Minor changes.

Rework connections hierarchy.

Add Mode to the get operation.

Make get and set in Tree take PrimitiveParam?.

Unify naming of operations.

Make RunAt? use the given ExceptionHandler?.

It wraps the virtual runAt() method call with
try-catch passing exception to handler.

Force RunAt? to include ExceptionHandler?.

Improve ClientAtServer?.

Minor change.

Another sweep with FindBugs?.

Rename Instance to Tree.

Minor changes.

Minor changes.

Further clarify semantics of Futures.

Add FutureHandler?.

FutureHandler? is refinement of Future, that proxifies
exception handling to ExceptionResultHandler? given
at construction time.

Remove StateFunctor? (use Future<Void> instead).

Make Connection use Future<Void>.

Unparametrize *ResponseFuture?.

Remove StateCallback? not needed anymore.

Distinguish between sides of ResponseFuture?.

Base ResponseCallback? on Future (now ResponseFuture?).

Make asynchronous store taking Future for flags.

Implement storeValue in ObjectInstance?.

File size: 3.3 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.log4j.Logger;
4
5import java.util.LinkedList;
6import java.util.ListIterator;
7
8
9import com.framsticks.params.annotations.ParamAnnotation;
10import com.framsticks.util.dispatching.RunAt;
11
12/**
13 * @author Piotr Sniegowski
14 */
15public class Thread<C> extends AbstractJoinable implements Dispatcher<C> {
16
17        private static final Logger log = Logger.getLogger(Thread.class);
18
19        protected final java.lang.Thread thread;
20
21        private final LinkedList<Task<? extends C>> queue = new LinkedList<>();
22
23        public Thread() {
24                thread = new java.lang.Thread(new java.lang.Runnable() {
25                        @Override
26                        public void run() {
27                                Thread.this.routine();
28                        }
29                });
30        }
31
32
33        public Thread(java.lang.Thread thread) {
34                this.thread = thread;
35        }
36
37        @Override
38        protected void joinableStart() {
39                thread.start();
40        }
41
42        @Override
43        public final boolean isActive() {
44                return thread.equals(java.lang.Thread.currentThread());
45        }
46
47        protected void routine() {
48                log.debug("starting thread " + this);
49                assert getMonitor() != null;
50                ExceptionHandler exceptionHandler = getMonitor().getTaskExceptionHandler();
51                while (!java.lang.Thread.interrupted()) {
52                        Task<? extends C> task;
53                        synchronized (queue) {
54                                if (queue.isEmpty()) {
55                                        try {
56                                                queue.wait();
57                                        } catch (InterruptedException ignored) {
58                                                break;
59                                        }
60                                        continue;
61                                }
62                                task = queue.peekFirst();
63                                assert task != null;
64                                if (task.moment > System.currentTimeMillis()) {
65                                        try {
66                                                queue.wait(task.moment - System.currentTimeMillis());
67                                        } catch (InterruptedException ignored) {
68                                                continue;
69                                        }
70                                        continue;
71                                }
72                                queue.pollFirst();
73                        }
74                        try {
75                                task.runAt();
76                        } catch (Exception e) {
77                                if (exceptionHandler != null) {
78                                        if (exceptionHandler.handle(this, e)) {
79                                                continue;
80                                        }
81                                }
82                                log.error("error in thread: ", e);
83                        }
84                }
85                log.debug("finishing thread " + this);
86                finish();
87        }
88
89        protected void enqueueTask(Task<? extends C> task) {
90                synchronized (queue) {
91                        ListIterator<Task<? extends C>> i = queue.listIterator();
92                        while (i.hasNext()) {
93                                Task<? extends C> t = i.next();
94                                if (t.getMoment() > task.getMoment()) {
95                                        i.previous();
96                                        i.add(task);
97                                        task = null;
98                                        break;
99                                }
100                        }
101                        if (task != null) {
102                                queue.add(task);
103                        }
104
105                        /*
106                        Iterator<Task> j = queue.iterator();
107                        Task prev = null;
108                        while (j.hasNext()) {
109                                Task next = j.next();
110                                assert (prev == null) || prev.getMoment() <= next.getMoment();
111                                prev = next;
112                        }
113                        */
114                        queue.notify();
115                }
116        }
117
118        @Override
119        public void dispatch(final RunAt<? extends C> runnable) {
120                if (!(runnable instanceof Task)) {
121                        enqueueTask(new Task<C>(runnable) {
122                                @Override
123                                protected void runAt() {
124                                        runnable.runAt();
125                                }
126                        });
127                        return;
128                }
129                enqueueTask((Task<? extends C>) runnable);
130        }
131
132        @Override
133        protected void joinableInterrupt() {
134                thread.interrupt();
135        }
136
137        @Override
138        protected void joinableJoin() throws InterruptedException {
139                thread.join(500);
140                log.debug("joined " + this);
141        }
142
143        @ParamAnnotation
144        public void setName(String name) {
145                thread.setName(name);
146        }
147
148        @ParamAnnotation
149        public String getName() {
150                return thread.getName();
151        }
152
153        public static boolean interrupted() {
154                return java.lang.Thread.interrupted();
155        }
156
157        @Override
158        public String toString() {
159                return getName();
160        }
161
162        @Override
163        protected void joinableFinish() {
164        }
165
166}
Note: See TracBrowser for help on using the repository browser.