Recursion with a Runnable in java

You might want to consider rephrasing the question.In summary, the conversation discussed using a recursive runnable in Java and Android to increase a counter while a button is held down. It was mentioned that Java Swing is not thread-safe and calls to UI elements must be made from the EDT. It was also noted that the equivalent of SwingUtilities.invokeLater() in Android is not as simple and a link was provided for more information. The purpose of the button was clarified to increment the counter while the button is held down, and the desired rate at which the counter should be incremented was requested for further clarification.
  • #1
Darkmisc
220
31
Hi everyone

Could someone show my the correct way to use a recursive runnable? The example I have involves the invokeLater method from SwingUtilities.


Recursive runnable with invokeLater:
public void mousePressed( MouseEvent e )
{
      Runnable runnable = new Runnable( )
       {
             public void run( )
             {
                   SwingUtilities.invokeLater( new Runnable( )
                    {
                          public void run( )
                          {
                                button.setText( String.valueOf( counter++ ) );
                           }
               } );
          }
      };

       future = executor.scheduleAtFixedRate( runnable, 0, 200,     TimeUnit.MILLISECONDS );
 }
I tried using it as a template while removing reference to the invokeLater method. The program loads, but nothing happens when I click the button.

code with my modifications:
Runnable mRunnable = new Runnable( )
{
     public void run( )
     {
          new Runnable( )
          {
              public void run( )
              {
                    n++;
                    myText.setText( String.valueOf( n ) );

                }
             } ;
        }
  };

The following lines are highlighted in Logat

Logcat:
2022-12-09 08:48:26.632 31195-31195/? E/le.clickcounte: Unknown bits set in runtime_flags: 0x8000
2022-12-09 08:48:27.744 31195-31227/com.example.clickcounter W/Gralloc3: mapper 3.x is not supported

2022-12-09 08:48:27.684 31195-31227/com.example.clickcounter W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...

2022-12-09 08:48:27.433 31195-31195/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2022-12-09 08:48:27.433 31195-31195/com.example.clickcounter W/le.clickcounte: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWin2022-12-09 08:48:27.228 31195-31195/com.example.clickcounter W/RenderThread: type=1400 audit(0.0:168): avc: denied { write } for name="property_service" dev="tmpfs" ino=8445 scontext=u:r:untrusted_app:s0:c136,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0
2022-12-09 08:48:27.236 31195-31229/com.example.clickcounter W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
Thanks
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
I am getting confused. This seems to be highly related to your previous thread, to which I have already replied. Do you want a solution in Java or Android? You are posting Logcat reports, which means Android. Please clarify.

And I don't understand why you even need recursion here. What are you trying to achieve?

Java UI (Swing or JavaFX) and Android UI are two different things. It appears that you are mixing them up.
 
  • #3
If you are trying to go from Java to Android, then here's what you should know: Java Swing is not thread-safe. All calls to UI elements must be made from the EDT (Event Dispatcher Thread). SwingUtilities.invokeLater(…) allows you to call the UI elements from a background thread.

In Android, too, you cannot update UI elements from child threads. But the equivalent of SwingUtilities.invokeLater(…) is not so simple in Android: refer to my post in your previous thread.

This may be helpful: https://stackoverflow.com/q/3551542/8387076
 
  • Like
Likes Darkmisc
  • #4
Wrichik Basu said:
If you are trying to go from Java to Android, then here's what you should know: Java Swing is not thread-safe. All calls to UI elements must be made from the EDT (Event Dispatcher Thread). SwingUtilities.invokeLater(…) allows you to call the UI elements from a background thread.

In Android, too, you cannot update UI elements from child threads. But the equivalent of SwingUtilities.invokeLater(…) is not so simple in Android: refer to my post in your previous thread.

This may be helpful: https://stackoverflow.com/q/3551542/8387076
The example I had was for Java and I'd been playing around with it in Java before trying to run it on Android. That's when I ran into problems.

I wanted to make a button that would increase a counter so long as the button was held down. Instead, the button increased the count by one for each click.

The original Java code used recursion to increase the counter so long as the button was held down. I'd forgotten to replicate the recursion when I modified the code for Android.
 
  • #5
I changed double "" to single " in the first post to fix a problem with the reply box not appearing for some users.
 
  • Like
Likes Wrichik Basu
  • #6
Darkmisc said:
I wanted to make a button that would increase a counter so long as the button was held down. Instead, the button increased the count by one for each click.
What is the rate at which the counter should be incremented while the button is down? That is, if I hold the button down for one second, do you want the counter to increment ten times during that second? Or ten thousand? Or what?

The answer will have a major effect on the design.
 

FAQ: Recursion with a Runnable in java

What is recursion in java and how is it used?

Recursion in java is a programming technique where a method repeatedly calls itself until a certain condition is met. In other words, the method is called within its own body. Recursion is often used to solve problems that can be broken down into smaller subproblems.

What is a Runnable in java?

A Runnable in java is an interface that is used to define a task to be executed by a thread. It contains a single method, run(), which is where the code to be executed by the thread is defined. Runnable is often used in conjunction with threads to perform concurrent operations in java.

How can recursion and Runnable be used together in java?

In java, recursion can be used within a Runnable object to create a task that will be executed by a thread. This allows for the execution of a recursive method in a separate thread, which can be useful for tasks that require a lot of processing or take a long time to complete.

What are some benefits of using recursion with a Runnable in java?

Using recursion with a Runnable in java allows for concurrent execution of recursive tasks, which can improve the performance and efficiency of the program. It also allows for better organization and separation of tasks, making the code more maintainable and easier to understand.

Are there any potential drawbacks of using recursion with a Runnable in java?

One potential drawback of using recursion with a Runnable in java is the risk of encountering a stack overflow error if the recursive method does not have a proper base case or termination condition. This can be avoided by carefully designing the recursive method and ensuring it has a proper termination condition.

Back
Top