Recursion with a Runnable in java

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

This discussion focuses on implementing a recursive Runnable in Java, specifically using the SwingUtilities.invokeLater method to update UI elements safely. The user initially attempted to adapt a Java example for Android but encountered issues due to the differences in threading models between Java Swing and Android UI. Key points include the importance of using the Event Dispatcher Thread (EDT) in Java and the need for a different approach in Android to update UI elements from background threads. The user ultimately aims to create a button that increments a counter continuously while pressed, rather than on each click.

PREREQUISITES
  • Understanding of Java Runnable and threading concepts
  • Familiarity with SwingUtilities and the Event Dispatcher Thread (EDT)
  • Knowledge of Android UI threading and its differences from Java Swing
  • Experience with Logcat for debugging Android applications
NEXT STEPS
  • Learn about Android Handler and its role in updating UI from background threads
  • Explore the use of ScheduledExecutorService for periodic tasks in Java
  • Investigate the differences between Java Swing and Android UI frameworks
  • Research best practices for implementing recursive methods in Java and Android
USEFUL FOR

Java developers transitioning to Android, Android UI developers, and anyone interested in managing UI updates from background threads effectively.

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