Java Recursion with a Runnable in java

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    Java Recursion
AI Thread Summary
The discussion centers around the correct implementation of a recursive runnable in Java, specifically in the context of using SwingUtilities for UI updates. The original poster is attempting to create a button that increments a counter continuously while held down, but is encountering issues when transitioning from Java to Android. Key points include the importance of thread safety in UI updates, as both Java Swing and Android require UI elements to be updated from the main thread. The poster initially used recursion in their Java code but failed to replicate it in the Android version, leading to the counter only incrementing with each click rather than continuously. There is also a discussion about the desired rate of incrementing the counter while the button is held down, which is crucial for determining the implementation approach.
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
 
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 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
Back
Top