Results 1 to 13 of 13
  1. #1
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Making sure there is a delay between 2 threads

    Hi,

    i need to make sure that a thread is only running after a certain time of another.
    I have a list of class instances like MyTheadList(ThreadcClass).
    In the class there is a method called startThread looking like this:

    Java Code:
    public void startThread()
    	{
    		
    		scheduler = Executors.newScheduledThreadPool( 1 );
    		scheduler.scheduleAtFixedRate(new Runnable() {
    			@Override public void run() {			
    		       	updateStuff(scheduler);
                    System.out.println("UPDATED");
    		     }      
    		 },
    		      0,
    		      50000,                                // run this every xx milliseconds
    		      TimeUnit.MILLISECONDS );
    	}
    there is another static synchronized Method "updateThread" to make sure each Thread is only running after a certain timespan from another, i do it like this:

    Java Code:
    static synchronized void updateStuff() {
    		long time = new Date().getTime();
    		long actualTime = new Date().getTime();
    		
    		while(actualTime < time + 5000)                    // wait 5 seconds before doing something
    			actualTime = new Date().getTime();
                          .................                                             // doing work
                          .................
    		
    	}
    This is working, but with this code i (obviously) get a 100% cpu utilization from the while loop. So i would like to have something that doesn't.

  2. #2
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Making sure there is a delay between 2 threads

    Use the Thread.sleep() method, or use a timer and wait/notify calls

  3. #3
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making sure there is a delay between 2 threads

    Thanks for the answer - used Thread.sleep() :)

  4. #4
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making sure there is a delay between 2 threads

    I now have another problem, i changed my updateStuff method to this:

    Java Code:
    	static synchronized void updateStuff() {
    
    		try {
    			Thread.sleep(5000);
    			System.out.println("UPDATED");
                ...................                             // doing work 
                ...................
    		} catch (InterruptedException e) {
    			System.out.println("Thread interrupted!");
    			Thread.currentThread().interrupt();
    		}
    		
    	}
    The problem comes up when i want to kill threads. Say i want to kill all running threads, i would then call either scheduler.shutdown() or scheduler.shutdownNow() for each object in my List. If i kill them with scheduler.shutdown() all Threads will execute their last run of updateStuff, meaning if i eg have 20 Threads the last will ending in 20*5 seconds. If i cancel them with shutdownNow() all Threads will end immediately but this will also cancel the one thread that is currently "doing work". What i need would be something that only does shutdownNow() on all the Threads that currently aren't in the updateStuff method and shutdown() for the one that is.
    Last edited by AmFreak; 10-30-2011 at 05:35 PM.

  5. #5
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making sure there is a delay between 2 threads

    Ok, doing it like this now:

    Java Code:
    	public void close()
    	{
    		  scheduler.shutdownNow();
    	}
    Java Code:
    static synchronized void updateStuff() {
    		boolean working = false;
    		try {
    			Thread.sleep(5000);
    			working = true;		
                Thread.sleep(5000);         // Only for testing to catch the working part
    		} catch (InterruptedException e) {
    			System.out.println("Thread Interrupted!");
    			if(working != true)
    			{
    				Thread.currentThread().interrupt();
    				return;
    			}
    		}
            System.out.println("WORKING");
            .............           // doing work
            .............
    	}
    It works but beeing new to this stuff i would feel better if someone with knowing of Threading Stuff could comment on this.

  6. #6
    jMaster's Avatar
    jMaster is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Making sure there is a delay between 2 threads

    I'm not any better then you are at threads(i'm probably a lot worse), but at line 9 it should be if(!working) instead of if(working != true)

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

    Default Re: Making sure there is a delay between 2 threads

    if(!working) instead of if(working != true)
    Did you try this and test the result of each? what makes you think they are different? I ask because these two expressions are identical aside from syntax

    if someone with knowing of Threading Stuff could comment on this.
    What do you want a comment on? Does the code work? Its an open ended question that I personally don't even know how to answer

  8. #8
    jMaster's Avatar
    jMaster is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Making sure there is a delay between 2 threads

    Did you try this and test the result of each? what makes you think they are different? I ask because these two expressions are identical aside from syntax
    Yeah I have not on this but other programs. if(working != true) has a higher chance of failing.

  9. #9
    AmFreak is offline Member
    Join Date
    Jul 2011
    Posts
    34
    Rep Power
    0

    Default Re: Making sure there is a delay between 2 threads

    Quote Originally Posted by doWhile View Post
    Did you try this and test the result of each? what makes you think they are different? I ask because these two expressions are identical aside from syntax
    Thought so too.


    What do you want a comment on? Does the code work? Its an open ended question that I personally don't even know how to answer
    Ya it works, but i actually find it a little weird that the execution of the code isn't canceled if the thread is "working" (aka past the catch block).

    @ jMaster

    Yeah I have not on this but other programs. if(working != true) has a higher chance of failing.
    Hmm i thought they were the same, and at first thought u were being a smartass when you mentioned it , but now that u have made it clear that u are not, could you give an example?

  10. #10
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Making sure there is a delay between 2 threads

    Quote Originally Posted by jMaster
    if(working != true) has a higher chance of failing.
    This information is based upon what? Do you have evidence to back up that it 'fails'? Try it for yourself. Write a small program to prove (or suggest) that if 'fails'.
    Last edited by doWhile; 10-31-2011 at 03:00 AM.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Making sure there is a delay between 2 threads

    Quote Originally Posted by jMaster View Post
    if(working != true) has a higher chance of failing.
    That one is going straight to the pool room.

  12. #12
    jMaster's Avatar
    jMaster is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Making sure there is a delay between 2 threads

    Okay cool I found this out a few months ago when my friend made a program that worked perfectly on his home computer, but when he tried the same program at school it didn't work. It compiled fine but the outputs where total rubbish, so I looked around and changed the ifs so that they said if(whateverBoolean) instead of if(whateverBoolean== true) and it worked fine. So I assumed the same would be true for !true(!= true) or false(== false). Since then i've only used my way but I think its good if this goes for some good testing I wont mind being proven wrong but I was just sharing my experience. I'm looking, maybe i'll find the question that this happened its unlikely because there are so many programs though.

  13. #13
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Making sure there is a delay between 2 threads

    Quote Originally Posted by jMaster View Post
    Okay cool I found this out a few months ago when my friend made a program that worked perfectly on his home computer, but when he tried the same program at school it didn't work. It compiled fine but the outputs where total rubbish, so I looked around and changed the ifs so that they said if(whateverBoolean) instead of if(whateverBoolean== true) and it worked fine. So I assumed the same would be true for !true(!= true) or false(== false). Since then i've only used my way but I think its good if this goes for some good testing I wont mind being proven wrong but I was just sharing my experience. I'm looking, maybe i'll find the question that this happened its unlikely because there are so many programs though.
    Its not about being proven wrong, its about learning. This situation is a great opportunity to write an SSCCE to test your assumptions (in fact, the SSCCE is so important, I would say you're original situation could have greatly benefited from - if you don't know what an SSCCE is, google it). When things go wrong with a program, there is always a logical reason...writing an SSCCE strips away a lot of unnecessary code and boils down assumptions to concrete logical facts.

Similar Threads

  1. What type of delay will work for me?
    By keo in forum New To Java
    Replies: 8
    Last Post: 05-07-2011, 08:15 AM
  2. Read URL with delay
    By Eviler in forum Advanced Java
    Replies: 5
    Last Post: 04-04-2011, 07:16 AM
  3. Quick question about for looping and delay
    By DouboC@gmail.com in forum JDBC
    Replies: 0
    Last Post: 12-29-2010, 09:00 PM
  4. how to give delay?
    By shaluchandran in forum New To Java
    Replies: 10
    Last Post: 12-17-2008, 05:57 PM
  5. Delay on inputs during calculation
    By matt_well in forum New To Java
    Replies: 14
    Last Post: 07-26-2008, 04:17 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
  •