Results 1 to 6 of 6
  1. #1
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default How to keep a thread running

    I'm having a problem with a thread I'm trying to use.
    The issue is that I start a thread, but it dies, and then when I run the run() method of the class FireThread during mousePressed events the entire game freezes up because firethread is sleeping for 5000 milliseconds.


    My question is how to keep this second thread alive so it can sleep without interrupting the game.



    Java Code:
    import javax.swing.ImageIcon;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.util.ArrayList;
    
    
    public class Player
    {
    
      public boolean standingUp = true;
      public int x = 40;
      public int y = 365;
      public int movex;
      public int movey = -5;
      public ArrayList<Bullet> list;
      public int v = 0;
      public ArrayList second;
      public Enemy alien;
      public FireThread firethread;
      public boolean notFiring;
    
      Player()
      {
        list = new ArrayList<Bullet>();
        second = new ArrayList();
        alien = new Enemy();
        firethread = new FireThread();
        firethread.start();
      }
    
      public void fire()
      {
        list.add(new Bullet(x+image.getWidth(null), y + 20));
        new Audio().playAudio();
      }
    
      public void mousePressed(MouseEvent e)
      {
        notFiring = firethread.isFiring();
        if(notFiring)
        {
          fire();
          firethread.run();
        }
      }
    
    
    
      class FireThread extends Thread
      {
        public boolean NotFiring;
    
        FireThread()
        {
          NotFiring = true;
        }
    
        public void run()
        {
          NotFiring = false;
          try
          {
            sleep(5000);
          }
          catch(InterruptedException e)
          {
    	e.printStackTrace();
            System.exit(0);
          }
          NotFiring = true;
        }
    
        public boolean isFiring()
        {
          return NotFiring;
        }
      }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    sure.
    don't do the fireThread.run() inside the mousepressed.

    instead, modify FireThread to run forever.

    Java Code:
    class FireThread extends Thread
      {
        public boolean NotFiring;
    
        FireThread()
        {
          NotFiring = true;
        }
    
        public void run()
        {
          NotFiring = false;
          try
          {
           while (true) { // i guess we could test while (!NotFiring) here too, and have an external thing setNotFiring(true) later to break the loop. here I am just going to call interrupt().
            sleep(50); // just use some small sleep amount of time so it doesnt use high cpu utilization.
    }  
          }
          catch(InterruptedException e)
          {
        //	e.printStackTrace();
        //    System.exit(0);
          }
          NotFiring = true;
        }
    
        public boolean isFiring()
        {
          return NotFiring;
        }
      }
    and then in your main application, when you want the thread to stop, invoke a firingThread.interrupt() to have it exit.

  3. #3
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    That kind of helps, but I'm more looking to keep the thread alive, but more like the idea that the thread stays alive, but runs through its run() method from start to finish more than once. The point of this thread is to make the player wait for a "reload" time after firing a gun for a certain amount of seconds, so I have this thread set "NotFiring" to false, so when a person tries to fire while the thread is executing, they can't shoot again. Right before the run() method finishes, NotFiring is set back to true so a player can fire again, but I want to know how I can keep this thread I'm using alive so it can run through its run() method many times, because a player will want to shoot more than once.

  4. #4
    nothing1010 is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by travishein View Post
    and then in your main application, when you want the thread to stop, invoke a firingThread.interrupt() to have it exit.
    why don't you use method stop's Thread? What different between two method interrupt and stop?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nothing1010 View Post
    why don't you use method stop's Thread? What different between two method interrupt and stop?
    You did read the API documentation for that method didn't you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    nothing1010 is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You did read the API documentation for that method didn't you?

    kind regards,

    Jos
    I will try to read in the future.
    Thanks you so much!

Similar Threads

  1. Is it possible to keep a thread running?
    By Fortu in forum Threads and Synchronization
    Replies: 5
    Last Post: 01-07-2011, 06:36 PM
  2. Running a thread for a fixed period of time
    By ronyb85 in forum Threads and Synchronization
    Replies: 5
    Last Post: 12-16-2010, 07:26 PM
  3. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 12:57 PM
  4. need info on running thread during a particular time interval alone
    By karthikeyan_raju in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-06-2009, 02:40 AM
  5. Running a thread on another appilcation
    By Charlie in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 12:28 AM

Tags for this Thread

Posting Permissions

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