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

📄 testmanager.java

📁 一个简单的Java并发系统动态测试工具
💻 JAVA
字号:
/**********************************************************************

contest ver 1.0 
一个简单的并发系统动态测试工具
Copyright (C) 2003, 2004 王咏刚
	
作者对 contest 软件及其所有源代码授权如下:

允许任何个人、学术机构、公司无偿获得、修改、使用、重新发布 contest 软
件及其源代码,并可以按照有偿或者无偿的方式发行基于 contest 源代码的全
部或部分内容开发的软件产品,——但行使以上权利时,须遵守以下约定:

1、重新发布 contest 软件及其源代码时,不得隐去软件以及源代码中原有
   的版权信息和开发者标识。

2、发行基于 contest 源代码的全部或部分内容开发的软件产品时,必须在
   产品的显著位置标明以下字样:

   “本产品的一部分功能是基于王咏刚在 contest 软件中的工作完成的”

3、在正式出版物中引用 contest 的文档、源代码或注释内容的,应注明软
   件的原作者。

		王咏刚
		2004. 1

**********************************************************************/

package contest;

import java.util.Vector;

// 对并发系统进行同步测试的管理类
public class TestManager
{
	// 所有待测试线程
	private static Vector threads = new Vector();
	// 所有同步事件
	private static Vector events = new Vector();
	// 每个同步事件之间的间隔时间
	private static Vector spans = new Vector();

	// 测试管理类的惟一对象实例
	private static TestManager instance = new TestManager();

	// 获取测试管理类的对象实例
	public static TestManager getInstance()
	{
		return instance;
	}

	// 添加待测试线程,target是线程的对象实例,
	// name指定线程实例的名称
	public void addThread(Runnable target, String name)
	{
		Thread thread = new Thread(target, name);
		threads.add(thread);
	}

	// 添加同步事件,name指定事件名称,
	// span指明此事件与上一事件间的时间间隔,
	// 对于第一个添加的事件,span指明所有线程启动
	// 到该事件激活的事件间隔
	public void addEvent(String name, long span)
	{
		TestEvent event = new TestEvent(name);
		events.add(event);
		spans.add(new Long(span));
	}

	// 开始测试
	public void start()
	{
		// 启动所有待测试线程
		for (int i = 0; i < threads.size(); i++)
			( (Thread) threads.get(i)).start();
		// 根据预先设定的时间间隔,依次激活每个同步事件
		for (int i = 0; i < events.size(); i++)
		{
			try{Thread.sleep(((Long) spans.get(i)).longValue());}
			catch (InterruptedException e) {}
			((TestEvent)events.get(i)).setEvent();
		}
		// 等待所有线程执行完毕
		for (int i = 0; i < threads.size(); i++)
		{
			try{((Thread)threads.get(i)).join();}
			catch (Exception e) {}
		}
	}

	// 在同步事件数组中,根据事件名称寻找某个事件对象
	private static TestEvent findEvent(String eventName)
	{
		for (int i = 0; i < events.size(); i++)
		{
			TestEvent e = (TestEvent) events.get(i);
			if (e.getName().equals(eventName))
				return e;
		}
		return null;
	}

	// 等待某个事件timeout毫秒
	public static void waitFor(String eventName, long timeout)
	{
		TestEvent e = findEvent(eventName);
		if (e != null)
			e.waitFor(timeout);
	}

	// 等待某个事件
	public static void waitFor(String eventName)
	{
		waitFor(eventName, 0);
	}

	// 指定某个具体的线程实例等待某个事件timeout毫秒
	// threadName给出线程实例的名称
	public static void conditionalWaitFor(String threadName, String eventName,
										  long timeout)
	{
		Thread t = Thread.currentThread();
		if (t.getName().equals(threadName))
			waitFor(eventName, timeout);
	}

	// 指定某个具体的线程实例等待某个事件
	// threadName给出线程实例的名称
	public static void conditionalWaitFor(String threadName, String eventName)
	{
		conditionalWaitFor(threadName, eventName, 0);
	}
}

⌨️ 快捷键说明

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