使用GUI进行堆栈溢出

时间:2014-10-15 02:47:51

标签: java multithreading stack-overflow

当此方法在线程runnable上运行时,此代码会生成堆栈溢出错误。另外,如果您对效率有任何建议,请随时告诉我!您可能想知道某些部件是否必要,但大多数部件是必需的。谢谢你的帮助!

private void solveBrute(long start, long end) {

    long current = start;
    boolean found = false;
    boolean first = true;
    String check = "";
    while(!found && current <= end && !shouldStop) {
        check = this.intToBaseN(current, MainHS.getInstance().possiblechars);
        logVerbose("Trying " + check + " : " + getMd5Digest(check));
        if(first) {
            //MainHS.getInstance().central.logVerbose("Thread " + threadnum + " is starting value " + current + ". (" + last + ")");
            first = false;
        }
        if (valid(MainHS.getInstance().solveFor, check)) { 
            MainHS.getInstance().found(check, getMd5Digest(check));
            found = true;
            break;
        }
        current++;
    }
    if(!found && !shouldStop) {
        solveBrute((long)start + (long)(MainHS.getInstance().possiblechars.length) + 1L,(long)end + (long)(MainHS.getInstance().possiblechars.length) + 1L);
    }
}
//This is how I launch each thread
for(int a = 0; a < solvers.size(); a++) {
        final Solver s = solvers.get(a);
        final long sv = startingvalue;
        Thread t = new Thread(new Runnable() {
            public void run() {
                s.solve(wordlistSplit[current - 1], current + start - 1, f, l,sv,sv + possiblechars.length / MainHS.getInstance().possiblethreads);
            }
        });
        threads.add(t);
        t.start();
        current++;
        startingvalue += (possiblechars.length / MainHS.getInstance().possiblethreads) + 1 /*possiblethreads - 1*/;
        try {
            Thread.sleep((long)100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

产生的错误如下(线条偏离106):

Exception in thread "Thread-2" java.lang.StackOverflowError
at java.security.AccessControlContext.optimize(AccessControlContext.java:388)
at java.security.AccessController.getContext(AccessController.java:502)
at java.awt.AWTEvent.<init>(AWTEvent.java:106)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:224)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:188)
at java.awt.event.InvocationEvent.<init>(InvocationEvent.java:150)
at javax.swing.RepaintManager.scheduleProcessingRunnable(RepaintManager.java:1384)
at javax.swing.RepaintManager.scheduleProcessingRunnable(RepaintManager.java:1377)
at javax.swing.RepaintManager.addDirtyRegion0(RepaintManager.java:434)
at javax.swing.RepaintManager.addDirtyRegion(RepaintManager.java:456)
at javax.swing.JComponent.repaint(JComponent.java:4795)
at java.awt.Component.repaint(Component.java:3286)
at javax.swing.text.WrappedPlainView.updateChildren(WrappedPlainView.java:292)
at javax.swing.text.WrappedPlainView.removeUpdate(WrappedPlainView.java:485)
at javax.swing.plaf.basic.BasicTextUI$RootView.removeUpdate(BasicTextUI.java:1616)
at javax.swing.plaf.basic.BasicTextUI$UpdateHandler.removeUpdate(BasicTextUI.java:1876)
at javax.swing.text.AbstractDocument.fireRemoveUpdate(AbstractDocument.java:260)
at javax.swing.text.AbstractDocument.handleRemove(AbstractDocument.java:623)
at javax.swing.text.AbstractDocument.remove(AbstractDocument.java:591)
at javax.swing.text.AbstractDocument.replace(AbstractDocument.java:667)
at javax.swing.text.JTextComponent.setText(JTextComponent.java:1718)
at me.jadengrossman.hashsolver.gui.Solver$1.run(Solver.java:168)

但是,下面的代码工作得很好。我希望第一个代码能够工作,我该怎么做?

public void solveBrute(final int aaa, String first, String last) {
    count = 0;
    lettercount = aaa;
    START_OF_SEARCH_AREA = first.toCharArray()[0];
    END_OF_SEARCH_AREA = last.toCharArray()[0];
    progress.setMaximum(1000000000);
    possiblevalues = (int)Math.pow((int)END_OF_SEARCH_AREA - (int)START_OF_SEARCH_AREA + 1, aaa);
    progress.setValue(0);
    progress.setVisible(true);
    progress.setStringPainted(true);
    boolean pro = false;
    for(Component c: MainHS.getInstance().central.statsBox.getComponents()) {
        if(c.equals(progress)) {
            pro = true;
        }
    }
    if(!pro) {
        MainHS.getInstance().central.statsBox.add(progress);
    }
        FIRST_CANDIDATE = "";
        boolean found = false;
           for(int b = 0; b < aaa; b++) {
               FIRST_CANDIDATE += "" + first;
           }
           String c = FIRST_CANDIDATE;
            while ((c = next(c)) != null && !shouldStop) {
                if (valid(MainHS.getInstance().solveFor, c)) {
                    MainHS.getInstance().found(c, getMd5Digest(c));
                    found = true;
                    break;
                }
                if(aaa != c.length()) {
                    MainHS.getInstance().central.logVerbose("Finished " + (c.length() - 1) + " words. Found " + count + "/" + this.possiblevalues);
                    break;
                }
            }
            if(!found && !shouldStop && MainHS.end >= aaa + MainHS.getInstance().possiblethreads)
                solveBrute(aaa + MainHS.getInstance().possiblethreads, first, last);
}

非常感谢您的帮助!

0 个答案:

没有答案