110
社区成员
发帖
与我相关
我的任务
分享
package 树形结构; /** * @author ${范涛之} * @Description * @create 2021-11-04 8:18 */ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Tree { /** * 添加节点 * @param nodeMap * @param parentNodeName 父节点名称 * @param nodeName */ public static void addNode(Map<String,Node> nodeMap,String parentNodeName,String nodeName){ Node parentNode = null != parentNodeName ? nodeMap.get(parentNodeName) : null; Node node = nodeMap.get(nodeName); if(null != node){ return; } node = new Node(); node.setName(nodeName); nodeMap.put(nodeName, node); if(null != parentNode){ parentNode.addChild(node); node.setParent(parentNode); } } public static void main(String[] args) throws IOException { String [] arrays = new String[]{ "中国/北京市/朝阳区", "中国/北京市/海淀区", "中国/湖北省/武汉市/江汉区", "中国/湖北省/武汉市/江厦区", "中国/湖南省/长沙市", "中国/湖南省/株洲市", "美国/加利福尼亚州", "美国/德克萨斯州", }; Map<String,Node> resultMap = new HashMap<String,Node>(); for(String line : arrays){ String nodeNameArray [] = line.split("/"); String preNodeName = null; for(String curNodeName : nodeNameArray){ addNode(resultMap,preNodeName,curNodeName); preNodeName = curNodeName; } } for(Map.Entry<String,Node> entry : resultMap.entrySet()){ Node node = entry.getValue(); if(null == node.getParent()){ print(node,""); } } } /** * 递归打印node * @param node * @param blank */ public static void print(Node node,String blank){ System.out.println(blank + node.getName()); if(node.getChilds() == null || node.getChilds().isEmpty()){ return; } for(Node cur : node.getChilds()){ print(cur,blank + " "); } } static class Node{ private String name; private int index; private List<Node> childs; private Node parent; public List<Node> getChilds() { return childs; } public void addChild(Node child) { if(null == childs){ childs = new ArrayList<Node>(); } childs.add(child); } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } } }
try { Thread.sleep(1000);//单位:毫秒 } catch (Exception e) { }
注意:Thread.sleep(int)不能直接用,要做异常处理,try{}catch{}.
在这里需要解释一下线程沉睡的时间。sleep()方法并不能够让程序"严格"的沉睡指定的时间。例如当使用5000作为sleep()方法的参数时,线 程可能在实际被挂起5000.001毫秒后才会继续运行。当然,对于一般的应用程序来说,sleep()方法对时间控制的精度足够了。 但是如果要使用精确延时,最好使用Timer类:
Timer timer=new Timer();//实例化Timer类 timer.schedule(new TimerTask(){ public void run(){ System.out.println("退出"); this.cancel();}},500);//五百毫秒
package 支付通知; import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; /** * @author ${范涛之} * @Description * @create 2021-11-04 8:56 */ // 张伟在超市买了一瓶快乐水和一些吃的,使用支付 APP 完成支付后,过了几 // 秒,APP 通知他已经支付成功 public class PayTest { public static void main(String[] args) { System.out.println("请输入支付密码:"); delay(pay()); } // 使用APP付款 public static boolean pay(){ Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); if (number == 123456) return true; else return false; } // 延迟函数编写: public static void delay(boolean number){ Timer timer = new Timer(); if (number) { System.out.println("正在支付中......"); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("支付成功"); this.cancel(); } }, 5000); } else { System.out.println("正在支付中......"); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("支付失败,密码错误!"); this.cancel(); } }, 5000); } } }
import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class Test { public static void main(String[] args) { //timer1(); timer2(); //timer3(); //timer4(); } // 第一种方法:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date time) public static void timer1() { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { System.out.println("-------设定要指定任务--------"); } }, 2000);// 设定指定的时间time,此处为2000毫秒 } // 第二种方法:设定指定任务task在指定延迟delay后进行固定延迟peroid的执行 // schedule(TimerTask task, long delay, long period) public static void timer2() { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { System.out.println("-------设定要指定任务--------"); } }, 1000, 1000); } // 第三种方法:设定指定任务task在指定延迟delay后进行固定频率peroid的执行。 // scheduleAtFixedRate(TimerTask task, long delay, long period) public static void timer3() { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { System.out.println("-------设定要指定任务--------"); } }, 1000, 2000); } // 第四种方法:安排指定的任务task在指定的时间firstTime开始进行重复的固定速率period执行. // Timer.scheduleAtFixedRate(TimerTask task,Date firstTime,long period) public static void timer4() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 12); // 控制时 calendar.set(Calendar.MINUTE, 0); // 控制分 calendar.set(Calendar.SECOND, 0); // 控制秒 Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00 Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { System.out.println("-------设定要指定任务--------"); } }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行 } }
package 观影通知; import java.util.*; /** * @author ${范涛之} * @Description * @create 2021-11-04 9:26 */ //李雷周一在某某宝 APP 上买了两张周五晚上的电影票。在周五晚上电影即将 放映的前一小时,APP 给他发来了观影通知 public class Movie { public static void main(String[] args) { System.out.println("请选择你要购买的电影票:1:灵笼 2:第七夜"); delay(); } public static int pay(){ Scanner scanner = new Scanner(System.in); int r = scanner.nextInt(); if (r==1){ System.out.println("你选择了电影:灵笼,放映前一小时将会通知您"); return 1; }else System.out.println("你选择了电影:第七夜,放映前一小时将会通知您"); return 2; } public static void delay(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 10); // 控制时 calendar.set(Calendar.MINUTE, 36); // 控制分 calendar.set(Calendar.SECOND, 0); // 控制秒 Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的10:36:00 Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() { if (pay()==1) { System.out.println("请八点准时在新颖影院观看电影:灵笼"); } else { System.out.println("请八点准时在新颖影院观看电影:第七夜"); } } }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行 } }
package 洗衣服; import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; /** * @author ${范涛之} * @Description * @create 2021-11-04 14:42 */ //韩梅梅周末在家休息,她把衣服丢到洗衣机里之后就去扫地、做饭去了,过了一会,洗衣机就「嘀~嘀~」响几声通知她衣服已经洗好了 public class Clean { public static void main(String[] args) { System.out.println("请输入洗衣服的时长,请输入以小时为单位的整数"); delay(); } public static int time(){ Scanner scanner = new Scanner(System.in); int time = scanner.nextInt(); return time*1000*3600; } public static void delay(){ Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { System.out.println("嘀嘀嘀......嘀嘀嘀......嘀嘀嘀"); } } , time()); } }