[graalvm-users] [External] : java.lang.Thread.sleep() supported in GraalVM?

Christian Wimmer christian.wimmer at oracle.com
Mon Oct 16 18:16:58 UTC 2023


Hi Eric,

As Gilles already said, initializing all classes at build time is something that cannot work for your use case. For AWT, a lot of class initializer call into native code to initialize OS state, and that state cannot be transferred from image build time to image run time.

-Christian
________________________________
From: Gilles DUBOSCQ via GraalVM-Users <graalvm-users at oss.oracle.com>
Sent: Monday, October 16, 2023 11:10
To: Eric Samson <erix.samson at gmail.com>
Cc: graalvm-users at oss.oracle.com <graalvm-users at oss.oracle.com>
Subject: Re: [graalvm-users] [External] : java.lang.Thread.sleep() supported in GraalVM?

It looks like the issue is not with Thread.sleep but Swing/AWT on Windows (specifically sun.awt.shell.Win32ShellFolderManager2$ComInvoker).

I think it's not a good idea to use --initialize-at-build-time, you're likely to get into a lot of such corner cases. In general only code that has been designed for it or has explicit support in native-image works well under --initialize-at-build-time. I would recommend to use --initialize-at-build-time only on specific packages or type for which you know there is a real benefit.
If you really want to initialize everything at build time, maybe you can try to use --initialize-at-run-time=sun.awt.shell.Win32ShellFolder2

 Gilles
________________________________
From: Eric Samson <erix.samson at gmail.com>
Sent: Monday, 16 October 2023 19:10
To: Gilles DUBOSCQ <gilles.m.duboscq at oracle.com>
Cc: graalvm-users at oss.oracle.com <graalvm-users at oss.oracle.com>
Subject: Re: [External] : [graalvm-users] java.lang.Thread.sleep() supported in GraalVM?

Hi Gilles,

Thanks for the quick reply.
Here is the error message:

Error: Detected a started Thread in the image heap. Threads running in the image generator are no longer running at image runtime.  To see how this object got instantiated use --trace-object-instantiation=java.lang.Thread. The object was probably created by a class initializer and is reachable from a static field. You can request class initialization at image runtime by using the option --initialize-at-run-time=<class-name>. Or you can write your own initialization methods and call them explicitly from your main entry point.

I've tried the option --initialize-at-runtime=java.lang.Thread, without success.

StackTrace:
Trace: Object was reached by
  reading field java.util.concurrent.ThreadPoolExecutor$Worker.thread of constant
    java.util.concurrent.ThreadPoolExecutor$Worker at 63a4cfd: java.util.concurrent.ThreadPoolExecutor$Worker at 63a4cfd[State = 1, empty queue]
  reading field java.util.HashMap$Node.key of constant
    java.util.HashMap$Node at 53798803: java.util.concurrent.ThreadPoolExecutor$Worker at 63a4cfd[State = 1, empty queue]=j...
  indexing into array java.util.HashMap$Node[]@2f2400d: [Ljava.util.HashMap$Node;@2f2400d
  reading field java.util.HashMap.table of constant
    java.util.HashMap at 1ac46e3c: {java.util.concurrent.ThreadPoolExecutor$Worker at 63a4cfd[State = 1, empty queue]=...
  reading field java.util.HashSet.map of constant
    java.util.HashSet at 74b3b357: [java.util.concurrent.ThreadPoolExecutor$Worker at 63a4cfd[State = 1, empty queue]]
  reading field java.util.concurrent.ThreadPoolExecutor.workers of constant
    sun.awt.shell.Win32ShellFolderManager2$ComInvoker at 7f227548: sun.awt.shell.Win32ShellFolderManager2$ComInvoker at 7f227548[Running, pool size = ...
  scanning root sun.awt.shell.Win32ShellFolderManager2$ComInvoker at 7f227548: sun.awt.shell.Win32ShellFolderManager2$ComInvoker at 7f227548[Running, pool size = ... embedded in
    sun.awt.shell.ShellFolder.invoke(ShellFolder.java:537)
  parsing method sun.awt.shell.ShellFolder.invoke(ShellFolder.java:537) reachable via the parsing context
    at sun.awt.shell.Win32ShellFolder2.<init>(Win32ShellFolder2.java:292)
    at com.oracle.svm.core.code.FactoryMethodHolder.Win32ShellFolder2_constructor_79b7324abf2daa84de416777f746e499d5310ac3(generated:0)
    at sun.awt.shell.Win32ShellFolderManager2.getDesktop(Win32ShellFolderManager2.java:171)
    at sun.awt.shell.Win32ShellFolderManager2.isFileSystemRoot(Win32ShellFolderManager2.java:504)
    at javax.swing.filechooser.FileSystemView.isFileSystemRoot(FileSystemView.java:423)
    at javax.swing.filechooser.FileSystemView.getFiles(FileSystemView.java:581)
    at javax.swing.filechooser.FileSystemView.isParent(FileSystemView.java:338)
    at javax.swing.JFileChooser.setSelectedFile(JFileChooser.java:513)
    at javax.swing.JFileChooser.setFileFilter(JFileChooser.java:1472)
    at javax.swing.JFileChooser.setup(JFileChooser.java:398)
    at javax.swing.JFileChooser.<init>(JFileChooser.java:362)
    at com.oracle.svm.core.code.FactoryMethodHolder.JFileChooser_constructor_0980039f483e9a48b2389d8a4a1a9cd473a16dbd(generated:0)
    at javax.swing.text.html.FormView$BrowseFileAction.actionPerformed(FormView.java:968)
    at java.awt.MenuItem.processActionEvent(MenuItem.java:692)
    at java.awt.MenuItem.processEvent(MenuItem.java:651)
    at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:379)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:776)
    at java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.awt.EventQueue$4.run(EventQueue.java:716)
    at com.oracle.svm.core.jdk.Target_java_security_AccessController.executePrivileged(SecuritySubstitutions.java:171)
    at sun.font.SunFontManager.<init>(SunFontManager.java:457)
    at sun.awt.Win32FontManager.<init>(Win32FontManager.java:88)
    at com.oracle.svm.core.code.FactoryMethodHolder.Win32FontManager_constructor_002b6104bcda27602265e5cec50c16a5ebc813af(generated:0)
    at static root method.(Unknown Source)

My Groovy code snippet:

@groovy.transform.CompileStatic
class App {
    static void main(String[] args) {
    // some very simple Swing UI init here
    while (true) {
            time = LocalTime.now()
            label.text = " " + time.format("HH:mm:ss")
            Thread.sleep(1000)
        }
 }

My native-compile command line:
native-image --report-unsupported-elements-at-runtime --initialize-at-build-time --trace-class-initialization=java.lang.Thread --no-fallback -H:ConfigurationFileDirectories=conf/ -cp ".;%GROOVY_HOME%lib\groovy-4.0.9.jar;%GROOVY_HOME%lib\groovy-datetime-4.0.9.jar" displaytime.App

My native-agent command line (works well):
java -agentlib:native-image-agent=config-output-dir=conf/ displaytime.App

:: Cordialement/Best regards, Erix


Le lun. 16 oct. 2023 à 18:48, Gilles DUBOSCQ <gilles.m.duboscq at oracle.com<mailto:gilles.m.duboscq at oracle.com>> a écrit :
Hi Eric,
Yes Thread.sleep() should work in GraalVM native-image.
It would help to know what kind of error you're seeing exactly.
 Gilles
________________________________
From: Eric Samson via GraalVM-Users <graalvm-users at oss.oracle.com<mailto:graalvm-users at oss.oracle.com>>
Sent: Monday, 16 October 2023 18:38
To: graalvm-users at oss.oracle.com<mailto:graalvm-users at oss.oracle.com> <graalvm-users at oss.oracle.com<mailto:graalvm-users at oss.oracle.com>>
Subject: [External] : [graalvm-users] java.lang.Thread.sleep() supported in GraalVM?

Hi everyone,

I'm totally new to GraalVM.
I've set up the whole environment for Windows this morning.

I'm now trying to natively compile a Groovy app (with neither dynamic nor meta programming).
I've been able to compile it and use the native-image-agent to generate the /conf files.

Now I'm trying to generate the executable and I get an error message due to the fact I'm using Thread.sleep(x) in my main loop.

Sleep is a static method of the Thread class. I understand there must be some kind of conflicts between the class being initialized at build-time or runtime. But I'm not sure how to fix it. I've spent a lot of time in the GraalVM documentation and several Web sites (stackOverflow, etc.) but could not find a good example of how to use Thread.sleep in GraalVM.

Sorry for the noob question. Any hint would be greatly appreciated. Thanks in advance.
Best regards, Erix.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://oss.oracle.com/pipermail/graalvm-users/attachments/20231016/b46019be/attachment-0001.html>


More information about the GraalVM-Users mailing list