📄 threadpooltest.java
字号:
package de.spieleck.util;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test the ThreadPool on slow processes.
*/
public class ThreadPoolTest
extends TestCase
{
private static int seed = 42;
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 testCountSum10_100()
throws Exception
{
testCountSum(10,100);
}
public void testCountSum100_100()
throws Exception
{
testCountSum(100, 100);
}
public void testCountSum200_100()
throws Exception
{
testCountSum(200, 100);
}
public void testCountSum400_100()
throws Exception
{
testCountSum(400, 100);
}
public void testCountSum800_100()
throws Exception
{
testCountSum(800, 100);
}
public void testCountSum100_200()
throws Exception
{
testCountSum(100, 200);
}
public void testCountSum10_10()
throws Exception
{
testCountSum(10,10);
}
public void testCountSum100_10()
throws Exception
{
testCountSum(100, 10);
}
/** medium jobs: do it ten times. */
public void testCountSum200_10()
throws Exception
{
testCountSum(200, 10);
}
public void testCountSum400_10()
throws Exception
{
testCountSum(400, 10);
}
public void testCountSum800_10()
throws Exception
{
testCountSum(800, 10);
}
/** minijobs: do it once. */
public void testCountSum10_1()
throws Exception
{
testCountSum(10,1);
}
public void testCountSum100_1()
throws Exception
{
testCountSum(100, 1);
}
public void testCountSum200_1()
throws Exception
{
testCountSum(200, 1);
}
public void testCountSum400_1()
throws Exception
{
testCountSum(400, 1);
}
public void testCountSum800_1()
throws Exception
{
testCountSum(800, 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)
{
count++;
sum += i;
}
// Another test (in the same class...) do some sync and String thing!
private String res;
public void testString()
{
res = "";
for (int j = 0; j < 10; j++)
tq.addTask(new Testrun2(Integer.toString(j),1000));
tp.join();
assertTrue("Length of String", res.length() == 10000);
}
public synchronized void append(String s)
{
res += s;
}
public class Testrun
implements Runnable
{
// Poor mans random sequence
private int rnd;
private int loops;
public Testrun(int loops)
{
rnd = seed++;
this.loops = loops;
}
public void run()
{
for (int i = 0; i< loops; i++ )
{
rnd = 5 * rnd + 1;
ThreadUtil.sleep(0, rnd & 768);
incCount(i);
}
}
}
public class Testrun2
implements Runnable
{
private String s;
private int loops;
public Testrun2(String s, int loops)
{
this.s = s;
this.loops = loops;
}
public void run()
{
for(int i = 0; i < loops; i++)
{
append(s);
}
}
}
public static Test suite() {
TestSuite suite = new TestSuite(ThreadPoolTest.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 + -