×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Finland
    Posts
    51

    Series 40 emulators /Thread.interrupt() causes exception outside sleep() block

    I get an interrupt exception if I call interrupt()-method for other thread that is not sleeping. That is againts Java principles.
    This happens in all Series 40 emulators but not in Series 60 emulators.

    Has anyone else faced this problem ?

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    As far as I know interrupt has been deprecated since Java 1.1 or something. It doesn't even appear on the MIDP docs. I wouldn't use it. What do you need it for?

    shmoove

  3. #3
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Finland
    Posts
    51
    Do you mean stop-method ?
    It is depreceated and should not be used.

    Interrupt-method only wakes a sleeping thread (or a waiting thread). InterruptedException is a checked exception but in Series 40 emulators it seems to be inherited from the runtime exception which is not checked.

    I have a thread that is in a loop and sleeps for 1000 ms. If a user presses a button I want to wake this thread immediately by interrupting it, otherwise the user should wait 1000 ms and it makes the UI non responsive. In Series 60 emulators and also in Nokia 6600 device the same code works fine.
    Last edited by kakoskin; 2005-05-29 at 09:41.

  4. #4
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Yes, I think I had stop in mind. But I'm pretty sure interrupt() shouldn't be used either.

    For what you are describing you should use wait() instead of sleep() and notify() instead of interrupt().

    shmoove

  5. #5
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Finland
    Posts
    51
    One should not use stop, suspend and resume as they are depreceated.

    However interrupt method provides an easy way to interrupt a sleeping thread.

    If I used wait and notify mechanism then I should use synchronized block which results to a small overhead (acquiring and releasing a lock).

    In my program I don't need to protect any resources using locks, I just want to interrupt a sleeping thread and Nokia implementation is not working as it should.

    You may want to try interrupting a thread in J2SE, it does not throw a non checked interrupted exception.

  6. #6
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Finland
    Posts
    51
    EDIT: The problems does NOT occur if I use wait and notify.
    Last edited by kakoskin; 2005-05-29 at 16:56.

  7. #7
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Finland
    Posts
    51
    To reproduce the problem:
    class: Test.java
    class: GUITest.java
    How: Start application and press Interrupt command.

    http://koti.mbnet.fi/kakoskin/InterruptTest.jad
    http://koti.mbnet.fi/kakoskin/InterruptTest.jar

    ---------------------------------------------------------------------------------------

    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;

    public class Test extends MIDlet {

    protected void startApp() throws MIDletStateChangeException {
    GUITest test = new GUITest("Testing");
    Display.getDisplay(this).setCurrent(test);
    }

    protected void pauseApp() {}
    protected void destroyApp(boolean p1) throws MIDletStateChangeException {}

    }

    import java.lang.*;
    import javax.microedition.lcdui.*;

    public class GUITest extends Form implements CommandListener, Runnable {
    private Command cmdInterrupt;
    private Thread thrWorker;

    public GUITest(String p1) {
    super(p1);
    this.cmdInterrupt = new Command("Interrupt",Command.SCREEN,1);
    this.addCommand(this.cmdInterrupt);
    this.setCommandListener(this);
    this.thrWorker = new Thread(this);
    this.thrWorker.start();
    }

    public void run() {

    while(true) {
    this.append(new java.util.Date().getTime() + "\n");
    try {
    Thread.currentThread().sleep(3000);
    }
    catch (InterruptedException ex) {
    this.append("interrupted\n");
    }
    }
    }

    public void commandAction(Command command, Displayable displayable) {
    this.thrWorker.interrupt();
    }
    }

  8. #8
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    Interrupt() is not included in CLDC 1.0 but only in CLDC 1.1.

    Is that your problem?

  9. #9
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Finland
    Posts
    51
    Good point, thanx!
    So I need to use wait and notify for CLDC 1.0.

    How come the interrupt method still works in actual Nokia 6600 device ??

  10. #10
    Regular Contributor
    Join Date
    Mar 2005
    Posts
    249
    I have no answer to that.
    I think that's anybody's best guess

    Cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×