Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 043 | Call 'wait ()' only inside a "while" loop | 2 | 115:38 |
Java Inspector 083 | Do not use printStackTrace() for exception logging. | 2 | 72:54 |
Java Inspector 089 | Undocumented method | 2 | 43:25 |
Java Inspector 089 | Undocumented method | 2 | 83:13 |
Java Inspector 089 | Undocumented parameter task | 2 | 106:9 |
Java Inspector 089 | Undocumented method | 2 | 122:9 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 39:41 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 95:53 |
1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.util.LinkedList;
26
27/**
28 * Processes tasks from differen threads in a single thread.
29 * @author Pavel Vlasov
30 * @version $Revision: 1.6 $
31 */
32public class SerialProcessor {
33
34 private LinkedList tasks=new LinkedList();
35
36 {
37 new Thread() {
38 {
39 setName("Task processing thread");
40 start();
41 }
42
43 public void run() {
44 try {
45 while (true) {
46 Runnable task;
47 synchronized (tasks) {
48 while (tasks.isEmpty()) {
49 try {
50 tasks.wait();
51 } catch (InterruptedException e) {
52 return;
53 }
54 }
55
56 task=(Runnable) tasks.removeFirst();
57 }
58
59 if (task==null) {
60 return;
61 }
62
63 try {
64 task.run();
65 } finally {
66 synchronized (task) {
67 task.notifyAll();
68 }
69 }
70 }
71 } catch (Exception e) {
72 e.printStackTrace();
73 } finally {
74 synchronized (tasks) {
75 terminated=true;
76 tasks.notifyAll();
77 }
78 }
79 }
80 };
81
82 Runtime.getRuntime().addShutdownHook(new Thread() {
83 public void run() {
84 synchronized (tasks) {
85 tasks.add(null);
86 tasks.notifyAll();
87 }
88 }
89 });
90 }
91
92 private void checkTerminated() {
93 synchronized (tasks) {
94 if (terminated) {
95 throw new IllegalStateException("Task processing thread was terminated prematurely");
96 }
97 }
98 }
99
100 private boolean terminated;
101
102 /**
103 * Executes task in a separate thread
104 * @throws InterruptedException
105 */
106 public void execute(Runnable task) throws InterruptedException {
107 if (task!=null) {
108 synchronized (tasks) {
109 checkTerminated();
110 tasks.add(task);
111 tasks.notifyAll();
112 }
113
114 synchronized (task) {
115 task.wait();
116 }
117 }
118 }
119
120 private static SerialProcessor defaultInstance;
121
122 public static SerialProcessor getDefaultInstance() {
123 synchronized (SerialProcessor.class) {
124 if (defaultInstance==null) {
125 defaultInstance=new SerialProcessor();
126 }
127 }
128 return defaultInstance;
129 }
130}
131