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

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

HIGHLIGHTS:

  • upgrade to Java 7
    • use try-multi-catch clauses
    • use try-with-resources were appropriate
  • configure FindBugs? (use mvn site and then navigate in browser to the report)
    • remove most bugs found
  • parametrize Dispatching environment (Dispatcher, RunAt?) to enforce more control on the place of closures actual call

CHANGELOG:
Rework FavouritesXMLFactory.

FindBugs?. Thread start.

FindBugs?. Minor change.

FindBugs?. Iterate over entrySet.

FindBugs?. Various.

FindBug?.

FindBug?. Encoding.

FindBug?. Final fields.

FindBug?.

Remove synchronization bug in ClientConnection?.

Experiments with findbugs.

Finish parametrization.

Make RunAt? an abstract class.

More changes in parametrization.

More changes in parametrizing dispatching.

Several changes to parametrize tasks.

Rename Runnable to RunAt?.

Add specific framsticks Runnable.

Add JSR305 (annotations).

Add findbugs reporting.

More improvements to ParamBuilder? wording.

Make FramsClass? accept also ParamBuilder?.

Change wording of ParamBuilder?.

Change wording of Request creation.

Use Java 7 exception catch syntax.

Add ScopeEnd? class.

Upgrade to Java 7.

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