怎样在线程外部控制线程运行

一个在外部控制线程运行的一个论证实例。标准的java因为安全问题取消了stop等控制方法,我想看看能不能自己把它们安全地实现。此代码已经考虑的内容如下:

  • java有堵塞的功能,所以通过一个类对象的同步锁 在线程的运行中 堵塞或解堵塞 来实现 暂停或继续的功能;

  • 因为java线程不允许直接关闭线程,这个实验代码中也考虑了这一点,所以停止线程的实现方法是 在线程外部先中断线程,然后在 线程运行内部 定义 处理怎样稳妥的终止程序

  • 也考虑到了 多线程中的 运行对象的 数据 共享 和 同步的问题,为了方便管理多个线程的运行 设置了一个列表管理器

  • 这个实验是用 多个 抽象的控制对象 与 一个可控制运行对象 结合实现,改变运行对象的 具体功能代码 既可以改变最终功能,但是 对于控制对象的控制方法和效果 还是一样的,只是在定义运行对象时 需要考虑如何利用 lock 来卡住 正在独立运行的 线程。

因为是刚刚开始学习java的多线程的内容,所以可能有一些地方的设置考虑不周,可能还可以做一些微调。也保留了一些想到的 可能可以 调整和修改 地方的 注释,移除他们并不会影响实验代码的运行~

/**
 * @author Hanbinbin
 */
package myTest;

import java.io.IOException;
import java.util.ArrayList;

interface ILockObject{
	/**
	 * 开始
	 */
	void start();
	/**
	 * 暂停
	 */
	void pause();
	/**
	 * 暂停多久
	 */
	void pause(int delay);
	/**
	 * 停止
	 */
	void stop();
	
	/**
	 * 测试卡
	 * @throws InterruptedException
	 */
	void lockTest() throws InterruptedException;
	
}
/**
 * 必须自己定义 存储测试卡的对象,在何处测试卡;怎样处理 卡中的 异常中断
 * @author Hanbinbin
 *
 */
interface IFunObject extends Runnable{
	/**
	 * 设置测试卡,备暂停使用
	 * @param lock
	 */
	void setLock(ILockObject lock);
	/**
	 * 创建运行线程,以自定义线程属性
	 * @param threadRunnable
	 * @return
	 */
	Thread createThread(Runnable threadRunnable);
}




/**
*抽象的控制对象
*/
class LockObj implements ILockObject{

	private Thread thread;
	private FunObject runnable;
	private boolean isRun = false;
	
	
	private int timeLong = 0;
	
	LockObj(FunObject runnable){
		
		this.runnable = runnable;
		this.runnable.setLock(this);
	}
	@Override
	public void start() {
		//synchronized(this) {	
		System.out.println("start.");
		
			//if(isRun == false)
			{				
				isRun = true;
				
				//处理启动
				if(thread == null){
					synchronized(runnable) {
						thread = runnable.createThread(runnable);							
						if(thread != null){
							thread.start();							
						}
						runnable.notifyAll();
					}
				}
				else if(thread.isInterrupted()){
					
				}//else{
					//处理解除暂停
					synchronized (this) {			
						//this.notify();
						this.notifyAll();
					}
				//}
			
			}
		//}
			
	}

	@Override
	public void pause() {
		synchronized (this) 
		{
			System.out.println("pause.");
			isRun = false;	
		}
	}
	
	@Override
	public void pause(int timeLong) {
		synchronized (this) 
		{
			System.out.println("pause."+timeLong);
			if(timeLong>0){
				this.timeLong = timeLong;
				isRun = false;
				
			}
		}
		
	}
	@Override
	public void stop() {
		synchronized (this) {
			System.out.println("stop.");
			
			isRun = false;		
		
			if(this.thread != null && this.thread.isAlive()){
				this.notify();
				this.thread.interrupt();
				this.thread = null;
			}
		}
		
	}
	
	@Override
	public  void lockTest() throws InterruptedException{
			
					
			if(isRun == false)			
			{
				System.out.println("test lock.");//+thread.getName()				
				synchronized(this) { 
					if(this.timeLong==0){
						this.wait();
					}else if(this.timeLong>0){
						this.wait(timeLong);
						
						this.timeLong = 0;
						isRun = true;					
					}
				}				
			}else{
				System.out.println("test lock."+thread.getName());//
			}
			
	}
	
}
/**
*抽象的控制对象
*/
class LockList implements ILockObject{
	
	private ArrayList<ILockObject> lockObjects = new ArrayList<ILockObject>(); 
	public void add(ILockObject lockObject) {
		lockObjects.add(lockObject);
	}

	@Override
	public void start() {
		for (int i = 0; i < lockObjects.size(); i++) {
			ILockObject object = lockObjects.get(i);
			object.start();			
		}
		
	}

	@Override
	public void pause() {
		for (int i = 0; i < lockObjects.size(); i++) {
			ILockObject object = lockObjects.get(i);
			object.pause();
		}
		
	}

	@Override
	public void pause(int delay) {
		for (int i = 0; i < lockObjects.size(); i++) {
			ILockObject object = lockObjects.get(i);
			object.pause(delay);
		}
		
	}

	@Override
	public void stop() {
		for (int i = 0; i < lockObjects.size(); i++) {
			ILockObject object = lockObjects.get(i);
			object.stop();
		}
		
	}

	@Override
	public void lockTest() throws InterruptedException {		
		
	}
	
}

/**可控制运行对象*/
class FunObject implements IFunObject {

	private ILockObject lock;
	private volatile int count = 0;
	
	//@Override
	public void setLock(ILockObject lock){
		this.lock = lock;
	}
	@Override
	public  void run() {
		while(true){
			try {	
				if(this.lock != null){
					this.lock.lockTest();
				}						
				Thread.sleep(10);
				synchronized (lock) {
					System.out.println("run."+ ++count);//thread.getName()+","+
				}									
			} catch (InterruptedException e) {
				
				System.out.println("end,count:"+ count);
				return;
			}
		}
	}
	
	//Thread thread ;
	@Override
	public synchronized Thread createThread(Runnable threadRunnable) {
		Thread thread = new Thread(threadRunnable);
		thread.setPriority(4);
		thread.setDaemon(true);
		return thread;
	}
	
}
/**主程序*/
public class TestThreadControl {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		FunObject funObject = new FunObject();
		//LockObj lockObj = new LockObj(funObject)
		LockList list = new LockList();
		for (int i = 0; i < 30; i++) {
			list.add(new LockObj(funObject));
		}
		
		try {
		        //监视输入一个字符,回车完成输入
			int input = System.in.read();
			
			while(true){
				System.out.println("input:"+input);
				switch (input) {
				case 'S':
					//lockObj.start();
					list.start();
					
					break;
				case 'W':
					//lockObj.pause(5000);
					list.pause(5000);
					
					break;
				case 'P':
					//lockObj.pause();
					list.pause();
					
					break;
				case 'T':
					//lockObj.stop();
					list.stop();
					
					break;	
				case 'E':
					//lockObj.stop();
					list.stop();
					
					return;
				default:
					break;
				}		
				//System.in.reset();
				input = System.in.read();
			}			
			
		} catch (IOException e) {
			System.out.println("IOException:"+e.getMessage());
			e.printStackTrace();
			return;
		}
		

	}

}


转载于:https://my.oschina.net/hacxlly/blog/265131

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值