Recursion with a Runnable in java

  • Context: Java 
  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    Java Recursion
Click For Summary

Discussion Overview

The discussion revolves around the implementation of a recursive runnable in Java, specifically in the context of using it with Android UI elements. Participants explore the challenges faced when transitioning from Java Swing to Android, particularly regarding thread safety and UI updates.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant shares a code snippet attempting to use a recursive runnable with the invokeLater method from SwingUtilities, but notes that it does not function as expected in Android.
  • Another participant expresses confusion about whether the solution is intended for Java or Android, highlighting the differences between the two environments.
  • Some participants mention that Java Swing is not thread-safe and that UI updates must occur on the Event Dispatcher Thread (EDT), while also noting that Android has its own mechanisms for UI updates that differ from Swing.
  • A participant clarifies their intention to create a button that increments a counter while held down, indicating that they had initially implemented recursion in Java but forgot to replicate it in the Android version.
  • There is a question raised about the desired rate of incrementing the counter while the button is held down, suggesting that this will significantly impact the design of the solution.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of recursion in this context, and there is no consensus on the best approach to implement the desired functionality in Android versus Java.

Contextual Notes

Participants note the importance of understanding the differences in thread safety and UI element updates between Java Swing and Android, which may affect the implementation of the recursive runnable.

Darkmisc
Messages
222
Reaction score
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.


[CODE title="Recursive runnable with invokeLater" highlight="13, 25"]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 );
}[/CODE]
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 lang="java" title="code with my modifications" highlight="9, 21"]Runnable mRunnable = new Runnable( )
{
public void run( )
{
new Runnable( )
{
public void run( )
{
n++;
myText.setText( String.valueOf( n ) );

}
} ;
}
};[/CODE]

The following lines are highlighted in Logat

[CODE lang="java" title="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[/CODE]
 
Last edited by a moderator:
Technology news on Phys.org
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.
 
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   Reactions: Darkmisc
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.
 
I changed double "" to single " in the first post to fix a problem with the reply box not appearing for some users.
 
  • Like
Likes   Reactions: Wrichik Basu
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.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
Replies
3
Views
4K
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K