01: /*
02: *
03: *
04: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26: package com.sun.midp.jsr82emul;
27:
28: /**
29: * Runs emulation request processing in a separate thread.
30: */
31: abstract public class RunnableProcessor implements Runnable {
32: /** Shows if processing can be started. */
33: protected boolean ready = false;
34: /** Shows if processing has been interrupted. */
35: protected boolean interrupted = false;
36: /** Shows if processing should be performed repeatedly. */
37: private boolean loop = false;
38: /** Thread that performs processing. */
39: private Thread thread = new Thread(this );
40:
41: /**
42: * Constructs an instance.
43: * @param loop flag to define if repeated processing required.
44: */
45: RunnableProcessor(boolean loop) {
46: this .loop = loop;
47: thread = new Thread(this );
48: thread.start();
49: }
50:
51: /** Constructs an instance for one time processing. */
52: RunnableProcessor() {
53: this (false);
54: }
55:
56: /**
57: * Implements <code>Runnable</code>.
58: * Accepts and opens connection to client
59: */
60: public void run() {
61: do {
62: synchronized (this ) {
63: if (!ready) {
64: try {
65: wait();
66: } catch (InterruptedException e) {
67: return;
68: }
69: }
70: ready = false;
71: }
72:
73: process();
74:
75: } while (loop);
76: }
77:
78: /** Interrupts processing thread. */
79: public void interrupt() {
80: interrupted = true;
81: loop = false;
82: thread.interrupt();
83: }
84:
85: /**
86: * Allows one processing procedure. No processing is performed prior
87: * to this method call. For a repeatedly working processor each
88: * iteration waits for <code>start()</code>.
89: */
90: public synchronized void start() {
91: ready = true;
92: notify();
93: }
94:
95: /** Processing procedure. */
96: abstract protected void process();
97: }
|