-1

I have an application which has 2 classes(form and execution) and 2 buttons. With first button I will execute entire function I want to be executed and I want the second button to execute a part of function. More exactly, after I press the second button it will finish a part of function and after that it will enter a while true and wait to be pressed again the second button. The problem there it's... all my program is stopped when I've press the second button.

Form class:

   new Thread() {
        @Override
        public void run() {
            debugChecker = 1;
            exection(microCode);
            debugChecker = 2;
        }
    }.start();

Execution class:

   public static void exection(List<List<String>> microCode) {
   for (int i = 0; i < microCode.size(); i++) {
        OUTER:
        for (int j = 0; j < microCode.get(i).size(); j++) {
            STEP:
            {
                aluValue.setText(microCode.get(i).get(2));
                String curentFunction = microCode.get(i).get(j);
                switch (curentFunction) {
                    case "IF":
                        try {
                            check = true;

                            Method m = Registers.class.getMethod(microCode.get(i).get(j + 1));
                            boolean condition = (boolean) m.invoke(new Registers());
                            if (condition) {
                                if (!microCode.get(i).get(j + 2).equals("STEP")) {
                                    String adr2 = microCode.get(i).get(j + 3);
                                    String jump_adr = JUMPI(adr2);
                                    microCode = UCODE.get(jump_adr);

                                    if (jump_adr.matches("INT|CALL|NOT_RD|RET|RETI|IM_D|IM_S")) {
                                        memoryOffset = offset;
                                    } else {
                                        memoryOffset = 0;
                                    }
                                    exection(microCode);
                                    return;
                                } else if (microCode.get(i).get(j + 2).equals("STEP")) {
                                    i++;
                                    j = -1;
                                    break STEP;
                                }
                            }
                            if (microCode.get(i).get(j + 2).equals("STEP")) {
                                j = j + 2;
                            } else {
                                j = j + 3;
                            }
                        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                            Logger.getLogger(ExecutionMicroInstruction.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        break;
                    case "ELSE":
                        if (microCode.get(i).get(j + 1).equals("STEP")) {
                            j += 1;
                            break OUTER;
                        } else if (JMP(microCode.get(i).get(j + 2)).equals("IFCH")) {
                            return;
                        } else {
                            String adr3 = microCode.get(i).get(j + 2);
                            String jump_adr3 = JUMPI(adr3);
                            microCode = UCODE.get(jump_adr3);

                            getCurentOperation(jump_adr3);

                            exection(microCode);
                            return;
                        }
                    case "STEP":
                        check = true;
                        break;
                    case "JUMPI":
                        check = true;
                        String instr2 = microCode.get(i).get(j + 1);
                        String address = JUMPI(instr2);
                        microCode = UCODE.get(address);

                        getCurentOperation(address);
                        if (address.matches("INT|CALL|NOT_RD|RET|RETI|IM_D|IM_S")) {
                            memoryOffset = offset;
                        } else {
                            memoryOffset = 0;
                        }
                        exection(microCode);
                        return;
                    case "JMP":
                        check = true;
                        if (JMP(microCode.get(i).get(j + 1)).equals("IFCH")) {
                            return;
                        }
                        microCode = UCODE.get(JMP(microCode.get(i).get(j + 1)));

                        getCurentOperation(JMP(microCode.get(i).get(j + 1)));
                        System.out.println("---" + JMP(microCode.get(i).get(j + 1)) + "---");

                        exection(microCode);
                        return;
                    default:
                        switch (debugChecker) {
                            case 0: {
                                String instruction = microCode.get(i).get(j);
                                List<String> list = microCode.get(i);
                                try {
                                    if (check) {
                                        mergeImagesByName(list);
                                        check = false;
                                    }
                                    Method instr = Registers.class.getMethod(instruction);
                                    instr.invoke(new Registers());
                                } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                                    Logger.getLogger(ExecutionMicroInstruction.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                break;
                            }
                            case 1: {
                                String instruction = microCode.get(i).get(j);
                                try {
                                    mergeJustOneElemOnTime(instruction);
                                    Method instr = Registers.class.getMethod(instruction);
                                    instr.invoke(new Registers());
                                    debugChecker = 2;
                                    System.out.println(instruction);

                                } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                                    Logger.getLogger(ExecutionMicroInstruction.class.getName()).log(Level.SEVERE, null, ex);
                                }
                                break;
                            }
                            case 2: {
                                while (debugChecker == 2) {
                                }
                            }
                            default:
                                break;
                        }
                        break;
                }
            }
        }
    }
}

EDITED:

I've tried with thread, but that's not what I want. Because if I will do with threads it will executed entire function. What I need is... when I will press on secondButton I want to make JUST first step of this for, I want to do just j=j+1 after I've press again it will do this j=j+1 again, and so on. After it will finish with J it will take I, and so on. All what I want it's just one increment on time. Just it. And threads don't help me.

Filburt
  • 17,626
  • 12
  • 64
  • 115
cavaler12345
  • 367
  • 1
  • 5
  • 15

2 Answers2

1

You are blocking execution by running in the main thread. You need to move your while loop into a background thread instead. It sounds like you need to learn more about how processes and threads work. I attached a tutorial from Oracle.

The Java Tutorials: Concurrency

bodangly
  • 2,473
  • 17
  • 28
  • I want to put any thread in background... that want help me... because all my program will works like that don't exist... but what I want it's just to put a pause on program until I will press again on secondButton. – cavaler12345 May 10 '16 at 19:59
  • seriously guys, I've no idea how to resolve it... no any fking idea... please... can one of you help me, really now... :( – cavaler12345 May 10 '16 at 21:27
0

seriously guys, I've no idea how to resolve it...

You have to structure the program differently.

Your secondButton(...) function is an event handler. When your application starts up, it creates a new thread called the Event Dispatch Thread (EDT). The EDT runs a loop that;

  • Accepts keyboard or mouse input (a.k.a., an event) from the operating system,
  • Figures out which of your handlers is interested in that event,
  • Calls your handler, and then
  • Goes back to waiting for the next event.

The EDT can not respond to the next event until your handler returns. Your program will never be able to respond to another key press or mouse click if seconButton(...) never returns (which, it doesn't in case 2).


You want your "second button" to start something on the first press, and finish it on the second press. What you'll have to do is write your secondButton() handler to know how many times it has been pressed, and perhaps, to know whether or not it's time to accept the second press.

If the second press can come too early, then you'll either have to figure out what kind of feedback to give the user on a too-early press, or else you'll want to make the first press disable the button, and then have some other thread re-enable it when it makes sense for the user to press the button the second time.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • I've do that... the problem is... I don't need to put it on another thread, I've already try that. If I will put it in another thread.. yes, it will be executed correctly but it will be executed complete and I don't want that... I will edit my question to add my `exectuion class`. Maybe you will understand much better what I need – cavaler12345 May 10 '16 at 21:45