Hello, Viral.

It's known that there are some deadlocks in SWT_AWT bridge on Mac. Here's an 
example:
https://bugs.openjdk.java.net/browse/JDK-8020165

It would be useful if you also post the dump of an Appkit thread to identify 
your particular case.

The deadlock occurs because AWT is called on the main thread. So the main 
thread get blocked
on some internal AWT lock, while the EventDispatchThread is trying to perform a 
synchronous operation
on the main thread. It's not quite obvious how to fix that internally, because 
for the normal AWT application
we have a strict rule not to acquire locks on the Appkit thread. 

For now the workaround could be to wrap all the usages of AWT inside 
invokeLater (except the initial  
SWT_AWT.new_Frame(composite) call). This should fix your deadlock. 

Also, which version of SWT are you using? The newer versions are patched so 
that you could not set the 
embeddedFrameClass explicitly. That patch also contains additional changes in 
bridge initialization,
so the bridge does not work quite well with older versions. This could be 
another reason of your deadlock.

With best regards. Petr.

On 19.11.2013, at 2:51, Viral Barot <vba...@tibco.com> wrote:

> Accessing AWT frame from a thread causes the application to freeze when 
> executed with JRE 7. The AWT frame is created with SWT_AWT bridge.
> 
> Below is the test case in which call to frame.setVisible(true); from the AWT 
> event dispatching thread does not return.
> 
> Test Case:
> import java.awt.Button;
> import java.awt.Color;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import java.lang.reflect.InvocationTargetException;
> 
> import javax.swing.JOptionPane;
> import javax.swing.SwingUtilities;
> 
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.awt.SWT_AWT;
> import org.eclipse.swt.layout.GridLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.Composite;
> 
> public class Main {
> 
>  public static void main(String[] args) {
> 
> 
> 
> if(System.getProperty("os.name").toLowerCase().startsWith("mac")){
> SWT_AWT.embeddedFrameClass = "sun.lwawt.macosx.CViewEmbeddedFrame";
> }
> 
> 
>    Display display = new Display();
>    Shell shell = new Shell(display);
>    shell.setText("Shell");
>    shell.setSize(200, 200);
>    shell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLUE));
> 
> 
> 
>    final Composite composite = new Composite(shell, SWT.EMBEDDED);
>    GridLayout gridLayout = new GridLayout();
>    gridLayout.numColumns = 1;
>    composite.setLayout(gridLayout);
>    composite.setSize(200,200);
>    
> composite.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
> 
> 
> 
>    final java.awt.Frame frame  = SWT_AWT.new_Frame(composite);
> frame.setVisible(false);
> frame.setBackground(Color.red);
> frame.setSize(200, 200);
>    Button button = new Button("Submit");
>    button.addActionListener(new ActionListener() {
> 
> 
> @Override
> public void actionPerformed(ActionEvent e) {
> 
> 
> JOptionPane.showMessageDialog(null, "Hello");
> }
> });
>    frame.add(button);
>    frame.doLayout();
> 
> 
> 
>    try {
> SwingUtilities.invokeAndWait(new Runnable() {
> 
> 
> @Override
> public void run() {
> frame.setVisible(true);
> }
> });
> } catch (InterruptedException e1) {
> e1.printStackTrace();
> } catch (InvocationTargetException e1) {
> e1.printStackTrace();
> }
>    shell.open();
> 
> 
> 
>    while (!shell.isDisposed()) {
>      if (!display.readAndDispatch())
>        display.sleep();
>    }
>    display.dispose();
> 
> 
> 
>  }
> }
> 
> Below is the thread dump for this.
> 
> "AWT-EventQueue-0" prio=5 tid=0x00007f8d0b087000 nid=0x8703 runnable 
> [0x0000000195e40000]
>   java.lang.Thread.State: RUNNABLE
> at sun.lwawt.macosx.CCursorManager.nativeGetCursorPosition(Native Method)
> at sun.lwawt.macosx.CCursorManager.getCursorPosition(CCursorManager.java:54)
> at sun.lwawt.LWCursorManager.updateCursorImpl(LWCursorManager.java:79)
> at sun.lwawt.LWCursorManager.updateCursor(LWCursorManager.java:56)
> at sun.lwawt.LWComponentPeer.updateCursorImmediately(LWComponentPeer.java:852)
> at java.awt.Component.updateCursorImmediately(Component.java:3123)
> at java.awt.Component.show(Component.java:1626)
> - locked <0x0000000160c5b8c8> (a java.awt.Component$AWTTreeLock)
> at java.awt.Window.show(Window.java:1042)
> at sun.awt.EmbeddedFrame.show(EmbeddedFrame.java:179)
> at java.awt.Component.show(Component.java:1651)
> at java.awt.Component.setVisible(Component.java:1603)
> at java.awt.Window.setVisible(Window.java:1014)
> at Main$2.run(Main.java:60)
> at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:241)
> at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
> at java.awt.EventQueue.access$200(EventQueue.java:103)
> at java.awt.EventQueue$3.run(EventQueue.java:694)
> at java.awt.EventQueue$3.run(EventQueue.java:692)
> at java.security.AccessController.doPrivileged(Native Method)
> at 
> java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
> at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
> at 
> java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
> at 
> java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
> at 
> java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
> at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
> at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
> at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
> 
>   Locked ownable synchronizers:
> - None
> 
> While in the above test case if invokeLater is called instead of 
> invokeAndWait, the test case runs fine. But in my application the invokeLater 
> calls also freeze at same location.
> 
> This behavoir happens with SWT 3.7.2 as well as 4.3.
> 
> 
> 
> 

Reply via email to