即使在vm args中使用XstartOnFirstThread也无效的线程访问 [英] Invalid Thread Access even with XstartOnFirstThread in vm args

查看:261
本文介绍了即使在vm args中使用XstartOnFirstThread也无效的线程访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个胚胎Java Web Start应用程序,只有一个类。它在Windows和Linux上运行,但在Mac OS X上遇到了可怕的无效线程访问错误。我意识到这已在其他地方处理过。我花了整整两天时间在互联网上搜索并实施了所有解决方案,但问题仍然存在。

I have an embryonic Java Web Start application with a single class. It runs on Windows and Linux but gets the dreaded Invalid Thread Access error on Mac OS X. I realise that this has been dealt with elsewhere. I have spent two full days scouring the Internet and have implemented all the solutions, but the problem persists.

我的理解是,必须从主线程调用SWT这是这种情况。如果我错了,请纠正我。

My understanding is that calls to SWT must be made from the main thread which is the case here. Correct me if I am wrong in that.

我将在下面发布3个片段,应用程序的源代码,jnlp文件的相关部分以及错误消息Mac。问题是在最后。

I will post 3 snippets below, the source code of the application, the relevant part of the jnlp file and the error message on the Mac. The question is at the end.

JAVA来源代码

package client;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class AccountWindow {
 public static void main(String[] args) {
  Display display = new Display(); **// error occurs here**
  Shell shell = new Shell(display); shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
  display.dispose();
 }
}






JNLP SNIPPET


JNLP SNIPPET

<resources os="Mac\ OS\ X" arch="x86_64">
    <j2se version="1.5+" java-vm-args="-XstartOnFirstThread" />
    <nativelib href="swt-4.2-cocoa-macosx-x86_64.jar" />
</resources>






ERROR MESSAGE


ERROR MESSAGE

org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.error(Unknown Source)
    at org.eclipse.swt.widgets.Display.createDisplay(Unknown Source)
    at org.eclipse.swt.widgets.Display.create(Unknown Source)
    at org.eclipse.swt.graphics.Device.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at org.eclipse.swt.widgets.Display.<init>(Unknown Source)
    at client.AccountWindow.main(AccountWindow.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.javaws.Launcher.executeApplication(Launcher.java:1550)
    at com.sun.javaws.Launcher.executeMainClass(Launcher.java:1488)
    at com.sun.javaws.Launcher.doLaunchApp(Launcher.java:1299)
    at com.sun.javaws.Launcher.run(Launcher.java:114)
    at java.lang.Thread.run(Thread.java:637)






请注意

- 发布于 http://www.eclipse.org/swt/faq.php#javawebstart 不适用,因为在您可以调用之前你需要一个显示器。当我尝试创建显示时,会发生错误。

- 我使用JaNeLa验证jnlp文件并且没有红色错误。

- <资源os =Mac \ OS \ Xarch =i386>正在解释因为正在加载正确的swt库。

- 您可以在 http://thelinkjuicer.com/gannonline/client.jnlp


PLEASE NOTE
- The display.syncExec solution posted at http://www.eclipse.org/swt/faq.php#javawebstart is not applicable because before you can invoke it you need a display. The error here happens when I try to create the display.
- I have used JaNeLa to validate the jnlp file and there are no red errors.
- <resources os="Mac\ OS\ X" arch="i386"> is being correctly interpreted because the correct swt library is being loaded.
- You can reproduce the error at http://thelinkjuicer.com/gannonline/client.jnlp

现在问题是什么?
任何人都可以看到导致错误的源代码或jnlp片段中的任何内容吗?
b $ b次要问题:如何判断-XstartOnFirstThread参数是否存在VM实际上是在读它?

AND NOW THE QUESTION
Can anyone see anything in the source code or the jnlp snippet that would cause the error?
Secondary question: how can you tell if the -XstartOnFirstThread argument is actually being read by the VM?

推荐答案

显然,你的主方法没有在主线程上执行。您可以在堆栈跟踪中看到启动器实际上是在另一个线程中启动的,然后启动器仅间接调用 main 。不幸的是,这只是诊断,我不确定解决方案。我做了类似的事情(通过Java Web Start的SWT应用程序),但我不记得我们如何解决这个问题,如果有的话。

Clearly, your main method is not being executed on the main thread. You can see in the stack trace that the launcher is actually started in another thread, and then the Launcher only indirectly calls main. This is unfortunately just the diagnostic, I am not sure about the solution. I have done a similar thing (SWT app through Java Web Start), but I can't remember how we solved this, if at all.

检查后 com.sun.javaws.Launcher 源代码,目前尚不清楚如何使其工作。 Launcher.launch 方法启动一个新线程,在该线程中执行 main 方法。您可以按照代码重新创建您正在获得的精确堆栈跟踪。

After checking the com.sun.javaws.Launcher source code, it is quite unclear how this could be made to work. The Launcher.launch method starts a new thread within which your main method is executed. You can follow the code to recreate the exact stacktrace you are getting.

主要入口点显示主线程在启动后很快就会死亡。

The main entry point of Java Web Start shows that the main thread dies soon after starting.

我挖出了一些东西:在这个Eclipse错误报告建议问题可能与此有关:

I dug something out: in this Eclipse bug report it is suggested that the problem could be related to this:

<resources>
  <j2se version="1.4+" />
  <jar href="client.jar" />
</resources>

解析器从这里获取j2se规范并忽略后面更具体的规范。尝试删除< j2se ... 行。

The parser takes the j2se spec from here and ignores the later, more specific ones. Try removing the <j2se... line.

现在我从这里中挖掘出来了:

Now I dug this up from here:

com.apple.concurrent.Dispatch.getInstance().getNonBlockingMainQueueExecutor().execute(
  new Runnable() { public void run() {
      final Display display = Display.getDefault(); 
      while (!display.isDisposed()) {
        if (!display.readAndDispatch())
          display.sleep();
      }
});

这实际上听起来像是可行的。它完全按照我在下面的评论中描述的那样:通过专门为此目的而实施的机制修补主线程。尝试根据您的需要进行调整。你可能甚至不需要-XstartOnFirstThread。

This actually sounds like something workable. It does exactly what I described in my comment below: patches into the main thread through a mechanism specifically put in place for this purpose. Try to adapt this to your need. You may not even need -XstartOnFirstThread with this.

我终于找到了我的旧SWT-JWS项目。它有这个:

I finally found my old SWT-JWS project. It's got this in it:

<resources os="Mac OS X" arch="x86_64">
  <j2se version="1.6+" java-vm-args="-XstartOnFirstThread"/>
  <jar href="swt-cocoa-macosx-x86-64-3.6.2.jar" />
</resources>

并且有效。它没有默认的 j2se 元素,此元素在特定于OSX的条目中显示为

and it works. It has no default j2se element, this element appears only in the OSX-specific entry.

这篇关于即使在vm args中使用XstartOnFirstThread也无效的线程访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆