01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Pavel Dolgov
19: * @version $Revision: 1.2 $
20: */package org.apache.harmony.applet;
21:
22: import java.util.List;
23: import java.util.Collections;
24: import java.util.LinkedList;
25:
26: /**
27: * Thread that initializes applet and performs commands from the host application
28: */
29: final class AppletThread extends Thread {
30: private final Object monitor = new Object();
31: private final List<Command> commandQueue = Collections
32: .synchronizedList(new LinkedList<Command>());
33:
34: private boolean doExit;
35:
36: AppletThread(Proxy proxy) {
37: super (proxy.docSlice.codeBase.threadGroup, "Applet-"
38: + proxy.params.className);
39: setContextClassLoader(proxy.docSlice.codeBase.classLoader);
40: }
41:
42: @Override
43: public void run() {
44:
45: while (true) {
46:
47: while (!commandQueue.isEmpty()) {
48: Command command = commandQueue.remove(0);
49: command.run();
50: }
51:
52: synchronized (monitor) {
53: if (doExit) {
54: return;
55: }
56: try {
57: monitor.wait();
58: if (doExit) {
59: return;
60: }
61: } catch (InterruptedException e) {
62: // set the interrupt state
63: interrupt();
64: // the thread was interrupted, so we end it
65: return;
66: }
67: }
68: }
69: }
70:
71: void postCommand(Command command) {
72: commandQueue.add(command);
73:
74: if (Thread.currentThread() != this ) {
75: synchronized (monitor) {
76: monitor.notifyAll();
77: }
78: }
79: }
80:
81: void exit() {
82: if (Thread.currentThread() != this ) {
83: throw new InternalError(
84: "Attempt to stop applet main thread outside of that thread");
85: }
86: synchronized (monitor) {
87: doExit = true;
88: monitor.notifyAll();
89: }
90: }
91:
92: }
|