设计模式之状态模式(State)+线程状态转换实例

定义:
状态模式是一种对象行为型模式,是指将有状态的对象,把复杂的“判断逻辑”提取到不同的状态对象中,允许状态对象在其内部状态发生改变时改变行为。
思想:
当控制一个对象状态转换的表达式过于复杂时,将相关“判断逻辑”提取出来,放到一系列的状态类中,这样可以把原来复杂的逻辑判断简单化
缺点:
在原有的基础上增加了系统的类和对象。
结构和实现较为复杂。
线程的生命周期以线程的生命周期为例。
代码:

package com.hh;

public class ThreadContext {
	private ThreadState threadState;
	public ThreadContext() {
		threadState=new NewState();
	}
	public ThreadState getThreadState() {
		return threadState;
	}
	public void setThreadState(ThreadState threadState) {
		this.threadState=threadState;
	}
	public void start(){
		((NewState)threadState).start(this);
	}
	public void getCpu() {
		((RunnableState)threadState).getCpu(this);
	}
	public void suspend() {
		((RunningState)threadState).suspend(this);
	}
    public void stop() {
    	((RunningState)threadState).stop(this);
    }
    public void resume() {
    	((BlockedState)threadState).resume(this);
    }
}

package com.hh;

public abstract class ThreadState {
	protected int state;//状态名
}

package com.hh;

public class StateSet {

	public static final int RUNNING = 0;
	public static final int RUNNABLE = 0;
	public static final int BLOCKED = 0;
	public static final int NEW = 0;
	public static final int DEAD = 0;

}

package com.hh;

public class RunningState extends ThreadState{
	public RunningState() {
		state=StateSet.RUNNING;
		System.out.println("运行状态");
	}
	public void suspend(ThreadContext tc) {
		System.out.println("调用suspend方法");
		if(state==StateSet.RUNNING)
			tc.setThreadState(new BlockedState());
		else
			System.out.println("当前状态不是运行状态");
	}
	public void stop(ThreadContext tc) {
		System.out.println("调用stop方法");
		if(state==StateSet.RUNNING)
			tc.setThreadState(new DeadState());
		else
			System.out.println("当前状态不是运行状态");
	}
}

package com.hh;

public class NewState extends ThreadState {
	public NewState() {
		state=StateSet.NEW;
		System.out.println("新建线程");
	}
	public void start(ThreadContext tc) {
		System.out.println("调用了Start方法");
		if(state==StateSet.NEW)
			tc.setThreadState(new RunnableState());
		else
			System.out.println("当前线程不是新建状态");
	}
}

package com.hh;

public class RunnableState extends ThreadState {
	public RunnableState() {
		state=StateSet.RUNNABLE;
		System.out.println("就绪状态");
	}

	public void getCpu(ThreadContext tc) {
		// TODO Auto-generated method stub
		System.out.println("调用getCpu方法");
		if(state==StateSet.RUNNABLE)
			tc.setThreadState(new RunningState());
		else
			System.out.println("当前状态不是就绪状态");
	}
}

package com.hh;

public class BlockedState extends ThreadState {
	public BlockedState() {
		state=StateSet.BLOCKED;
		System.out.println("阻塞线程");
	}
	public void resume(ThreadContext tc) {
		System.out.println("调用了resume方法");
		if(state==StateSet.BLOCKED)
			tc.setThreadState(new RunnableState());
		else
			System.out.println("当前线程不是新建状态");
	}
}

package com.hh;

public class DeadState extends ThreadState {
	public DeadState() {
		state=StateSet.DEAD;
		System.out.println("就绪状态");
	}

	public void stop(ThreadContext tc) {
		// TODO Auto-generated method stub
		System.out.println("调用getCpu方法");
		if(state==StateSet.DEAD)
			tc.setThreadState(new DeadState());
		else
			System.out.println("当前状态死亡");
	}
}

package com.hh;

public class ClientClass {
	public static void main(String args []) {
	ThreadContext tc=new ThreadContext();
	tc.start();
	tc.getCpu();
	tc.suspend();
	}
	
}

运行结果:
在这里插入图片描述如果修改一下客户端代码 在阻塞前stop线程:
在这里插入图片描述报错,状态不符,不可以执行stop操作,体现为不能进行状态转换。
学习视频指路:黑手书生
https://www.bilibili.com/video/av95698793?from=search&seid=5012547147156938065

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值