繁体   English   中英

Java 线程未并发执行

[英]Java threads not executed concurrently

我试图在 Java 中自动化某种详尽的密码,使用线程将任务分成四部分。

出于某种原因,当以下行

t0 = new password(0,1,0);

被执行,程序不会超出那个范围。 也就是说,它甚至在被 start() 方法调用之前运行线程。

在这种情况下,并行性根本不起作用。 试图停止线程也不起作用。

package password;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class password extends Thread {

    String[] l1 = {"w","e","r","s","f","x","c","v"};
    String[] l2 = {"u","o","j","k","l"};
    String[] l3 = {"a","s","d","y","c"};
    String[] l4 = {"i","o","p","k","é"};
    String[] l5 = {"w","e","r"};
    String[] l6 = {"q","w"};
    String[] l7 = {"e","r","t"};
    String[] l8 = {"e","t","d","f","g"};
    String[] l9 = {"z","u","i","h","k","n","m"};
    String[] l10 = {"q","e","a","s","d"};
    String[] l11 = {"q","w"};
    String[] l12 = {"r","t","z"};
    String[] l13 = {"i","o","p"};
    String[] l14 = {"h","j","k","n"};
    String[] l15 = {"w","a","s"};
    String[] l16 = {"o","è","l","é","a"};
    String[] l17 = {"q","w","e"};
    String[] l18 = {"t","z","u"};
    String[] l19 = {"r","t","z"};

    public password(int x, int y, int z) throws IOException{

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream("Z:/key"+"z"+".txt"), "utf-8"));

        new Thread(){
            int counter = 0, counter2 = 0;
            public void run() {}{
                for(int a = x; a<y; a++){
                    for(int b = 0; b<5; b++){
                        for(int c = 0; c<5; c++){
                            for(int d = 0; d<5; d++){
                                for(int e = 0; e<3; e++){
                                    for(int f = 0; f<2; f++){
                                        for(int g = 0; g<3; g++){
                                            for(int h = 0; h<5; h++){
                                                for(int i = 0; i<7; i++){
                                                    for(int j = 0; j<5; j++){
                                                        for(int k = 0; k<2; k++){
                                                            for(int l = 0; l<3; l++){
                                                                for(int m = 0; m<3; m++){
                                                                    for(int n = 0; n<4; n++){
                                                                        for(int o = 0; o<3; o++){
                                                                            for(int p = 0; p<5; p++){
                                                                                for(int q = 0; q<3; q++){
                                                                                    for(int r = 0; r<3; r++){
                                                                                        for(int s = 0; s<3; s++){
                                                                                            writer.write(new String(l1[a]+l2[b]+l3[c]+l4[d]+l5[e]+l6[f]+l7[g]+l8[h]+l9[i]+l10[j]+l11[k]+l12[l]+l13[m]+l14[n]+l15[o]+l16[p]+l17[q]+l18[r]+l19[s]));
                                                                                            writer.newLine();
                                                                                            counter++;
                                                                                            if(((counter%1000000) == 0) && counter!=0){
                                                                                                counter2++;
                                                                                                System.out.println("r0" + counter2);
                                                                                            }
                                                                                            Thread.yield();
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                writer.close();
            }
        };
    }
    public static void main(String[] args) throws IOException {
        password t0,t1,t2,t3;
        t0 = new password(0,1,0);
        t1 = new password(2,3,1);
        t2 = new password(4,5,2);
        t3 = new password(6,7,3);
        System.out.println("asd");

        long tiem = System.nanoTime();
        t0.start();
        t1.start();
        t2.start();
        t3.start();
        long anodertiem = System.nanoTime();

        System.out.println("Time taken: " + (anodertiem-tiem)/1000000);
    }

}

您的run()方法在没有任何代码的情况下关闭。

        public void run() {} // The method is already closed here...

接下来是在对象创建期间执行的代码块。

{
   // Many nested for loops.
}

不要关闭您的 run 方法(只需删除不必要的右大括号)。

您在构造函数中执行所有操作。 您应该将它们移到 run() 方法中。

另外,在线程类中启动一个新线程有点奇怪。 你不需要那个。

更多信息: http : //docs.oracle.com/javase/7/docs/api/java/lang/Thread.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM