JAVA中线程的创建与理解2

单线程与多线程解析签到例题

例题:设计实现学生签到

要求:假设班级有四个人,通过控制台输入实现签到操作

单线程实现:

public class Insign {
	
	public static void main(String[] args) {
		Hashbook sign = new Hashbook();
		sign.show();
		
	
	}

}

public class Hashbook {
	//创建点名表
	HashMap<String,Student> book;
	Scanner sc = new Scanner(System.in);
	Date standard = new Date(System.currentTimeMillis()+10000);
	Date truetime;
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
	//开始点名
	public Hashbook(){
		book=new HashMap();
		System.out.println("开始签到");
		sign();
	}
	//点名
	public void sign(){
		int count=1;
		//班里有4个学生,count记学生签到数
		while(true){
			if(count==4){
				System.out.println("全员到齐");
				break;
			}
			truetime=new Date();
			if(standard.before(truetime)){
				System.out.println("签到结束,剩下的迟到了");
				break;
			}
			System.out.println("请签到:输入姓名");
			String sname = sc.next();
			System.out.println("请签到:学号");
			String sid = sc.next();
			Student stu = new Student(sname,sid);
			stu.setTime(truetime);
			book.put(stu.getId(), stu);
			count++;
		}
	}
	
	//显示签到名单
	public void show(){
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("显示已签到同学名单");
		Set<Map.Entry<String,Student>> set = book.entrySet();
		Iterator<Map.Entry<String,Student>> it = set.iterator();
		
		while(it.hasNext()){
			Map.Entry node=it.next();
			String strtime =format.format(((Student) node.getValue()).getTime());
			System.out.println(node.getKey()+"\t"+((Student) node.getValue()).getName()+"\t\t"+strtime);
		}
		System.out.println("班长到了吗?");//假设只有班长叫班长
		check();
	}
	public void check(){
		Collection<Student> col = book.values();
		for(Student stu:col){
			if("班长".equals(stu.getName())){
				System.out.println("班长到了");
				return;
			}

		}
		System.out.println("班长没到");
		
		
	}

}
public class Student {
	private String name;
	private Date time;
	private String id;
	
	public Student(String name,String id) {
		super();
		this.name = name;
		this.id = id;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getTime() {
		return time;
	}
	public void setTime(Date time) {
		this.time = time;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", time=" + time + ", id=" + id + "]";
	}
	
	
}

运行结果:


    

多线程实现

创建一个线程,在签到程序运行的同时,模拟时间的流走。

public class Insign {
	
	public static void main(String[] args) {
		Hashbook sign = new Hashbook();
		sign.show();

	}

}
public class Hashbook {
	//创建点名表
	//班里共有4个学生,count记学生签到数
	int count=1;
	HashMap<String,Student> book;
	Scanner sc = new Scanner(System.in);
	Date standard = new Date(System.currentTimeMillis()+10000);
	Date truetime;
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
	//开始点名
	public Hashbook(){
		book=new HashMap();
		System.out.println("开始签到");
		sign();
	}
	//点名
	public void sign(){	
		Checkedtime action2 = new Checkedtime();
		Thread act2 = new Thread(action2);
		act2.start();
		while(true){
			if(count==4){
				System.out.println("全员到齐");
				break;
			}
			if(standard.before(new Date())){
				System.out.println("签到结束,剩下的迟到了");
				break;
			}
			System.out.println("请签到:输入姓名");
			String sname = sc.next();
			System.out.println("请签到:学号");
			String sid = sc.next();
			Student stu = new Student(sname,sid);
			stu.setTime(truetime);
			book.put(stu.getId(), stu);
			count++;
		}
		
			
		
		
		
	}
	
	//显示签到名单
	public void show(){
		System.out.println();
		System.out.println();
		System.out.println();
		System.out.println("显示已签到同学名单");
		Set<Map.Entry<String,Student>> set = book.entrySet();
		Iterator<Map.Entry<String,Student>> it = set.iterator();
		
		while(it.hasNext()){
			Map.Entry node=it.next();
			String strtime =format.format(((Student) node.getValue()).getTime());
			System.out.println(node.getKey()+"\t"+((Student) node.getValue()).getName()+"\t\t"+strtime);
		}
		System.out.println("班长到了吗?");//假设只有班长叫班长
		check();
	}
	
	public void check(){
		Collection<Student> col = book.values();
		
		for(Student stu:col){
			if("班长".equals(stu.getName())){
				System.out.println("班长到了");
				return;
			}
				
			

		}
		System.out.println("班长没到");
		
		
	}
public class Checkedtime implements Runnable{
		@Override
		public void run() {
			while(true){
				truetime=new Date();
				if(standard.before(truetime)){
					System.out.println("请注意,签到到时,这是最后一位同学");
					break;
				}else{
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
	}
	
	
	
	
	
	
	

}
}

 

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值