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

    Default Is it possible 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
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Threads are started with the start() method, the run() method is just logic for what the thread should do.

    So if you use the run() method will run from the current thread, the thread invoking the run() method.
    If you use the start() method, run() will run from a new thread, the thread owning the run() method.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

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

    Default

    Yes, but how would I go about keeping the thread with the run() method alive so that it can run its run() method more than once before it dies?

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Can make a for( ;; ) loop:

    Java Code:
    public void run()
    {
        for(;;)
        {
            //The code you have right now in run().
        }
    }
    to make it run forever.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #5
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Threads are expensive (especially to start one every time something fires!)... so it would be 10,000x faster to do it this way...

    Java Code:
      private long lastFireTime = 0;
      public void mousePressed(MouseEvent e)
      {
        long current = System.currentTimeMillis();
        long elapsed = current - lastFireTime;
        if(elapsed >= 5000 /*ms*/)
        {
          fire();
          lastFireTime = current;
        }
      }
    This will keep track of the last time a projectile was fired, and it will only fire one if the last one was >= 5 seconds ago (5000ms)

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    5

    Default

    Try to does threads only when actually needed.
    Thread are often a bad idea to use in games; they are expensive and a threads are not guaranteed to run as much as another.

    If you have two threads increasing one integer each, either using
    Java Code:
    for (;;)
        n++;
    or using
    Java Code:
    for (;;)
    {
        n++;
        Thread.sleep(DURATION)
    }
    the integers' difference will probably increase a the time goes.
    Although, threads are system-dependent, so their behaviour is not guaranteed to be this way for all OS:es.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

Similar Threads

  1. Running a thread for a fixed period of time
    By ronyb85 in forum Threads and Synchronization
    Replies: 5
    Last Post: 12-16-2010, 08:26 PM
  2. Running thread a second time
    By Fleur in forum New To Java
    Replies: 18
    Last Post: 11-17-2009, 01:57 PM
  3. 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, 03:40 AM
  4. Replies: 11
    Last Post: 01-26-2009, 01:22 AM
  5. Running a thread on another appilcation
    By Charlie in forum Advanced Java
    Replies: 2
    Last Post: 08-12-2008, 01: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
  •