×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Lithuania
    Posts
    14

    Question Help with synchronized threads

    Hello,

    Please help find my mistake - I have one thread, that starts location listener and other thread runTh(), which starts and waiting coordinates. My thread runTh() does not continue working, when locationUpdated() gets coordinates:
    Code:
    ..
     // Start querying GPS data :
                new Thread() {
    
                    public void run() {
                        locationProvider.setLocationListener(TrackMe.this, sec, -1, -1);
                        busy = true;
                    }
                }.start();
                runTh();
    ....
    ....
     public void runTh(){
            new Thread() {
                    public void run() {
                        synchronized (this) {
                           if(busy) {
                                try {
                                    gps2.setText("Thread waiting");
                                    wait();
                                    //do something
                                } catch (InterruptedException ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }                          
                    }
                }.start();
        }
    ...
    ...
    public synchronized void locationUpdated(LocationProvider provider, Location location) {
            if (location != null && location.isValid()) {
                statusDisplay.setText("Available\n");
                QualifiedCoordinates qc = location.getQualifiedCoordinates();
                gps = gps
                        + "Lat: " + qc.getLatitude() + "\n"
                        + "Lon: " + qc.getLongitude() + "\n";
                busy = false;
                notify();
      }
    }
    ...

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Help with synchronized threads

    If you're using LocationListener, you don't usually need to create any threads. Just get rid completely of the two threads you have. If the processing you want to do when you get GPS coordinates is quick, then do it in the event method. If it will take time, then start a new thread to do the processing in the event handler.

    Graham.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Lithuania
    Posts
    14

    Re: Help with synchronized threads

    I gave not exact my code, actually I have LocationListener in other package than runTh() method. So I want to start LocationListener and start runTh(), which must wait GPS coordinates and than open http connection and pass some information. I don't want to loop while(busy) in runTh() method also I need the way to terminate runTh() in wait() mode. Thats because I decided to do it with synchronized threads.

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Help with synchronized threads

    You're having problems because you're trying to solve the wrong problem. Instead of starting a thread, then making it wait until an event occurs, simply start the thread from the event!

    Code:
    ..
        // don't need any threads here!
        locationProvider.setLocationListener(TrackMe.this, sec, -1, -1);
    ....
    ....
     public void runTh(){
            new Thread() {
                    public void run() {
                        //do something
                    }
                }.start();
        }
    ...
    ...
    public synchronized void locationUpdated(LocationProvider provider, Location location) {
            if (location != null && location.isValid()) {
                statusDisplay.setText("Available\n");
                QualifiedCoordinates qc = location.getQualifiedCoordinates();
                gps = gps
                        + "Lat: " + qc.getLatitude() + "\n"
                        + "Lon: " + qc.getLongitude() + "\n";
                runTh();
      }
    }
    ...

Similar Threads

  1. Available memory and Threads
    By NevenS in forum Symbian
    Replies: 1
    Last Post: 2009-02-27, 08:29
  2. Replies: 0
    Last Post: 2008-09-22, 11:37
  3. Replies: 3
    Last Post: 2008-01-31, 23:54
  4. Message to everybody - STOP USING THREADS
    By hotcheese in forum Symbian
    Replies: 8
    Last Post: 2007-03-05, 12:18
  5. mixing AO, threads and sockets
    By mikfi in forum Symbian Networking & Messaging (Closed)
    Replies: 6
    Last Post: 2006-11-21, 23:19

Posting Permissions

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