×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    7

    Thread issues....

    I`m developing a J2ME game for a college project.
    I need a function to run a certain time e.g. every tenth of a second and threads are the only way to do this, i`m told.

    but while I can create the thread in the class that extends from canvas with
    Thread myThread=new Thread(this);

    and then get the thread to run using
    myThread.start(); or myThread.run();

    and then get it to sleep with
    myThread.sleep(100);

    Is there anyway of getting the thread to run again itself after the sleep period is over or do you have to call the run() function every time you want it to run

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    Code:
    class MyCanvas extends Canvas implements Runnable {
      boolean running;
    
      // ...
    
      public void paint(Graphics g) {
        // ...
      }
      
      public void run() {
        while (running) {
          // ...
          try {
            Thread.sleep(100);
          }
          catch (Exception e) {
             // ...
          }
        }
      }
    
      void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
      }
    
      void stop() {
        running = false;
      }
    }
    shmoove

Posting Permissions

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