×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    13

    Arrow Please please help me

    midp 2.0
    Java SE 5.0
    J2ME version 2.2

    Below is my trouble code... My trouble with the code is with the thread. the variable "scanning" is sett to TRUE in void Scan() and the last thing done in the run() method is setting scanning = FALSE... the Paint method will print out TRUE to the debug window if scanning is TRUE, but it never prints it - it doesn't print TRUE even if the run is busy in the FOR-loop either. "scanning" isn't written to the debug window either

    I've been looking into this problem for a couple of days and I got stuck... I think this error is related to synchronization but I honestly cannot find out what is causing this problem. So please tell me what I must change in my code to make it work...


    package com.test;

    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import javax.microedition.media.*;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.*;
    import javax.microedition.io.*;

    class testManager extends javax.microedition.lcdui.game.LayerManager implements Runnable
    {

    public boolean scanning = false;

    public testManager(int width, int height) throws Exception {
    }

    //Methods
    public void paint(Graphics g) {
    synchronized (this) {
    if (scanning) {
    System.out.println("TRUE");
    }
    }
    paint(g, 0, 0);
    }


    public void advance() {
    synchronized (this) {
    if (scanning) {
    strInfoText = "scanning";
    System.out.println("scanning");
    }
    }
    }

    public void Scan() {
    strInfoText = "helloworld3";
    synchronized (this) {
    scanning = true;
    }
    Thread thread = new Thread(this);
    thread.start();

    }


    public void run() {
    for (int i=0; i<10; i++) {
    System.out.println("TESTING " + i);
    }
    System.out.println("scan done");

    synchronized (this) {
    scanning = false;
    }
    }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    13

    Re: Please please help me, thread problems

    I've done a few changes to the 'run' method and still
    I have the same problem, I've been testing this code for a while and it
    seems like while the 'run' method is running the 'paint' method is called
    many times. the 'paint' method reads 'scanning' as FALSE, while 'run' is
    running and 'scanning' have got the value TRUE inside 'run'....


    package com.test;

    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import javax.microedition.media.*;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.*;
    import javax.microedition.io.*;

    class testManager extends javax.microedition.lcdui.game.LayerManager
    implements Runnable
    {
    public boolean scanning = false;
    public testManager(int width, int height) throws Exception {
    }

    //Methods
    public void paint(Graphics g) {
    synchronized (this) {
    if (scanning) {
    System.out.println("TRUE");
    }
    else {
    System.out.println("FALSE");
    }
    }
    paint(g, 0, 0);
    }

    public void Scan() {
    strInfoText = "helloworld3";
    synchronized (this) {
    scanning = true;
    }
    Thread thread = new Thread(this);
    thread.start();
    }

    public void run() {
    synchronized (this) {
    if (scanning) {
    for (int i=0; i<10; i++) {
    System.out.println("TESTING " + i);
    }
    System.out.println("scan done");
    scanning = false;
    }
    }
    }
    }


    This code generate this output to the debug window....
    The FALSE are written by the paint method.....

    FALSE
    FALSE
    TESTING 0
    TESTING 1
    TESTING 2
    TESTING 3
    TESTING 4
    TESTING 5
    TESTING 6
    TESTING 7
    TESTING 8
    TESTING 9
    scan done
    FALSE
    FALSE
    FALSE
    FALSE
    FALSE


    But when I modified run, removed this statement:
    scanning = false;

    Now the result gets as shown below

    FALSE
    FALSE
    FALSE
    FALSE
    TESTING 0
    TESTING 1
    TESTING 2
    TESTING 3
    TESTING 4
    TESTING 5
    TESTING 6
    TESTING 7
    TESTING 8
    TESTING 9
    scan done
    TRUE
    TRUE

    In my humble opinion this means that this statement :
    synchronized (this) {
    scanning = true;
    }
    works okay....

    BUT, while the 'run' method is running the 'paint' method is called many
    times. the 'paint' method reads 'scanning' as FALSE, while 'run' is running
    and 'scanning' have got the value TRUE inside 'run'....

    What must I change so that the 'paint' reads the value 'scanning' have while
    'run' is running, I mean the value the value 'scanning' have within the synchronized block of 'run'?

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    13

    Re: Please please help me

    public void run() {
    synchronized (this) {
    if (scanning) {
    for (int i=0; i<10; i++) {
    System.out.println("TESTING " + i);
    }
    System.out.println("scan done");
    scanning = false;
    }
    }
    }

    It seems like 'scanning = false;' get executed before the for-loop. To me this is very strange behavior, 'scanning = false;' is under the for-loop and I thought that the for-loop would be executed before the 'scanning = false;' statment

    If I remove the statment 'scanning = false;' from 'run' then it works, I mean that while the for-loop is executing the 'paint' method reads the value 'scanning' has before set to FALSE, in other words it reads the value TRUE

    But if the statment 'scanning = false;' is removed the program can't tell when the thread is done executing...I want 'scanning' to be set back to FALSE when the for-loop is finished OR when the thread is done executing

    maybe using wait(), notify() or notifyAll() can help???, I'm not sure how to apply them....

    I'm not sure how to fix this so I would appreciate any tips helping me solve this problem

    Audun

  4. #4
    (Retired) Nokia Developer Admin.
    Join Date
    Jan 2006
    Location
    Michigan
    Posts
    4,664

    Re: Please please help me

    Try this

    public void run() {
    synchronized (this) {
    if (scanning) {
    for (int i=0; i<10; i++) {
    System.out.println("TESTING " + i);
    }
    System.out.println("scan done");
    }
    scanning = false;
    }
    }

    it looks like an optimization

    Ron

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    13

    Re: Please please help me

    Hey!

    Thanks for your suggestion, but it didn't fix it... I think however that this bug is related to this 'run' taking all processing power while it get executed, leaving no room for other threads... So I thought inserting a Thread.yield() into the script would fix it.... but no, it didn't solve it... But maybe I'm not using Thread.yield() correctly here???

    Here is a new version of 'run' with the Thread.thread() statment:
    public void run() {
    boolean ecopy;
    synchronized (this) {
    ecopy = scanning;
    }
    if (ecopy) {
    for (int i=0; i<10; i++) {
    System.out.println("TESTING " + i);
    Thread.yield();
    }
    System.out.println("scan done");
    synchronized (this) {
    scanning = false;
    }
    }
    }
    }

Posting Permissions

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