标签:决定 请求 相关 范围 分享图片 isp 结束 java线程 正在执行
线程的创建与启动
import java.io.File; /** * 启动一个线程,每隔一秒扫描一个指定文件夹,发现里面有其他文件存在就删除,保证该文件夹下只能存在(*.txt)的文件。 * @author dong007 * */ public class ThreadTxt extends Thread { /** 指定文件夹路径 */ private static String PATH = ""; public void run() { File file = new File(PATH); File[] fileArray = file.listFiles(); while (true) { try { Thread.sleep(1000); // 休眠一秒 } catch (InterruptedException e) { e.printStackTrace(); } for (File currFile : fileArray) { if (currFile.isFile() && !((currFile.getName()).endsWith(".txt"))) { currFile.delete(); System.out.println("此文件夹下非*.txt文件已删除"); } else if(currFile.isDirectory()){ System.out.println("此文件夹下不存在文件"); } else{ System.out.println("结束"); } } } } }
import java.io.File; public class MonitorRunner implements Runnable { private static String PATH = ""; public void run() { File file = new File(PATH); File[] fileArray = file.listFiles(); int before = 0; int current = 0; while(true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } before = current; current = fileArray.length; int count = current - before; if (count == 0) { System.out.println("未进行任何操作"); } else if (count > 0) { System.out.println("新建文件操作"); } else { System.out.println("删除文件操作"); } } } }
public class Main { public static void main(String[] args) { ThreadTxt threadTxt = new ThreadTxt(); threadTxt.start(); MonitorRunner monitorRunner = new MonitorRunner(); Thread monitorThread = new Thread(monitorRunner); monitorThread.start(); } }
线程的6种状态:
标签:决定 请求 相关 范围 分享图片 isp 结束 java线程 正在执行
原文地址:https://www.cnblogs.com/jxtx92/p/8484768.html