Multithreading - Games problem

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

Multithreading - Games problem

Postby raagaas » Sat Apr 21, 2007 11:45 am

I am working on a multiplayer game ....

Here is what the structure of my program(s) luks like ....

I have the BlueToothMidlet which implements Runnable .... creates server/connects to a server according to the choice provided .... as soon as the client and server are ready .... i do Display.getDisplay(this).setCurrent(MyGameCanvas) ....and the canvas does all the initial work each player needs to do .... after each player is done with his work ... he should tell the other player that he is done ....

now the run method of BlueToothMidlet is always running .... i created another thread (Thread t ) to send and receives messages from client/server..... and when i want to send/receive message i do "t.start()" so that each player knows that the other player is "done" ... but i am getting an "IllegalThreadStateException" in the BlueToothMidlet's run method ...everytime !!

Also i want to send messages as soon as each player is done with his turn to play ....

What might be the problem ??
raagaas
 
Posts: 4
Joined: Sat Apr 21, 2007 8:57 am

Postby DevelopmentTeam » Sat Apr 21, 2007 11:54 am

This error is thrown usually when more than one thread is created and access the same variable. Also first make sure if there is only one bluetooth thread running everytime otherwise you need to make the other thread wait untill the first one finishes. Please let me know if you are testing on a device or emulator... if possible put your bluetooth code here...
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Postby raagaas » Sat Apr 21, 2007 12:10 pm

am using an emulator for now ....
Actually yes .... both the threads access the variable "sendStr" ....

i am posting the run() method of the BlueToothMidlet() ... the ClientServer class performs all the BlueTooth Connection(s) ... sending and receiving messages ....

Code: Select all
     public void run()
    {
       
       while (gameAlive)
       {
         try  {
            if (clientServerObj == null) {
               clientServerObj = new ClientServer();
               t = new Thread() {
                  public void run() {
                     clientServerObj.SendMessages(sendStr);
                      rcvStr = clientServerObj.RecieveMessages();
                     if (rcvStr.equals("CHANCE_DONE"))
                        remoteGameState = rcvStr;
                     return;
                  }
               };
               
               
               if (clientServerObj.isServerConnected()) {
                  if (myCanvas == null)
                     myCanvas = new MyGameCanvas(this, Display
                           .getDisplay(this));
                  Display.getDisplay(this).setCurrent(myCanvas);
                                 
               }
            }
            if (clientServerObj.isClientConnected()) {
               
               if (myCanvas == null)
                  bCanvas = new MyGameCanvas(this, Display
                        .getDisplay(this));
               Display.getDisplay(this).setCurrent(myCanvas);
               
            }

            
            if (myCanvas.getGameState() == "CHANCE_DONE") {
   
               sendStr = "CHANCE_DONE";
               
               waitPlayer.append("Waiting for other player....");
               Display.getDisplay(this).setCurrent(waitPlayer);
               
               myCanvas.setGameState("BEGIN_GAME");
            }
            if (myCanvas.getGameState() == "BEGIN_GAME"
                  && remoteGameState == "CHANCE_DONE") {
               System.out.println("Lets Begin the Game !!");
            }
            
            
            t.start();

         } catch (Exception ex) {
            ex.printStackTrace();
         }
      }
    }
raagaas
 
Posts: 4
Joined: Sat Apr 21, 2007 8:57 am

Postby raagaas » Sat Apr 21, 2007 2:43 pm

am eagerly waitin for the reply ... :( :(
is nebody thr ? :cry:
raagaas
 
Posts: 4
Joined: Sat Apr 21, 2007 8:57 am

Postby DevelopmentTeam » Sat Apr 21, 2007 3:31 pm

Better write a seperate class for bluetooth thread with runnable implementation. And make the function syncronized so that no 2 thread access the function at the same time.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Postby DevelopmentTeam » Sat Apr 21, 2007 3:32 pm

For example see the httpconnection sample code here
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Postby DevelopmentTeam » Sat Apr 21, 2007 3:34 pm

the illegalstateexception will occur when you are redeclaring the same thread before the thread is ended...
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Postby raagaas » Sat Apr 21, 2007 5:27 pm

thanx :D .... i created another class like the one u showed in the above code and it worked :) ... yipppee :wink:
raagaas
 
Posts: 4
Joined: Sat Apr 21, 2007 8:57 am

Postby DevelopmentTeam » Sun Apr 22, 2007 4:36 am

Always when you use Bluetooth/Httpconnection/file access/Any such device oriented operations make sure that you only have one thread doing the same operation, most devices dont allow 2 connections at the same time. So it is always safe to ensure this...
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 2 guests

cron