簡體   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