-1

I have a function say Foo()...My requirement to run some function for specific time and force it to return value within that given time.For eg if i run this function for 100 ms then no matter how many numbers are added in List listofnumbers whitin 100ms it should return those values.I have seen Timer as one solution but Timer or TimerTask has API to schedule task once every some seconds.What i want is to run function only and return whatever value it has wihtin given time.

foo()
{
List lisof numbers
for(int i=0;i<somenumber;i++)
{
listofnumbers.add(i);
}

return listofnumbers }

steve
  • 67
  • 10
  • -1 for the syntactically incorrect code. You cannot have a **function** on it's own. Every block of code is always enclosed within some class. – Bhesh Gurung Mar 26 '14 at 20:11
  • @steve Thanks for showing the core of what you're trying to do. Really, there's no need to show us the class that this is in. Your question is clear from what you've given us. – Dawood ibn Kareem Mar 26 '14 at 20:28
  • @BheshGurung Sometimes you don't have to see the whole class to answer the question. The function in the example has some problems, but it only references one free variable. It's not hard to see the programmer's intent, and that's all we need in this case. It's not as if he was asking us to find an obscure bug. – Solomon Slow Mar 27 '14 at 02:38

2 Answers2

4

The easiest way would be to just keep track of elapsed time.

List<Integer> generateList(int maxRuntime, int maxNum) {
    long startTime = System.currentTimeMillis();
    List<Integer> numbers = new ArrayList<Integer>();
    for(int i = 0; i < maxNum; i++) {
       if(System.currentTimeMillis() - startTime > maxRuntime) {
           break;
       }
       numbers.add(i);
    }
    return numbers;
}

I called this:

System.out.println(generateList(100, Integer.MAX_VALUE));

Output is around 2041737.

Clete2
  • 367
  • 1
  • 8
  • But this would run in same thread..This question was part of building game.The main requirement is to make a move within given time.I have function that creates list of moves.But i want to return moves within specific time.I was thinking of creating two threads.One thread will sleep for that time and other thread will call that function and when thread1 timeouts then thread 2 will terminate and will return list – steve Mar 26 '14 at 20:33
  • There are a lot of different ways to do what you're talking about. What I wrote above is a method that will return in ~100ms (+ a little bit for the task of returning). If you put that code into a new Thread, you'll be able to then call join on that thread to wait for it to complete. – Clete2 Mar 26 '14 at 20:38
  • @steve "But this would run in same thread." So? It gets the job done. If you really, really want to have two threads, then have the timer thread set a flag that the move generator thread periodically checks while it is working. When the move generator sees the flag set, then it's time to clean up and quit. Threads should always cooperate. It's never a good idea for one thread to _force_ another thread to do anything. – Solomon Slow Mar 26 '14 at 22:00
  • @steve: What's the point of having two threads if one of them just sleeps? Seems like you're better off with this solution using a single thread. – Jim Mischel Mar 27 '14 at 13:00
1

Use an AtomicReference and a ScheduledExecutorService.

Create an AtomicReference<List<Something>>; make it shared by the readers and writers.

When a writer needs to write this list again, do:

final List<Something> list = new ArrayList<>(ref.get());
// modify list
ref.set(list);

and schedule that writer using the builtin ScheduledExecutorService capabilities.

fge
  • 119,121
  • 33
  • 254
  • 329