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

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

HIGHLIGHTS:

  • simplification of entities management model
  • cleanup around params (improve hierarchy)
  • migrate from JUnit to TestNG
  • introduce FEST to automatically test GUI
  • improve slider control
  • loosen synchronization between gui tree and backend representation
  • and many other bug fixes

NOTICE:

  • a great many of lines is changed only because of substituting spaces with tabs

CHANGELOG (oldest changes at the bottom):

Some cleaning after fix found.

Fix bug with tree.

More changes with TreeNodes?.

Finally fix issue with tree.

Improve gui tree management.

Decouple update of values from fetch request in gui.

Minor changes.

Minor changes.

Minor change.

Change Path construction wording.

More fixes to SliderControl?.

Fix SliderControl?.

Fix SliderControl?.

Minor improvement.

Several changes.

Make NumberParam? a generic class.

Add robot to the gui test.

Setup common testing logging configuration.

Remove Parameters class.

Remove entityOwner from Parameters.

Move name out from Parameters class.

Move configuration to after the construction.

Simplify observers and endpoints.

Remove superfluous configureEntity overrides.

Add dependency on fest-swing-testng.

Use FEST for final print test.

Use FEST for more concise and readable assertions.

Divide test of F0Parser into multiple methods.

Migrate to TestNG

Minor change.

Change convention from LOGGER to log.

Fix reporting of errors during controls filling.

Bound maximal height of SliderControl?.

Minor improvements.

Improve tooltips for controls.

Also use Delimeted in more places.

Move static control utilities to Gui.

Rename package gui.components to controls.

Some cleaning in controls.

Improve Param classes placing.

Move ValueParam?, PrimitiveParam? and CompositeParam? one package up.

Improve ParamBuilder?.

Move getDef to ValueParam? and PrimitiveParam?.

Move getMax and getDef to ValueParam?.

Move getMin to ValueParam?.

Upgrade to laters apache commons versions.

Use filterInstanceof extensively.

Add instanceof filters.

Make ValueParam? in many places of Param.

Place assertions about ValueParam?.

Add ValueParam?

Rename ValueParam? to PrimitiveParam?

Minor changes.

Several improvements to params types.

Add NumberParam?.

Add TextControl? component.

Add .swp files to .gitignore

Greatly improved slider component.

Some improvements.

Make Param.reassign return also a state.

Add IterableIterator?.

Several changes.

  • Move util classes to better packages.
  • Remove warnings from eclim.

Several improvements.

Fix bug with BooleanParam?.

Some experiments with visualization.

Another fix to panel management.

Improve panel management.

Some refactorization around panels.

Add root class for panel.

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