线程逻辑的正确执行

jecbmhm3  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(273)

我试图在python的模拟器中重新创建java代码中设置断点的线程逻辑。

void runToBreakpoint() {
/******************************************************************************************

* If we have a runnable program loaded, we will run instructions until we encounter a   *
* breakpoint or the program terminates.  The tricky part is to capture the condition    *
* where we have just resumed from a previous breakpoint condition.  This method sets    *
* a boolean variable, "resumed," to toggle past the first instruction executed after    *
* a previous breakpoint pause.                                                          *
******************************************************************************************/

     Runnable runIt = new Runnable() {         // Create a thread in which to run.
       int lastStatementRun;                   // Hold the value of the PC for the
       Boolean isBreakpoint;                   // instruction we will run.
       String aString;
       public void run() {
         machineState = MARIE_RUNNING;
         while ((machineState == MARIE_RUNNING) && (!fatalError)) {
           runStop.setEnabled(true);
           aString = regPC.toString().trim();           // Move the cursor.
           if (codeReference.containsKey(aString)) {
             lastStatementRun =((Integer) codeReference.get(aString)).intValue();
           }
           fetchNext();
           try {                              // Give the user a chance to abort and also
             Thread.sleep(delay);             // a chance to see what's happening.
           }
           catch (InterruptedException e) {
           }
           if (!fatalError) {
             execute();
           }
           isBreakpoint = (Boolean) programArray[lastStatementRun][0];
           if ((machineState == MARIE_RUNNING)
               && (isBreakpoint.booleanValue()))  {    // Check for a breakpoint.
             machineState = MARIE_PAUSED;              // If we find one, pause.
             setStatusMessage(" Stopped for breakpoint.");
           }
           repaint();
         } // while
       } // run()
     }; // runIt
   if ((machineState == MARIE_UNINITIALIZED) ||
        (machineState == MARIE_NO_PROGRAM_LOADED))
     return;
   if ((machineState == MARIE_HALTED_NORMAL) ||
        (machineState == MARIE_HALTED_ABNORMAL))
     restart();
   fatalError = false;
   validate();                           // Reset fatal errors.
   breakpointOn = true;
   Thread runThread = new Thread(runIt);         // Run this in a thread.
   runThread.start();                            // Fire it off.
   if (fatalError)                               // Stop on errors.
     halt();
  } // runToBreakpoint()

我对python多线程非常陌生,并且尝试过重新创建这个示例,我想知道我对线程的逻辑是否与java示例中创建线程运行并在函数末尾启动线程的逻辑一样正确

def startBreakPoint():
    lastStatementRun = 0
    isBreakpoint = None
    aString = None
    machine_state = Running
    while machine_state == Running and not machine_error:
        # enable stop button as true
        aString = str(PC).strip()
        if aString in dictReference:
            lastStatementRun = int(dictReference.get(aString))
        fetchNextInstruction()
        try:
            time.sleep(5)
        except InterruptedError:
            print("Interrupted")
        if not machine_error:
            execute()
        isBreakpoint = bool(programArray[lastStatementRun][0])
        if machine_state == Running and isBreakpoint:
            machine_state = Paused
            print("Stopped for breakpoint")
    if machine_state == Uninitialized or machine_state == No_Program:
        return
    if machine_state == Normal_Halt or Abnormal_Halt:
        restart()
    machine_error = False
    # validate()
    breakpointOn = True
    x = threading.Thread(target=startBreakPoint(), args=(2,))
    x.start()

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题