0

i want method 2 to sleep until method 1 allow method 2 to continue to run again. How do I accomplish this pause?

method 1{
some code here
if(enter key pressed){
allow method 2 to continue
}
}
method 2{
some code here
wait until method 1 allow you to continue
do whatever you are supposed to continue with
}

4 Answers4

0

In any language methods should be precise and ONE-TASK oriented, if method 2 begins a tasks and then waits for external calls to continue, then that is a good sign of smelly code,

I will suggest to take a look at the code an split their actions is more specific task and then bind them in the logic you need.

LIKE

method 1{
        some code here
        method2_SubTaskA()
        if(enter key pressed){
            allow method 2 to continue
            method2_SubTaskB()
        }
}
method 2SubTaskA{
        some code here
        wait until method 1 allow you to continue
        do whatever you are supposed to continue with
}
method 2SubTaskB{
        some code here
        wait until method 1 allow you to continue
        do whatever you are supposed to continue with
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You could use multithreading for this:

boolean method2Continue = false;

public void method1() {
    // ...
    Thread thread = new Thread(() -> method2());
    thread.start();
    if(keyPressed){
        method2Continue = true;
    }
    thread.interrupt();
    // ...
}
public void method2() {
    // ...
    while(!method2Continue);
    // ...
}

After thread.start() is called, method 1 and method 2 will execute simultaneously, but when method 2 reaches the while loop, it will wait for method2Continue to be true until it continues on to the next line. At the end of method 1, the method 2 thread will be terminated which will stop its execution.

See a demo here: https://ideone.com/eqnTh7

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
  • You should probably add the `volatile` keyword to `method2Continue` – Austin May 06 '16 at 17:20
  • this seem to be good but i get an red line under InterruptedException in the try catch and the message "Syntax error, insert "VariableDeclaratorId" to complete FormalParameter" –  May 06 '16 at 17:33
0

Using multiple threads can be avoided here:

private Runnable runnable;

void method1() {
    // Some code here
    if(enter key pressed && runnable != null) {
        runnable.run(); // Execute continue code
        runnable = null; // Make sure it can't be run twice
    }
}

void method2() {
    // Some code here
    runnable = new Runnable(() -> {
        // Continue code here
    });
}

Using an anonymous class will allow you to capture variables from method2.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
0

As @SamTebbs33 said, use multi threading; his example is a good starting point. Instead of using a boolean, I recommend using a CountdownLatch to indicate that it's time for method2 to continue. A detailed example is given in the following question: How is CountDownLatch used in Java Multithreading?

In general, using the built-in concurrency APIs is safer than writing your own home-grown solution---it is more widely understandable and less error prone.

See the CoubtdownLatch JavaDocs at https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

Community
  • 1
  • 1
Austin
  • 8,018
  • 2
  • 31
  • 37