zoukankan      html  css  js  c++  java
  • 多线程

    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;
    	}
    
    }
    

      //

  • 相关阅读:
    SpringBoot 之基础学习篇.
    Java 反射机制
    第二十二节,TensorFlow中的图片分类模型库slim的使用、数据集处理
    第二十一节,条件变分自编码
    第二十节,变分自编码
    第十九节,去噪自编码和栈式自编码
    使用webdriver+urllib爬取网页数据(模拟登陆,过验证码)
    第十八节,自编码网络介绍及代码实现
    第十七节,受限玻尔兹曼机网络及代码实现
    第二十二节,TensorFlow中RNN实现一些其它知识补充
  • 原文地址:https://www.cnblogs.com/tao7/p/12210364.html
Copyright © 2011-2022 走看看