multithreading - How to pause and resume a thread in Java game development -


i need know how pause thread , resume after checking condition.

  thread t = new thread () {   public void run() {      while(true) {       update();       repaint();       try {           thread.sleep(50);       } catch(interruptedexception e){}     }   }          }; 

the above code game loop , need pause , resumed if condition checked.

i think thread can started once, cannot use suspend() nor resume()as deprecated.

// imports goes here /** bouncing ball (animation) via custom thread */  public class javaapplication12 extends jframe implements keylistener{      // define named-constants     private static final int canvas_width = 640;     private static final int canvas_height = 600;     private static final int update_interval = 10;     milliseconds int xx = 200; int yy = 500;     shape a1; shape a2; float das[] = {10.0f}; int f = 20;      boolean r = true;      font font = new font("arial",font.roman_baseline,f);      basicstroke color = new basicstroke (10,basicstroke.cap_round,basicstroke.join_bevel,10.0f,das,0.0f);     gradientpaint dash = new gradientpaint(0,0,color.blue,100,0,col or.cyan);      rectangle2d a3;      rectangle2d a12;     rectangle2d a4;     rectangle2d a5;     rectangle2d a6;     rectangle2d a7;     rectangle2d a8;     rectangle2d a9;     rectangle2d a10;     rectangle2d a11; rectangle2d a13;     rectangle2d a14;     rectangle2d a15; rectangle2d a16;     rectangle2d a17; rectangle2d a18;     rectangle2d a19; rectangle2d a20;     rectangle2d a21; rectangle2d a22;     rectangle2d a23;     rectangle2d a25;      arraylist s = new arraylist();      color c2 = color.red;      private drawcanvas canvas; //the drawing canvas (extends jpanel)      // attributes of moving object     // top-left (x, y)     private int x = 200;     private int y = 480;     //width , height     private int size = 20;     //moving speed in x , y directions     private int xspeed = 3;     //displacement per step in x , y     private int yspeed = 5;       /** constructor setup gui components */     public javaapplication12() {         canvas = new drawcanvas();          canvas.setpreferredsize(new dimension(canvas_width, canvas_height));          this.setcontentpane(canvas);          this.setdefaultcloseoperation(exit_on_ close);         this.pack();         this.settitle("bouncing ball");         this.setvisible(true); addkeylistener(this);          fun();      }      thread updatethread = new thread() { @override public void run() {          while (r) { update(); // update (x, y) position              repaint(); // refresh jframe. called paintcomponent()              try { // delay , give other thread chance run                  thread.sleep(update_interval); //milliseconds              } catch (interruptedexception ignore) {} } }      };      /** update (x, y) position of moving object */     public void update() {          x += xspeed; y += yspeed;          if (x > canvas_width - size || x < 0) { xspeed = -xspeed; }          if (y > canvas_height - 80 || y < 0) { yspeed = -yspeed;  }      }  @override public void keytyped(keyevent ke) { }  @override  @suppresswarnings({"deprecation", "deprecation"})  public void keypressed(keyevent ke) {      int keycode = ke.getkeycode();      if(keycode == keyevent.vk_left) { xx = xx-15; }      if(keycode == keyevent.vk_space) {          if(r == true) {              //r = false;             updatethread.start();         }      }      if(keycode == keyevent.vk_escape) {    }      if(keycode == keyevent.vk_right) { xx = xx+15; }  }  @override public void keyreleased(keyevent ke) {      // throw new unsupportedoperationexception("not supported yet."); }  /** drawcanvas (inner class) jpanel used custom drawing */     class drawcanvas extends jpanel {          private static final long serialversionuid = 1l;          int i=0;          int liv = 3;          @override          public void paintcomponent(graphics g) { super.paintcomponent(g); //paint parent's background graphics2d d = (graphics2d)g;              a1 = new ellipse2d.double(x,y,size,size);             d.drawline(0,540,640,540);             for(rectangle2d r : s){                      d.setstroke(color);                     //d.setpaint(dash);                     d.draw(r);              }             a25 = new rectangle2d.double(0,540,1000,10);             d.draw(a25);             setbackground(color.white);                  d.setcolor(color.blue);                  a2 = new roundrectangle2d.double(xx,yy,120,10,2 0,20);                  d.fill(a1); d.setpaint(c2);                 d.fill(a2); d.setfont(font);                 d.drawstring("score : "+i,5,575);                 d.drawstring("life : "+liv,550,575);                  if(a1.intersects(a25.getbounds())) {                     if(liv>0) { liv = liv-1;}                      if(liv == 0) { //joptionpane.showmessagedialog(null, "","",0);                      } }                  if(s.contains(a3) && a1.intersects(a3.getbounds())) {                      = i+10; s.remove(a3);                     yspeed = -yspeed;                     try { m(); }                      catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a4) && a1.intersects(a4.getbounds())) { = i+10; s.remove(a4);yspeed = -yspeed;                      try { m();                      } catch (unsupportedaudiofileexception ex) {                          logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                          logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                          logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a5) && a1.intersects(a5.getbounds())) { = i+10; s.remove(a5);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a6) && a1.intersects(a6.getbounds())) { = i+10; s.remove(a6);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a7) && a1.intersects(a7.getbounds())) { = i+10; s.remove(a7);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a8) && a1.intersects(a8.getbounds())) { = i+10; s.remove(a8);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); }                  } if(s.contains(a9) && a1.intersects(a9.getbounds())) { = i+10; s.remove(a9);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a10) && a1.intersects(a10.getbounds())) { = i+10; s.remove(a10);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a11) && a1.intersects(a11.getbounds())) { = i+10; s.remove(a11);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a12) && a1.intersects(a12.getbounds())) { = i+10; s.remove(a12);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a13) && a1.intersects(a13.getbounds())) { = i+10; s.remove(a13);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a14) && a1.intersects(a14.getbounds())) { = i+10; s.remove(a14);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } } if(s.contains(a15) && a1.intersects(a15.getbounds())) { = i+10; s.remove(a15);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  if(s.contains(a16) && a1.intersects(a16.getbounds())) { = i+10; s.remove(a16);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  if(s.contains(a17) && a1.intersects(a17.getbounds())) { = i+10; s.remove(a17);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  if(s.contains(a18) && a1.intersects(a18.getbounds())) { = i+10; s.remove(a18);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  if(s.contains(a19) && a1.intersects(a19.getbounds())) { = i+10; s.remove(a19);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  if(s.contains(a20) && a1.intersects(a20.getbounds())) { = i+10; s.remove(a20);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  if(s.contains(a21) &&a1.intersects(a21)) { = i+10; s.remove(a21);yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  if(s.contains(a22) && a1.intersects(a22)) { = i+10; s.remove(a22); yspeed = -yspeed; try { m(); } catch (unsupportedaudiofileexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (ioexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } catch (lineunavailableexception ex) {                      logger.getlogger(javaapplication12.cla ss.getname()).log (level.severe, null, ex); } }                  collide(); } public void m() throws unsupportedaudiofileexception, ioexception, lineunavailableexception { string = "chimes.wav"; audioinputstream aa = audiosystem.getaudioinputstream(new file (a).getabsolutefile()); clip c = audiosystem.getclip(); c.open(aa); c.start(); }      }      public final void fun() {          a3 = new rectangle2d.double(20,15,100,20);         a4 = new rectangle2d.double(150,15,100,20);         a5 = new rectangle2d.double(280,15,100,20);          a6 = new rectangle2d.double(400,15,100,20);         a7 = new rectangle2d.double(530,15,100,20);          a8 = new rectangle2d.double(20,50,100,20);         a9 = new rectangle2d.double(150,50,100,20);         a10 = new rectangle2d.double(280,50,100,20);         a12 = new rectangle2d.double(400,50,100,20);         a11 = new rectangle2d.double(530,50,100,20);          a13 = new rectangle2d.double(20,85,100,20);         a14 = new rectangle2d.double(150,85,100,20);         a15 = new rectangle2d.double(280,85,100,20);         a16 = new rectangle2d.double(400,85,100,20);         a17 = new rectangle2d.double(530,85,100,20);          a18 = new rectangle2d.double(20,120,100,20);         a19 = new rectangle2d.double(150,120,100,20);         a20 = new rectangle2d.double(280,120,100,20);         a21 = new rectangle2d.double(400,120,100,20);         a22 = new rectangle2d.double(530,120,100,20);          s.add(a3); s.add(a4); s.add(a5); s.add(a6);         s.add(a7); s.add(a8); s.add(a9); s.add(a10);         s.add(a11); s.add(a12);         s.add(a13); s.add(a14); s.add(a15); s.add(a16);         s.add(a17); s.add(a18);         s.add(a19); s.add(a20); s.add(a21); s.add(a22); }      public void collide() {          if(a2.intersects(a1.getbounds())) {              xspeed = xspeed; yspeed = -yspeed;          } }      public static void main(string[] args) {          // run gui codes in event-dispatching thread thread safety          swingutilities.invokelater(new runnable() {              @override public void run() {                  new javaapplication12(); // let constructor job             }         });     } } 

in addition sleep(), thread has 2 other static methods called wait() , await() purpose.

if want instance of class foo pause , wait instance of class bar tell wake up:

//in foo class synchronized void dostuffandwait() throws interruptedexception {     //do stuff     while(!somecondition) {             wait();            system.out.println("i woke up!");     }  //in bar class;  synchronized void wakefooup(foo foo){     foo.somecondition = true;      foo.notify(); } 

when foo instance gets done doing stuff , somecondition evaluates false, enters while loop. while loop throws people off, because happens isn't "iterative" in traditional sense.

to illustrate: if put call thread.sleep(1000) inside loop, there 1-second pause between each iteration of it. in same sense, assuming somecondition evaluates false, thread put sleep in first iteration of loop. rather going sleep fixed amount of time, goes sleep indefinitely - until else calls notify() on it. when wakes up, goes on next line after wait() (just when call sleep() completes). in case,it prints "i woke up" , checks somecondition again. had bar not set condition true before notifying it, have gone sleep.

i should note has been simplistic introductory explanation, , we're talking - called concurrency - notoriously complex topic.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -