SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    small java challange

    Its a small challange made by me, I wanna see how fast people can figure out whats missing in order to make this code work properly, good luck
    What the code does is printing how many loops were made after 10 seconds basically.

    Code:
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class Test
    {
    	private int a;
    	private long b;
    	private ScheduledThreadPoolExecutor c = new ScheduledThreadPoolExecutor(1);
    	
    	public Test()
    	{
    		c.scheduleAtFixedRate(new RunnableWrapper(), 0, 1000, TimeUnit.MILLISECONDS);
    		
    		while (a < 10)
    			b++;
    		
    		System.out.println("Loops: " + b);
    	}
    	
    	private class RunnableWrapper implements Runnable
    	{
    		@Override
    		public void run()
    		{
    			a++;
    		}
    	}
    	
    	public static void main(String[] args)
    	{
    		new Test();
    	}
    }

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I don't know how Java handles it, but it seems to me you didn't initialize your variables a and b ?

    And I suppose that scheduled runnable stops running automatically once the script is completed?

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't know how Java handles it, but it seems to me you didn't initialize your variables a and b ?
    In java variables are initilized automatically (in this case, a and b are 0 in the beginning)

    And I suppose that scheduled runnable stops running automatically once the script is completed?
    It does, but the problem with the code is that the script never finishes (infinite loop)

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ulthane View Post
    In java variables are initilized automatically (in this case, a and b are 0 in the beginning)
    ok
    It does, but the problem with the code is that the script never finishes (infinite loop)
    Why? Reading "scheduleAtFixedRate" I thought it would schedule that function to be run every 1 seconds, so after 10 seconds a would be 10? And the while loop would be exited?

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why? Reading "scheduleAtFixedRate" I thought it would schedule that function to be run every 1 seconds, so after 10 seconds a would be 10? And the while loop would be exited?
    Well thats excactly the challange, it never exits

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ulthane View Post
    Well thats excactly the challange, it never exits
    Saw this answer with a 2 week delay

    But why doesn't it exit? Is it a scope problem? The value of a is changed in RunnableWrapper, but this can't be seen in Test() ?

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,401
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Weird challenge.....

    Here's a real challenge. Make a Java class that inherits from multiple classes. I mean a real class that inherited all methods from 2 different class. I've actually did this for fun during presentation... it was fun to see java developers reaction when I showed a live demo. If you're curious then look up AOP Mixin or introduction.. some crazy stuff that you'll never use in real project.

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    why do you claim that this challange is weird? have you even tried getting it solved? so far I got no solutions and in fact this is something developers should know.

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,401
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ulthane View Post
    why do you claim that this challange is weird? have you even tried getting it solved? so far I got no solutions and in fact this is something developers should know.
    I misunderstood. I thought the answer to challenge is not available since it's working as intended.

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You could add a break statement, but that's not very elegant.
    Too much intelligence in a compiler is asking for trouble

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well since there hasn't been an answer for a while ill say it :-)
    To make the compiler aware of changes done in such situations on a variable the volatile keyword needs to be added on var a.
    this tells the compiler that the var might be changed from outside of its class so its not automatically turns it to true
    Code:
    private volatile int a;

  12. #12
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,401
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ulthane View Post
    Well since there hasn't been an answer for a while ill say it :-)
    To make the compiler aware of changes done in such situations on a variable the volatile keyword needs to be added on var a.
    this tells the compiler that the var might be changed from outside of its class so its not automatically turns it to true
    Code:
    private volatile int a;
    I would've done

    private static int a;

  13. #13
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I've googled to find the solution, but all I could find was sites that explain that if the value isn't changed in the loop itself, it defaults the while criteria to true. I'd say that's something that should be pointed out when explaining while loops?


Bookmarks

Posting Permissions

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