欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

多线程模拟光纤扫描过程

程序员文章站 2022-05-21 09:32:01
...

此篇文章主要是练习一下多线程,有业务需求,有多根光纤需要扫描,要求使用多线程.

代码都包含注释,不详细说了,直接贴代码了.

 

    ScanResult.java

 

package com.xinguo.myspring.util;

/**
 * 扫描结果封装成的对象
* @Title: ScanResult 
* @Package com.xinguo.myspring.util 
* @Description: 
* @author butingyou@gtstart.com.cn
* @date Mar 19, 2014 1:16:41 PM 
* @version V1.0
 */
public class ScanResult {
	private String dataFileName;//扫描完成生成的数据文件
	private String fiberId;//扫描的光纤ID

	public ScanResult(String dataFileName, String fiberId) {
		super();
		this.dataFileName = dataFileName;
		this.fiberId = fiberId;
	}

	public String getDataFileName() {
		return dataFileName;
	}

	public String getFiberId() {
		return fiberId;
	}

	public void setDataFileName(String dataFileName) {
		this.dataFileName = dataFileName;
	}

	public void setFiberId(String fiberId) {
		this.fiberId = fiberId;
	}

}

 

   FiberScanServiceImpl.java

package com.xinguo.myspring.util;

/**
 * 扫描的最底层类,此类调用真实扫描方法
* @Title: FiberScanServiceImpl 
* @Package com.xinguo.myspring.util 
* @Description: 现实中可能用到通信,获取消息等
* @author butingyou@gtstart.com.cn
* @date Mar 19, 2014 1:19:52 PM 
* @version V1.0
 */
public class FiberScanServiceImpl {

	/**
	 * 扫描
	* @Title: scan 
	* @Description:
	* @param @param fiberId
	* @param @return   
	* @return String 
	* @throws
	 */
	public String scan(String fiberId) {
		String dataFileName = "";
		try {
			System.out.println(Thread.currentThread().getName() + ",扫描光纤[" + fiberId + "]开始");
			//模拟扫描消耗的时间
			Thread.sleep(10000);
			System.out.println(Thread.currentThread().getName() + ",扫描光纤[" + fiberId + "]结束");
			//模拟文件格式
			dataFileName = "192.168.4.12_20140319_" + fiberId + ".sorda";
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return dataFileName;
	}
}

 

   Scanner.java

   这个类稍微说一下,实现了Callable接口,实现了call()方法,这个方法有返回值,是Java 5以后新增的.想再了解些请浏览http://blog.csdn.net/ghsau/article/details/7451464

package com.xinguo.myspring.util;

import java.util.concurrent.Callable;

/**
 * 实现Callable接口的线程类
 * 
 * @Title: Scanner
 * @Package com.xinguo.myspring.util
 * @Description: 实现Callable接口,返回扫描结果对象
 * @author butingyou@gtstart.com.cn
 * @date Mar 19, 2014 1:26:34 PM
 * @version V1.0
 */
public class Scanner implements Callable<ScanResult> {
	private String fiberName;
	private FiberScanServiceImpl scanServiceImpl = new FiberScanServiceImpl();

	public Scanner(String fiberName) {
		super();
		this.fiberName = fiberName;
	}

	public ScanResult call() throws Exception {
		String dataFile = scanServiceImpl.scan(fiberName);
		ScanResult scanResult = new ScanResult(dataFile, fiberName);
		return scanResult;
	}

	public String getFiberName() {
		return fiberName;
	}

	public void setFiberName(String fiberName) {
		this.fiberName = fiberName;
	}

}

  

   MutilThreadTest.java

   测试类.

package com.xinguo.myspring.util;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 多线程测试
 * 
 * @Title: MutilThreadTest
 * @Package com.xinguo.myspring.util
 * @Description: 模拟光纤扫描过程,根据光纤个数创建多个线程执行扫描工作
 * @author butingyou@gtstart.com.cn
 * @date Mar 19, 2014 11:19:28 AM
 * @version V1.0
 */
public class MutilThreadTest {
	// 光纤条数
	private static String[] fiber = { "F1", "F2", "F3", "F4" };

	public static void main(String[] args) {
		try {
			//初始化指定个数的线程池
			ExecutorService pool = Executors.newFixedThreadPool(fiber.length);
			for (int i = 0; i < fiber.length; i++) {
				Callable<ScanResult> callable = new Scanner(fiber[i]);
				//用Future<V>接收线程返回的扫描结果对象
				Future<ScanResult> future = pool.submit(callable);
				ScanResult scanResult = future.get();
				System.out.println("扫描光纤" + fiber[i] + "得到的数据文件是:" + scanResult.getDataFileName());
			}
			System.out.println(Thread.currentThread().getName() + ",end............,关闭线程池.");
			//关闭线程池
			pool.shutdown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

}

 

   测试结果:

  

pool-1-thread-1,扫描光纤[F1]开始
pool-1-thread-1,扫描光纤[F1]结束
扫描光纤F1得到的数据文件是:192.168.4.12_20140319_F1.sorda
pool-1-thread-2,扫描光纤[F2]开始
pool-1-thread-2,扫描光纤[F2]结束
扫描光纤F2得到的数据文件是:192.168.4.12_20140319_F2.sorda
pool-1-thread-3,扫描光纤[F3]开始
pool-1-thread-3,扫描光纤[F3]结束
扫描光纤F3得到的数据文件是:192.168.4.12_20140319_F3.sorda
pool-1-thread-4,扫描光纤[F4]开始
pool-1-thread-4,扫描光纤[F4]结束
扫描光纤F4得到的数据文件是:192.168.4.12_20140319_F4.sorda
main,end............,关闭线程池.