Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » thread wait()/notify() - updating label
thread wait()/notify() - updating label [message #440416] Fri, 30 July 2004 01:25 Go to next message
Eclipse UserFriend
Originally posted by: ryusasai.hotmail.com

Someone kind and sharp, please help me. I am in a serious trouble.
Basically what I want to do is when I click a button,
a thread starts and keep updating numbers on Labels,
and when I click the button again, the thread suspends,
and click again, it resumes and keep updating the numbers
and so on.

Here is my code, and it's SWT application.
Please help. Thank you.

//<Ezplus.java>
public class Ezplus {
private static Thread numthr;
private static boolean numthrNotStarted = true;
private static boolean numthrSuspended;

public static void main(String[] args) {
button101_2.addSelectionListener(new SelectionAdapter() {
public synchronized void widgetSelected(SelectionEvent e) {
if (numthrNotStarted) {
numthr.start();
numthrNotStarted = false;
numthrSuspended = false;
return;
}
numthrSuspended = !numthrSuspended;
if (!numthrSuspended)
notify();
}
});

// create thread that manages numbers
Vector conts = new Vector();
conts.addElement(ryXnum); // label
conts.addElement(ryYnum); // label
numthr = new Thread(new ManageNumbers(conts));
}
public static synchronized boolean getnumthrSuspended() {
return numthrSuspended;
}
public static void writeNums(CLabel c, String s) {
c.setText(s);
}
}

//<ManageNumbers.java>
public class ManageNumbers implements Runnable {
private Vector conts;
private CLabel curr;
private int id;
private int sz;

ManageNumbers(Vector controls) {
conts = controls;
sz = conts.size();
}

public void run() {
while (true) {
try {
Thread.currentThread().sleep(1000);
if (Ezplus.getnumthrSuspended()) {
synchronized(this) {
while (Ezplus.getnumthrSuspended()) {
wait();
}
}
}
} catch (Exception ex){}
for (id=0; id < sz; ++id) {
curr = (CLabel)conts.get(id);
curr.getDisplay().asyncExec(new UpdateThread());
}
}
}
class UpdateThread implements Runnable {
public void run(){
switch (id) {
case 0:{
Ezplus.writeNums(curr,
Long.toString(
System.currentTimeMillis()).substring(6,13));
} break;
case 1:{
Ezplus.writeNums(curr,
new String(" " +
Long.toString(
System.currentTimeMillis()).substring(7,13)));
} break;
default:{
} break;
}
}
}
}
Re: thread wait()/notify() - updating label [message #440444 is a reply to message #440416] Fri, 30 July 2004 16:25 Go to previous message
Eclipse UserFriend
Originally posted by: richkulp.NO.SPAM.us.ibm.com

You didn't explain what went wrong, but I did see one problem:

> button101_2.addSelectionListener(new SelectionAdapter() {
> public synchronized void widgetSelected(SelectionEvent e) {
> if (numthrNotStarted) {
> numthr.start();
> numthrNotStarted = false;
> numthrSuspended = false;
> return;
> }
> numthrSuspended = !numthrSuspended;
> if (!numthrSuspended)
> notify();
> }
> });
>
> public class ManageNumbers implements Runnable {
....
> public void run() {
> while (true) {
> try {
> Thread.currentThread().sleep(1000);
> if (Ezplus.getnumthrSuspended()) {
> synchronized(this) {
> while (Ezplus.getnumthrSuspended()) {
> wait();
> }
> }
> }
> } catch (Exception ex){}
> for (id=0; id < sz; ++id) {
> curr = (CLabel)conts.get(id);
> curr.getDisplay().asyncExec(new UpdateThread());
> }
> }

If you are expecting the notify() in widgetSelected to break the wait()
in the thread it won't because you are not notifying the object that is
waiting.

The notify will break any waits that are waiting on the SelectionAdapter
(since that is who you are calling the notify() against), while the
wait() is waiting on notifies against the ManageNumbers runnable. They
are two different objects.

You need to make the ManageNumbers runnable instance available in a
member so that the selection listener can access it. Then you need to
remove the synchronized from the widgetSelected method and instead put a
synchronized(manageNumbersRunnable) { } around the stuff instead, AND
you need to do manageNumbersRunnable.notify().

--
Thanks, Rich Kulp

Previous Topic:JFace WizardSelectionPage
Next Topic:new Shell with style SWT.NO_TRIM
Goto Forum:
  


Current Time: Sat Apr 27 00:55:03 GMT 2024

Powered by FUDForum. Page generated in 0.03647 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top