Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[java] how to do J2ME Thread!?

Started by Thanhda Apr 9, 2005 at 7:03 PM 4 replies 4.9k views
Original Post
Thanhda
Thanhda
just wondering how to do j2me threading? like a timer in milliseconds, i see things everywhere but they all have a global timer. i want something that i can do something like this

g.drawImage(IMG,x,10);
thread.sleep(300);
x+=10;
g.drawImage(IMG,x,10);
thread.sleep(100);

this is the applet code, i want to convert it to J2ME or http://www.dgp.toronto.edu/~mjmcguff/learn/java/06-threads/

import java.applet.*;
import java.awt.*;

public class Threads1 extends Applet implements Runnable {

   int width, height;
   int i = 0;
   Thread t = null;
   boolean threadSuspended;

   // Executed when the applet is first created.
   public void init() {
      System.out.println("init(): begin");
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
      System.out.println("init(): end");
   }

   // Executed when the applet is destroyed.
   public void destroy() {
      System.out.println("destroy()");
   }

   // Executed after the applet is created; and also whenever
   // the browser returns to the page containing the applet.
   public void start() {
      System.out.println("start(): begin");
      if ( t == null ) {
         System.out.println("start(): creating thread");
         t = new Thread( this );
         System.out.println("start(): starting thread");
         threadSuspended = false;
         t.start();
      }
      else {
         if ( threadSuspended ) {
            threadSuspended = false;
            System.out.println("start(): notifying thread");
            synchronized( this ) {
               notify();
            }
         }
      }
      System.out.println("start(): end");
   }

   // Executed whenever the browser leaves the page containing the applet.
   public void stop() {
      System.out.println("stop(): begin");
      threadSuspended = true;
   }

   // Executed within the thread that this applet created.
   public void run() {
      System.out.println("run(): begin");
      try {
         while (true) {
            System.out.println("run(): awake");

            // Here's where the thread does some work
            ++i;  // this is shorthand for "i = i+1;"
            if ( i == 10 ) {
               i = 0;
            }
            showStatus( "i is " + i );

            // Now the thread checks to see if it should suspend itself
            if ( threadSuspended ) {
               synchronized( this ) {
                  while ( threadSuspended ) {
                     System.out.println("run(): waiting");
                     wait();
                  }
               }
            }
            System.out.println("run(): requesting repaint");
            repaint();
            System.out.println("run(): sleeping");
            t.sleep( 1000 );  // interval given in milliseconds
         }
      }
      catch (InterruptedException e) { }
      System.out.println("run(): end");
   }

   // Executed whenever the applet is asked to redraw itself.
   public void paint( Graphics g ) {
      System.out.println("paint()");
      g.setColor( Color.green );
      g.drawLine( width, height, i * width / 10, 0 );
   }
}


Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Thanhda
Thanhda
Quote:
Original post by capn_midnight
my initial reaction is to encourage you AVOID using threading in j2me at all costs.


okay, and why is that?
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
capn_midnight
capn_midnight
Quote:
Original post by Thanhda
Quote:
Original post by capn_midnight
my initial reaction is to encourage you AVOID using threading in j2me at all costs.


okay, and why is that?

The best you can really hope for with J2ME is bytecode interpretation (no JIT compiling). This basically means that you are stuck with the slowness of Java 1.1, on a machine that is slower than your PC from the days of Java 1.1. Therefore, thread management will be an extremely taxing task. The JRE already runs a couple of it's own threads, there's no need to bog it down even more. Whatever you are doing, you can do it without threads.
Thanhda
Thanhda
Quote:
Original post by capn_midnight
Quote:
Original post by Thanhda
Quote:
Original post by capn_midnight
my initial reaction is to encourage you AVOID using threading in j2me at all costs.


okay, and why is that?

The best you can really hope for with J2ME is bytecode interpretation (no JIT compiling). This basically means that you are stuck with the slowness of Java 1.1, on a machine that is slower than your PC from the days of Java 1.1. Therefore, thread management will be an extremely taxing task. The JRE already runs a couple of it's own threads, there's no need to bog it down even more. Whatever you are doing, you can do it without threads.


ah i see, well i've given up on threads anyway. and your right, i dont need them, but i just looking for a more efficiant way of doing it.
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
shmoove
shmoove
Quote:
Original post by capn_midnight
my initial reaction is to encourage you AVOID using threading in j2me at all costs.

A bazillion action games already released on cellphones, some of them quite good, would beg to differ.

The resolution won't be nanosecond accurate, and the performance of some phones is very crappy, but it's not like threads are completely useless.

@ Thanhda (if you're still listening): the MIDP tutorial here on Gamedev goes from the start right up to the point of moving an image around the screen which is what you're asking about.

[edit]
This is also worth mentioning:
There are at least two alternative ways of running a game loop with J2ME. One is using Timers and TimerTasks. The other one is using the Display.callSerially() method. Threads usually work better, but on some phones using one of the other methods (callSerially() especially) gives better results.
[/edit]

shmoove

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.