Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Component's setCursor problem from arbitrary thread (JDK 1.4)



On Wed, 30 July 2003 18:52:10 -0400 Dmitry Markman <email@hidden> wrote:

if I call setCursor from arbitrary thread (not event thread) setCursor doesn't work

If setCursor itself is threaded, perhaps there is a missed notification or an early notification in the AWT implementation. Paul Hyde's book (chapter 8) provides a nice description:

Missed Notification
-------------------
"A missed notification occurs when threadB tries to notify threadA, but threadA is not yet waiting for the notification. ... This missed notification scenario can be quite dangerous. ... To fix [a missed notify], a boolean indicator variable should be added. The indicator is only accessed and modified inside synchronized blocks."

Early Notification
------------------
"If a thread is notified while waiting, but the condition the thread is waiting for has not yet been met, the thread has received an early notification. An early notification can also occur if the condition is briefly met but quickly changes so it's no longer met. This might sound strange, but early notification can happen due to subtle errors in the code (generally when an 'if' is used instead of a 'while').

A cosmetically modified version of your test case (different cursor, same behavior - works under 1.3 but fails under 1.4):

import java.awt.*;
import java.awt.event.*;

public class CursorTestAWT extends Frame
{
static CursorTestAWT frame;
static Button threadButton = new Button("From Thread");
static Button actionButton = new Button("From actionPerformed");

CursorTestAWT(String name)
{
super(name);
}

public static void main(String args[])
{
frame = new CursorTestAWT("Cursor Test");
frame.setSize(300,300);
frame.setLayout(new BorderLayout());

threadButton.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
Thread t = new Thread
(
new Runnable()
{
public void run()
{
settingCursors(threadButton); // HERE IT DOESN'T WORK
print("ActionEvent From Thread");
}
}
);
t.start();
}
}
);

actionButton.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
settingCursors(actionButton); // HERE IT DOES WORK
print("ActionEvent From actionPerformed");
}
}
);

frame.add(threadButton,BorderLayout.NORTH);
frame.add(actionButton,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}

static void settingCursors(Component c)
{
// c.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
c.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
try
{
print("Sleeping 2 seconds...");
Thread.sleep(2000);
print("...woke up!");
}
catch (Exception ignored) {}
c.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}

private static void print(String msg)
{
System.out.println("Thread " + Thread.currentThread().getName() + ": " + msg);
}

}
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.