runtime可实现调用系统命令打开网页
package day; import java.io.IOException; public class Runt { public static void main(String[] args) { Runtime run = Runtime.getRuntime(); System.out.println("jvm最大内存量为"+ run.maxMemory()); try { run.exec("cmd /c start http://www.baidu.com"); //打开百度 } catch (IOException e) { e.printStackTrace(); } } }
实现数据库的定时备份(利用多线程)
package day; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; public class Db implements Runnable { private int m = 0; public Db(int m) { this.m = m; } public Db() { this.m = 5; } @Override public void run() { long ftime = 0; int num = 0; long start = 0; while (true) { start += 5; SimpleDateFormat sd = new SimpleDateFormat("yyyHHddHHmmss"); Date d = new Date(); String ff = sd.format(d) + ".sql"; Runtime run = Runtime.getRuntime(); try { if (start >= (this.m * 60)) { //m分钟后开始备份 run.exec("cmd /c D:\\m8\\mysql-8.0.12\\bin\\mysqldump.exe -uroot -proot zzz zzz > c:\\dbtext\\" + ff); //使用runtime实现数据库备份zzz数组库 start = 0; num++; while (num > 3) { //文件夹中只存放最近三次的备份,多余则删除 File[] f = new File("c:/dbtext").listFiles(); var arr = new ArrayList<Long>(); for (File fl : f) { ftime = fl.lastModified(); arr.add(ftime); } long min = Collections.min(arr); for (File fl : f) { if (fl.lastModified() == min) { fl.delete(); } } num--; } } } catch (IOException e) { e.printStackTrace(); } try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } } ----------------------------------------------------------------------------- package day; public class Dbtext { public static void main(String[] args) { Db d = new Db(1); Thread th = new Thread(d); //开启线程一直执行 th.start(); } }