Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default thread and clock managment - help please!

    I have objects (baddies in a game) that have individual clocks/counters assigned to them (when I make an object, I fill an integer based array list with a new digit entry at a value of one)

    that all happens in threads.

    then in another thread, I am checking the whole list of clocks, to see if any are equal to a wanted value , then I am taking an action and reseting the clock at that point on the array list.

    now, the problem is:

    even though my code says " yes, I've taken that action because the value of array position x was >= specified value and I will now reset the clock"
    it doesn't reset the clock. Here is what it looks like

    *these are excerpts taken from a really big program, however they should stand on their own as they are individual classes
    [spoiler]
    :
    Java Code:
    public class levelclock extends Thread {
    
    		public void run() {
    			while (Run2 == true) {
    				if (Run == false) {
    
    					try {
    						Thread.sleep(100);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    
    				// this is the loop that handles all the code
    				while (Run == true) {
    
    					clocktime++;
    
    					if (clocktime >= 10000/10) {
    
    						clocktime = 0;
    						level++;
    						points = points * 1.10;
    						points = Math.round(points);
    					}
    
    					int loop = 0;
    
    					try {
    						while (loop <= badguyshootclock.size()) {
    
    							badguyshootclock.set(loop,
    									badguyshootclock.get(loop) + 10);
    							System.out.println(badguyshootclock.get(loop));
    									loop++;
    									
    									
    						}
    
    					}
    
    					catch (IndexOutOfBoundsException e) {
    						loop = 0;
    						//System.out.println("clockcounterreset");
    					}
    
    					try {
    						Thread.sleep(10);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    
    				}
    			}
    		}
    	}
    
    
    
    
    public class badguybulletcreator extends Thread {
    
    		public void run() {
    			while (Run2 == true) {
    				if (Run == false) {
    					try {
    						Thread.sleep(100);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    				// this is the loop that handles all the code
    				while (Run == true) {
    				
    					
    					int loop = 0;
    					
    					while (loop <= badguyshootclock.size()) {
    						
    
    						try {
    							System.out.println( "here1");
    							if (badguyshootclock.get(loop) >= 2500) {
    								System.out.println( "here2");
    
    								Rectangle bullet = new Rectangle(
    										badguys.get(loop).x,
    										badguys.get(loop).y, 10, 10);
    								System.out.println( "here3");
    								badguybullets.add(bullet);
    							System.out.println( "here4");
    								badguyshootclock.set(loop,badguyshootclock.get(loop)-badguyshootclock.get(loop));
    								System.out.println( "here5");
    								
    							}
    							
    							
    						} catch (IndexOutOfBoundsException e) {
    							loop = 0;
    						}
    
    						loop++;
    						}
    
    				
    					
    					
    					try {
    						Thread.sleep(1);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					
    					
    				}
    
    			}
    
    			
    
    		}
    	}
    [spoiler/]

    sorry if the formatting is weird, I used the auto format feature on eclipse.





    now, that is the question, but since I'm posting any way , wana take a look at my game I'm working on?(attached, just open up the zip and read the .txt) It's cool stuff yo . suggestions on better ways to do stuff are more then welcome! also If your wondering about the outta time comments and stuff, this was a project I started on last year for programming class in grade 12. I've come back to improve it this year.
    Attached Files Attached Files
    Last edited by cupid_the_conqueror; 12-19-2014 at 08:35 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: thread and clock managment - help please!

    Unfortunately, your excerpt doesn't help me much. When using threads, many things can go wrong and appropriate synchronization is critical. For example, have you declared Run and Run2 as volatile? Have you considered using any of the timer classes to have the object manager their own timers and send appropriate events? These questions/suggestions may not apply but only serve to illustrate that I don't have the big picture. Also, you may want to review Java naming conventions in my signature.

    One specific observation is your loop variable. If you are counting up appropriately to an upper bound, why should you expect an IOBE to be thrown? See line # 102.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Join Date
    Dec 2014
    Posts
    4
    Rep Power
    0

    Default Re: thread and clock managment - help please!

    I tossed in the IOBE because the thread is checking against an arraylist which at any moment may become smaller by one as an element is removed by a another thread. However, I've never heard of the timer classes.. I'll do some research into that because it sounds to be like it may save me a lot of trouble!

Similar Threads

  1. 24 hour clock to 12 hour clock project.
    By bs3ac in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 11:10 AM
  2. clock
    By zacake in forum New To Java
    Replies: 11
    Last Post: 03-28-2012, 03:46 PM
  3. help with clock
    By Bimz in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 02:50 PM
  4. good memory managment
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 12-15-2009, 11:44 AM
  5. clock
    By ws6driver in forum New To Java
    Replies: 1
    Last Post: 07-31-2009, 05:15 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
  •