⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadpooltest.java

📁 利用SyncML开发客户端程序的中间件
💻 JAVA
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.funambol.util;

import jmunit.framework.cldc10.AssertionFailedException;
import jmunit.framework.cldc10.TestCase;


/**
 * Testing the ThreadPool implementation.
 */
public class ThreadPoolTest extends TestCase {
    
    private int TEST_NUM_THREADS = 10;
    private int THREAD_MAX_SIZE = 5;
    private ThreadPool threadPool;
    
    /** Creates a new instance of ThreadPoolTest */
    public ThreadPoolTest() {
        super(1, "ThreadPoolTest");
        Log.setLogLevel(Log.TRACE);
        threadPool = new ThreadPool(THREAD_MAX_SIZE);
    }
    
    public void test(int i) throws Throwable {
        switch(i) {
            case 0:
                testThreadPool();
                break;
            default:
                break;
        }
    }
    
    /**
     * Try to start concurrently 10 threads while the max
     * number of threads of the pool (queue) has been configured to 5.
     * As soon as a thread ends a new thread starts from the queue.
     */
    public void testThreadPool() throws AssertionFailedException {
        
        for (int i=0; i<TEST_NUM_THREADS; i++) {
            Log.debug("Request starting thread n. " + i);
            threadPool.startThread(new MyRunnableClass());
        }
        
        // assertion not really useful due to threads usage
        assertTrue(true);
    }
    
    /**
     * Example of runnable class to be added to the queue
     * and later started from the queue
     */
    public class MyRunnableClass extends Thread {
        private long waitPeriod = 10000;
        
        MyRunnableClass() {
        }
        
        /**
         * Simple work for this sample thread: just waiting 10 seconds
         */
        public void run() {
            Log.debug("Thread " + this.currentThread() + " runs");
            try {
                Thread.sleep(waitPeriod);
            } catch(Exception e) {
                
            }
            Log.debug("Thread " + this.currentThread() + " ends");
        }
        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -