Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Swallock is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Trying to run side by side operations without interference

    Okay, the title is obviously rather hard to understand, but I couldn't think of something more accurate. Now that that's out of the way, here's my problem:

    I'm working on a game for java (in swing) for my final project (I'm in my first year of Programming) and I've run into a problem. I'm trying to make it so that I can basically perform multiple operations at once. Basically, I need a JButton to toggle between 2 colors every second without making it so that I can't do anything else, like click buttons. My best idea for delaying the color change would be Thread.sleep(), but doing that temporarily disables everything else. Suggestions?

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Make some timerclass extending Thread.

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

    Default

    Quote Originally Posted by Jodokus View Post
    Make some timerclass extending Thread.
    Make what?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Sorry Swallock, it is not my habbit to spoonfeed, but I want JOS to understand me.

    Java Code:
    public class ButtonchangeTimer extends Thread {
    	private ITimerTask task;
    	private long sleepingTime = 1000;//default is a second
    	private boolean pleaseStop = false;
    	
    	public ButtonchangeTimer(ITimerTask theTask){
    		task = theTask;
    	}
    	public void startAgain(){
    		pleaseStop = false;
    		run();
    	}
    	public void pause(){
    		pleaseStop = true;
    	}
    	public void run(){
    		while( !pleaseStop ){
    			doTask();
    			waitASec();
    		}
    	}
    	public void setSleepingTime( int mili ){
    		sleepingTime = mili;
    	}
    	private void waitASec(){
    		try{ Thread.sleep( sleepingTime );
    		}catch(InterruptedException e){}
    	}
    	private void doTask(){
    		task.doPerTick();
    	}
    }

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @Jos
    Hey! I see i'm a senior member now. Is that when you reach the 100 posts?
    Or am I revealing a topsecret?

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    To the OP, its better to read up and understand what the above code does...see the following
    Lesson: Concurrency (The Java™ Tutorials > Essential Classes)
    but take care to mix multithreading with single threaded GUI models (like AWT/Swing). See
    How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    And in relation to the code Jodokus posted, I will argue that it's better practice to implement runnable and create a thread with that implementation rather than extend Thread

  7. #7
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @doWhile
    Sorry, thread blocked. I'm reading........

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

  9. #9
    Swallock is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks, doWhile, I got that swing timer to do exactly what I needed. Thanks for the link!

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

    Default

    Quote Originally Posted by Jodokus View Post
    Sorry Swallock, it is not my habbit to spoonfeed, but I want JOS to understand me.

    Java Code:
    public class ButtonchangeTimer extends Thread {
    	private ITimerTask task;
    	private long sleepingTime = 1000;//default is a second
    	private boolean pleaseStop = false;
    	
    	public ButtonchangeTimer(ITimerTask theTask){
    		task = theTask;
    	}
    	public void startAgain(){
    		pleaseStop = false;
    		run();
    	}
    	public void pause(){
    		pleaseStop = true;
    	}
    	public void run(){
    		while( !pleaseStop ){
    			doTask();
    			waitASec();
    		}
    	}
    	public void setSleepingTime( int mili ){
    		sleepingTime = mili;
    	}
    	private void waitASec(){
    		try{ Thread.sleep( sleepingTime );
    		}catch(InterruptedException e){}
    	}
    	private void doTask(){
    		task.doPerTick();
    	}
    }
    That class doesn't make sense, e.g. the startAgain() method recursively runs the run() method again; I don't think that's what you want. And what does doPerTick() method mean in your own ITimeTask? Better use one of supplied Timers instead.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    That class doesn't make sense
    Please don't be so harsh on me. I detailed my firsts post in the first place on your request. I once adapted some example and used it in some programs, for instance a clock, obediently ticking away (and never being stopped by me). You are right that using "run();" in startAgain() is not a good idea.
    doPerTick() is obviously a method implemented by ITimerTask task, moving my clock or changing OP's buttoncolors.
    And of course, being largely an autodidact, there's always a chance of missing some supplied libraryfunction. That's one of my reasons of being here: see how others do it.
    Last edited by Jodokus; 05-05-2011 at 12:19 PM. Reason: spelling

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

    Default

    Quote Originally Posted by Jodokus View Post
    Please don't be so harsh on me.
    Deep breath please as that wasn't a harsh criticism. I've had to endure far worse from Jos and others.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Deep breath please as that wasn't a harsh criticism. I've had to endure far worse from Jos and others.
    Harsh? Who? Me? Have you forgotten about all those bottles of liquor you made me send to you? Hm?

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Quote Originally Posted by JosAH View Post
    Harsh? Who? Me? Have you forgotten about all those bottles of liquor you made me send to you? Hm?

    kindest regards,
    Have you forgotten the window you helped me through and all the folks with sharp pointy sticks below? Regards indeed.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Have you forgotten the window you helped me through and all the folks with sharp pointy sticks below? Regards indeed.
    Duh, you obviously forgot that I personally helped to get all the cars out of the way so they would be safe from all your bodily fluids and other filthy substances you couldn't keep with you after contact with the pavement.

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Wow, I obviously stirred up a long endured friendship.
    "Harsh" may have been a too strong word. It's just because I appreciated Jos as friendly and forgiving. But now I can sleep the sleep of the innocent again. Thanks Fubarable.

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

    Default

    Quote Originally Posted by Jodokus View Post
    Wow, I obviously stirred up a long endured friendship.
    "Harsh" may have been a too strong word. It's just because I appreciated Jos as friendly and forgiving. But now I can sleep the sleep of the innocent again. Thanks Fubarable.
    Right, walk all the way up the stairs to the bedroom, step out of the window and don't look down; thank your for your cooperatiion.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Create a new thread.

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

    Default

    Quote Originally Posted by Solarsonic View Post
    Create a new thread.
    Indeed, this thread has been hijacked to outerspace. Blame it on the Nederlanders, as scurvy and skanky a breed of people as I've ever had the distinct pleasure to have known.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Indeed, this thread has been hijacked to outerspace. Blame it on the Nederlanders, as scurvy and skanky a breed of people as I've ever had the distinct pleasure to have known.
    You start another thread because this one is ours; we are going to build a windmill on top of it; put a red light district somewhere in one of its corners and sell weed all over the place; and cheese and beer (Grolsch) and fine black tobacco. It'll be a fine place; this is going to be a world famous thread and I hereby declare me, myself to be the queen of this thread; so there.

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. how to countdown on server side
    By BigBear in forum Java Servlet
    Replies: 3
    Last Post: 04-26-2010, 10:33 PM
  2. Side-scrolling
    By shadycharacter in forum New To Java
    Replies: 0
    Last Post: 04-21-2010, 04:20 PM
  3. Replies: 1
    Last Post: 03-20-2010, 07:03 PM
  4. Deploy Jar Side By Side
    By Adrian in forum Eclipse
    Replies: 0
    Last Post: 04-11-2008, 02:09 PM

Posting Permissions

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