Results 1 to 9 of 9
Like Tree4Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By Norm
  • 1 Post By Fubarable

Thread: a problem with multithread!

  1. #1
    sina_oonline is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default a problem with multithread!

    hi to all friends and i hope that you guys accept me in your friendly social.
    i am programming a game that can play with 2 player first player should play with arrow keys and second with (wasd) keys..i must!! use tow thread for 2 players that every thread mnage a special player so 2 players can play cuncurrently..i write this code:
    Java Code:
    Thread re = new Thread() {
             public void run() {
                startGame.addKeyListener(new KeyListener() {
                   @Override
                   public void keyPressed(KeyEvent ee) {
                      System.out.println("thr 1 typed!!");
                      System.out.println("game current " + game.gameIsInCurrent);
                      System.out.println(ee.getKeyCode());
                      System.out.println("----------------------------");
                      if (game.gameIsInCurrent == true) {
                         if (ee.getKeyCode() == 38) {
                            game.move('u', 0);
                         }
                         if (ee.getKeyCode() == 40) {
                            game.move('d', 0);
                         }
                         if (ee.getKeyCode() == 39) {
                            game.move('r', 0);
                         }
                         if (ee.getKeyCode() == 37) {
                            game.move('l', 0);
                         }
                         if (ee.getKeyCode() == 10) {
                            game.move('f', 0);
                         }
                      }
                   }
    
                   @Override
                   public void keyReleased(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
    
                   @Override
                   public void keyTyped(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
                });
             }
          };
          re.start();
          Thread re2 = new Thread() {
             public void run() {
                startGame.addKeyListener(new KeyListener() {
                   @Override
                   public void keyPressed(KeyEvent ee) {
                      System.out.println("thr 2 typed!!");
                      System.out.println("game current " + game.gameIsInCurrent);
                      System.out.println(ee.getKeyCode());
                      System.out.println("----------------------------");
                      if (game.gameIsInCurrent == true) {
                         if (ee.getKeyCode() == 87) {
                            System.out.println("w clicked");
                            game.move('u', 1);
                         }
                         if (ee.getKeyCode() == 83) {
                            game.move('d', 1);
                         }
                         if (ee.getKeyCode() == 68) {
                            game.move('r', 1);
                         }
                         if (ee.getKeyCode() == 65) {
                            game.move('l', 1);
                         }
                         if (ee.getKeyCode() == 81) {
                            game.move('f', 1);
                         }
                      }
                   }
    
                   @Override
                   public void keyReleased(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
    
                   @Override
                   public void keyTyped(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
                });
             }
          };
          re2.start();
    but every time a buttun press all of 2 thread will active and in other words it has not any advantage for cuncurrent play
    Last edited by sina_oonline; 07-03-2011 at 04:35 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This isn't "the answer" to your question, but if you look at your posted code, can you honestly tell me that it's readable in its current form? Don't you think more folks would be willing to help you if you took the effort to make it readable? It wouldn't take much and in fact all you'd need to do would be shorten the indentations, something like so:

    Java Code:
          Thread re = new Thread() {
             public void run() {
                startGame.addKeyListener(new KeyListener() {
                   @Override
                   public void keyPressed(KeyEvent ee) {
                      System.out.println("thr 1 typed!!");
                      System.out.println("game current " + game.gameIsInCurrent);
                      System.out.println(ee.getKeyCode());
                      System.out.println("----------------------------");
                      if (game.gameIsInCurrent == true) {
                         if (ee.getKeyCode() == 38) {
                            game.move('u', 0);
                         }
                         if (ee.getKeyCode() == 40) {
                            game.move('d', 0);
                         }
                         if (ee.getKeyCode() == 39) {
                            game.move('r', 0);
                         }
                         if (ee.getKeyCode() == 37) {
                            game.move('l', 0);
                         }
                         if (ee.getKeyCode() == 10) {
                            game.move('f', 0);
                         }
                      }
                   }
    
                   @Override
                   public void keyReleased(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
    
                   @Override
                   public void keyTyped(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
                });
             }
          };
          re.start();
          Thread re2 = new Thread() {
             public void run() {
                startGame.addKeyListener(new KeyListener() {
                   @Override
                   public void keyPressed(KeyEvent ee) {
                      System.out.println("thr 2 typed!!");
                      System.out.println("game current " + game.gameIsInCurrent);
                      System.out.println(ee.getKeyCode());
                      System.out.println("----------------------------");
                      if (game.gameIsInCurrent == true) {
                         if (ee.getKeyCode() == 87) {
                            System.out.println("w clicked");
                            game.move('u', 1);
                         }
                         if (ee.getKeyCode() == 83) {
                            game.move('d', 1);
                         }
                         if (ee.getKeyCode() == 68) {
                            game.move('r', 1);
                         }
                         if (ee.getKeyCode() == 65) {
                            game.move('l', 1);
                         }
                         if (ee.getKeyCode() == 81) {
                            game.move('f', 1);
                         }
                      }
                   }
    
                   @Override
                   public void keyReleased(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
    
                   @Override
                   public void keyTyped(KeyEvent arg0) {
                      // TODO Auto-generated method stub
                   }
                });
             }
          };
          re2.start();
    Next of all, you wouldn't and in fact shouldn't use background threads to make Swing calls. Are you in fact absolutely required to use two background threads here? If so, can you post the actual requirements?
    Last edited by Fubarable; 07-03-2011 at 02:19 PM.
    sina_oonline likes this.

  3. #3
    sina_oonline is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks,honestly you are right.i was in hurry and realy didn't attention to indention of code.
    Scenario is that i have 2 player in play envoirment and i want to player 1 contorl with arrow keys and player 2 control with w&A&S&D buttuns so that two player can play cuncurrent for example if player 1 don't lift his finger of on of his keys ,player 2 can cuncurrent play with his buttun for example w buttun.
    my instructor said me that i should allocate on thead to each of two plaer because every threat can control its own player orders!
    i write above code but it don't work correctly because i realy don't allocate one thread to each player i allocate 2 shared thread between two player.
    i will be greatfull exteremely if one can help me
    Last edited by sina_oonline; 07-03-2011 at 04:37 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Which GUI library are you using for your GUI components, AWT (Frame or Applet) or Swing (JFrame or JApplet) as the answers may depend on this?
    sina_oonline likes this.

  5. #5
    sina_oonline is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    hi...
    Swing and more accurate JFrame

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    One more comment on your code's readability. Using numbers in your code:
    ee.getKeyCode() == 65 // ???? what key is this
    vs using a variable whose name documents what the value is:
    ee.getKeyCode() == KeyEvent.VK_A // This is the A key
    sina_oonline likes this.

  7. #7
    sina_oonline is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks alot but i think it has not or can not have any effect on answer in addition i said above that i used arrow keys and wasd keys.
    thank again

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sina_oonline View Post
    hi...
    Swing and more accurate JFrame
    Good as this is the more robust and flexible library of the two. I would suggest first of all using Key Bindings to get your keystrokes rather than a KeyListener as this will help you avoid some of the problems of focus that are inherent in KeyListeners. Using Key Bindings may look a bit daunting at first but if you give it a go, you'll find that they're not that hard. Tutorial: How to use Key Bindings

    Next, you won't need two threads to get the keystrokes and in fact can't do this since all user interaction in Swing must be done on one specific thread, the EDT or Event Dispatch Thread.
    sina_oonline likes this.

  9. #9
    sina_oonline is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    thanks... i keep trying with your new advises maybe i can ok that!

Similar Threads

  1. Java printing & multithread
    By aoi.onore in forum Threads and Synchronization
    Replies: 1
    Last Post: 09-19-2010, 05:23 PM
  2. simple question: A multithread situation
    By simorgh in forum Threads and Synchronization
    Replies: 1
    Last Post: 01-13-2010, 09:25 PM
  3. Multithread Chat server/client
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 09-19-2009, 01:22 AM
  4. Help design a program - multithread?
    By rushenas in forum New To Java
    Replies: 4
    Last Post: 01-23-2009, 07:41 AM

Posting Permissions

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