1. 继承Thread类
2. 实现Runnable接口
3. 匿名类的方式
注: 启动线程是start()方法,run()并不能启动一个新的线程
package multiplethread; import java.io.File; public class TestThread { public static void search(File file, String search) { if (file.isFile()) { if(file.getName().toLowerCase().endsWith(".jsp")){ //当找到.jsp文件的时候,就启动一个线程,进行专门的查找 new SearchFileThread(file,search).start(); } } if (file.isDirectory()) { File[] fs = file.listFiles(); for (File f : fs) { search(f, search); } } } public static void main(String[] args) { File folder =new File("D:\project"); search(folder,"include"); } }
package multiplethread; import java.io.File; import java.io.FileReader; import java.io.IOException; public class SearchFileThread extends Thread{ private File file; private String search; public SearchFileThread(File file,String search) { this.file = file; this.search= search; } public void run(){ String fileContent = readFileConent(file); if(fileContent.contains(search)){ System.out.printf("找到子目标字符串%s,在文件:%s%n",search,file); } } public String readFileConent(File file){ try (FileReader fr = new FileReader(file)) { char[] all = new char[(int) file.length()]; fr.read(all); return new String(all); } catch (IOException e) { e.printStackTrace(); return null; } } }
package multiplethread; import java.util.List; public class PasswordThread extends Thread{ private boolean found = false; private String password; private List<String> passwords; public PasswordThread(String password, List<String> passwords) { this.password = password; this.passwords = passwords; } public void run(){ char[] guessPassword = new char[password.length()]; generatePassword(guessPassword, password); } public void generatePassword(char[] guessPassword, String password) { generatePassword(guessPassword, 0, password); } public void generatePassword(char[] guessPassword, int index, String password) { if (found) return; for (short i = '0'; i <= 'z'; i++) { char c = (char) i; if (!Character.isLetterOrDigit(c)) continue; guessPassword[index] = c; if (index != guessPassword.length - 1) { generatePassword(guessPassword, index + 1, password); } else { String guess = new String(guessPassword); //穷举每次生成的密码,都放进集合中 passwords.add(guess); if (guess.equals(password)) { System.out.println("找到了,密码是" + guess); found = true; return; } } } } }
package multiplethread; import java.util.List; public class LogThread extends Thread{ private boolean found = false; private List<String> passwords; public LogThread(List<String> passwords) { this.passwords = passwords; this.setDaemon(true);//把记日志的这个线程,设置为守护线程 } public void run(){ while(true){ while(passwords.isEmpty()){ try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } String password = passwords.remove(0); System.out.println("穷举法本次生成的密码是:" +password); } } }
package multiplethread; import java.util.ArrayList; import java.util.List; public class TestThread { public static boolean found = false; public static void main(String[] args) { String password = randomString(3); System.out.println("密码是:" + password); List<String> passwords = new ArrayList<>(); new PasswordThread(password,passwords).start(); new LogThread(passwords).start(); } private static String randomString(int length) { String pool = ""; for (short i = '0'; i <= '9'; i++) { pool += (char) i; } for (short i = 'a'; i <= 'z'; i++) { pool += (char) i; } for (short i = 'A'; i <= 'Z'; i++) { pool += (char) i; } char cs[] = new char[length]; for (int i = 0; i < cs.length; i++) { int index = (int) (Math.random() * pool.length()); cs[i] = pool.charAt(index); } String result = new String(cs); return result; } }
//