多线程案例练习

这是多线程基础练习,防止将来忘记

 

1.把 练习-查找文件内容 改为多线程查找文件内容
原练习的思路是遍历所有文件,当遍历到文件是 .java的时候,查找这个文件的内容,查找完毕之后,再遍历下一个文件

现在通过多线程调整这个思路:
遍历所有文件,当遍历到文件是.java的时候,创建一个线程去查找这个文件的内容,不必等待这个线程结束,继续遍历下一个文件

FileRead.java

package cn.chenc.stydy.demo3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 *  @description: TODO
 *  @author 陈_C
 *  @date 2020/3/7 15:32
 *
 */
public class FileRead implements Runnable{

    private File file;

    public FileRead(File file) {
        this.file = file;
    }

    @Override
    public void run() {
        String name=Thread.currentThread().getName();
        System.out.println("准备使用"+name+"读取文件");
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            while ((str = in.readLine()) != null) {
                System.out.println(file.getName()+":"+str);
            }
            System.out.println(str);
        } catch (IOException e) {
        }
    }
}

  

 1 package cn.chenc.stydy.demo3;
 2 
 3 import java.io.File;
 4 
 5 /**
 6  *  @description: TODO
 7  *  @author 陈_C
 8  *  @date 2020/3/7 15:18
 9  *
10  */
11 public class Demo3Test {
12 
13     public static void main(String[] args){
14         File f=new File("E:\\program Files\\idea_project\\ThreadDemo\\src\\cn\\chenc\\stydy\\demo3");
15         File[] fileList=f.listFiles();
16         for(File file:fileList){
17             String fileName=file.getName();
18             String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
19             if(suffix.equals("java")){
20                 FileRead fileRead=new FileRead(file);
21                 new Thread(fileRead).start();
22             }
23         }
24 
25     }
26 }
Demo3Test.java

 

2.英雄有可以放一个技能叫做: 波动拳-a du gen。
每隔一秒钟,可以发一次,但是只能连续发3次。

发完3次之后,需要充能5秒钟,充满,再继续发。

 1 package cn.chenc.stydy.demo4;
 2 
 3 /**
 4  *  @description: TODO
 5  *  @author 陈_C
 6  *  @date 2020/3/7 16:17
 7  *
 8  */
 9 public class Demo4Test {
10 
11     public static void main(String[] args){
12 
13 
14         Thread t1=new Thread(){
15             int bd=1;
16             @Override
17             public void run() {
18                 for(;bd<=3;bd++){
19                     System.out.println("波动拳第"+bd+"发");
20                     if(bd==3){
21                         try {
22                             System.out.println("开始5秒充能");
23                             Thread.sleep(5000);
24                             bd=0;
25                         } catch (InterruptedException e) {
26                             e.printStackTrace();
27                         }
28                     }
29                 }
30             }
31         };
32         t1.start();
33 
34     }
35 }
Demo4Test.java

 

3.

1. 生成一个长度是3的随机字符串,把这个字符串当作 密码

2. 创建一个破解线程,使用穷举法,匹配这个密码

3. 创建一个日志线程,打印都用过哪些字符串去匹配,这个日志线程设计为守护线程

 1 package cn.chenc.stydy.demo5;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 /**
 7  *  @description: TODO
 8  *  @author 陈_C
 9  *  @date 2020/3/7 16:33
10  *
11  */
12 public class Demo5Test {
13 
14     public static void main(String[] args){
15         StringBuilder str1 = new StringBuilder();
16         String password;//存放长度是3的随机字符串
17         StringBuilder str3= new StringBuilder();
18         List<String> list=new ArrayList<>();
19         for (short i = '0'; i <= 'z'; i++) {
20             if (Character.isLetter((char) i) || Character.isDigit((char) i)) {
21                 str1.append((char) i);
22             }
23         }
24         String str2= str1.toString();
25 
26         for(int i=0;i<3;i++){
27             int idx =(int) Math.floor(Math.random() * str2.length());
28             str3.append(str2.charAt(idx));//str2.charAt(idx);得到一个字符
29         }
30         //长度是3的随机字符串
31         password=str3.toString();
32         System.out.println("生成的密码是:");
33         System.out.println(str3);
34         System.out.println("---------------");
35 
36         Thread t1=new Thread(){
37 
38             @Override
39             public void run(){
40                 for(int i=0;i<str2.length();i++){
41                     for(int j=0;j<str2.length();j++){
42                         for(int k=0;k<str2.length();k++){
43                             String check = ""+str2.charAt(i) + str2.charAt(j) + str2.charAt(k);
44                             list.add(check);
45                             if (check.equals(password)) {
46                                 System.out.println("密码是:" + check);
47                                 return;
48                             }
49                         }
50                     }
51                 }
52             }
53         };
54 
55         Thread t2=new Thread(){
56             public void run(){
57                 while (true){
58                     if (list.size()==0) {
59                         try {
60                             //发现容器是空的,就休息1秒
61                             Thread.sleep(1000);
62                         } catch (InterruptedException e) {
63                             e.printStackTrace();
64                         }
65                     }
66                     else{
67                         //日志线程不断的从这个容器中拿出可能密码
68                         System.out.println(list.remove(0));
69                     }
70                 }
71             }
72         };
73         t2.setDaemon(true);
74     //    t1.setPriority(Thread.MIN_PRIORITY);
75     //    t2.setPriority(Thread.MAX_PRIORITY);
76         t1.start();
77         t2.start();
78 
79 
80 
81     }
82 }
Demo5Test

 

posted on 2020-03-07 16:12  萌萌哒的陈C酱  阅读(332)  评论(0编辑  收藏  举报

导航