1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package com.sun.star.lib.uno.environments.remote; 25 26 import java.util.Collection; 27 import java.util.HashMap; 28 import java.util.WeakHashMap; 29 30 final class JavaThreadPoolFactory { JavaThreadPoolFactory()31 public JavaThreadPoolFactory() {} 32 createThreadPool()33 public IThreadPool createThreadPool() { 34 return new JavaThreadPool(this); 35 } 36 addJobQueue(JobQueue jobQueue)37 public void addJobQueue(JobQueue jobQueue) { 38 synchronized (jobQueues) { 39 jobQueues.put(jobQueue.getThreadId(), jobQueue); 40 } 41 } 42 removeJobQueue(JobQueue jobQueue)43 public void removeJobQueue(JobQueue jobQueue) { 44 synchronized (jobQueues) { 45 jobQueues.remove(jobQueue.getThreadId()); 46 } 47 } 48 getJobQueue(ThreadId threadId)49 public JobQueue getJobQueue(ThreadId threadId) { 50 synchronized (jobQueues) { 51 return (JobQueue) jobQueues.get(threadId); 52 } 53 } 54 getAsyncJobQueue(ThreadId threadId)55 public JobQueue getAsyncJobQueue(ThreadId threadId) { 56 JobQueue q = getJobQueue(threadId); 57 return q == null ? null : q._async_jobQueue; 58 } 59 dispose(Object disposeId, Throwable throwable)60 public void dispose(Object disposeId, Throwable throwable) { 61 JobQueue[] qs; 62 synchronized (jobQueues) { 63 Collection c = jobQueues.values(); 64 qs = (JobQueue[]) c.toArray(new JobQueue[c.size()]); 65 } 66 for (int i = 0; i < qs.length; ++i) { 67 qs[i].dispose(disposeId, throwable); 68 } 69 } 70 getThreadId()71 public static ThreadId getThreadId() { 72 Thread t = Thread.currentThread(); 73 if (t instanceof JobQueue.JobDispatcher) { 74 return ((JobQueue.JobDispatcher) t).getThreadId(); 75 } else { 76 ThreadId id; 77 synchronized (threadIdMap) { 78 id = (ThreadId) threadIdMap.get(t); 79 if (id == null) { 80 id = ThreadId.createFresh(); 81 threadIdMap.put(t, id); 82 } 83 } 84 return id; 85 } 86 } 87 88 private static final WeakHashMap threadIdMap = new WeakHashMap(); 89 private final HashMap jobQueues = new HashMap(); 90 } 91