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

📄 threadpool2test.java

📁 java编写的OCR软件
💻 JAVA
字号:
package de.spieleck.util;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Test the ThreadPool on faster processes with heavy synchronization.
 * Very similar to {@link TestThreadPool}.
 */
public class ThreadPool2Test
    extends TestCase
{
    protected ThreadPool tp;
    protected TaskQueue tq;

    protected int count;
    protected long sum;

    public void setUp()
    {
        tq = new TaskQueue();
        tp = new ThreadPool(5, tq);
    }

    public void tearDown()
    {
        tq = null;
        tp = null;
    }

    /** large jobs: do it hundred times. */
    public void testCountSum500_100()
        throws Exception
    {
        testCountSum(500,100);
    }

    public void testCountSum1000_100()
        throws Exception
    {
        testCountSum(1000, 100);
    }

    public void testCountSum2000_100()
        throws Exception
    {
        testCountSum(2000, 100);
    }

    public void testCountSum4000_100()
        throws Exception
    {
        testCountSum(4000, 100);
    }

    public void testCountSum8000_100()
        throws Exception
    {
        testCountSum(8000, 100);
    }

    public void testCountSum500_200()
        throws Exception
    {
        testCountSum(500, 200);
    }

    public void testCountSum500_10()
        throws Exception
    {
        testCountSum(500,10);
    }

    public void testCountSum1000_10()
        throws Exception
    {
        testCountSum(1000, 10);
    }

    /** medium jobs: do it ten times. */
    public void testCountSum2000_10()
        throws Exception
    {
        testCountSum(2000, 10);
    }

    public void testCountSum4000_10()
        throws Exception
    {
        testCountSum(4000, 10);
    }

    public void testCountSum8000_10()
        throws Exception
    {
        testCountSum(8000, 10);
    }

    /** minijobs: do it once. */
    public void testCountSum500_1()
        throws Exception
    {
        testCountSum(500,1);
    }

    public void testCountSum1000_1()
        throws Exception
    {
        testCountSum(1000, 1);
    }

    public void testCountSum2000_1()
        throws Exception
    {
        testCountSum(2000, 1);
    }

    public void testCountSum4000_1()
        throws Exception
    {
        testCountSum(4000, 1);
    }

    public void testCountSum8000_1()
        throws Exception
    {
        testCountSum(8000, 1);
    }


    protected void testCountSum(int tasks, int loops)
    {
        count = 0;
        sum = 0L;
        for(int i = 0; i < tasks; i++ )
            tq.addTask(new Testrun(loops));
        tp.join();
        assertTrue("Count is wrong: "+count, count == tasks * loops);
        long tsum = tasks * loops * (loops - 1) / 2;
        assertEquals("Sum is wrong: "+sum, tsum, sum);
    }

    public void incCount(int i)
    {
        synchronized(this)
        {
            count++;
            sum += i;
        }
    }

    public class Testrun
        implements Runnable
    {
        private int loops;

        public Testrun(int loops)
        {
            this.loops = loops;
        }

        public void run()
        {
            for (int i = 0; i< loops; i++ )
            {
                incCount(i);
            }
        }
    }

    public static Test suite() {
        TestSuite suite = new TestSuite(ThreadPool2Test.class);
        return suite;
    }
    
    public static void main(java.lang.String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}
//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library 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
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

⌨️ 快捷键说明

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