单线程与多线程解析签到例题
例题:设计实现学生签到
要求:假设班级有四个人,通过控制台输入实现签到操作
单线程实现:
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();
}
}
}
}
}
}
运行结果: