Execute commands later

W

Will

Is it possible in Java to do the following?

I would like to queue a list of java statements to run at a later time.
For example
queue System.out.println("command one")
.... more code ....
queue System.out.println("command two")
... more code .....



then later run the queued commands
 
V

VisionSet

Will said:
Is it possible in Java to do the following?

I would like to queue a list of java statements to run at a later time.
For example
queue System.out.println("command one")
.... more code ....
queue System.out.println("command two")
... more code .....

then later run the queued commands

See:
Java.util.Timer
Java.util.TimerTask
 
W

Will

No, that is not what i am trying to do.
I don't want to run the statements at a specified delay after.
I am trying to just create a queue of commands. Which i can then choose
to run at a later point in the programme by issuing a statement.
 
A

Andrew McDonagh

Will said:
No, that is not what i am trying to do.
I don't want to run the statements at a specified delay after.
I am trying to just create a queue of commands. Which i can then choose
to run at a later point in the programme by issuing a statement.

so use a queue of 'command objects'

google for it.
 
V

VisionSet

Will said:
No, that is not what i am trying to do.
I don't want to run the statements at a specified delay after.
I am trying to just create a queue of commands. Which i can then choose
to run at a later point in the programme by issuing a statement.

Why not just stick them all in a Runnable and call new
Thread(myRunnable).start() when you want to run them?
 
J

Juhan Kundla

Hei!

Is it possible in Java to do the following?

I would like to queue a list of java statements to run at a later time.
For example
queue System.out.println("command one")
.... more code ....
queue System.out.println("command two")
... more code .....



then later run the queued commands

Use the command pattern and a Queue class. The following is
(simplified example of) how i implemented the same thing for one of my
projects.


public interface Command {

public void execute();

}



public class CommandOne implements Command {


public void execute() {
System.out.println("command one")
// .... more code ....
}


}

// Etc, more implementations for the Command


import java.util.LinkedList;
import java.util.Queue;

public class CommandRunner {


private Queue<Command> commands = new LinkedList<Command>();


public void executeCommands() {
for (Command command; (command = commands.poll()) != null; ) {
command.execute();
}
}


public void queueCommand(Command command) {
commands.offer(command);
}


}
 
W

Will

Thanks for the suggestions, but isn't there anything simpler?

Looking at a smaller sub problem.

If i have a string, is there any way to execute the string as a
statement? For example if i have a string with the value
"System.out.println("OUT")"

Is there any way to execute the strings content as a statement?
 
R

Ranganath Kini

Will said:
Thanks for the suggestions, but isn't there anything simpler?

Looking at a smaller sub problem.

If i have a string, is there any way to execute the string as a
statement? For example if i have a string with the value
"System.out.println("OUT")"

Is there any way to execute the strings content as a statement?

As far as I can say. There is no facility of this kind till now. Maybe
we can look forward to Java compiler services within java applications.
That wud allow us to execute Java statements from within our program.

For now, u eiether have to write ur own Java compiler (which is a
mammoth task) or wait for Sun to bring in runtime compiler services.

Regards,
Ranganath
 
P

Patrick May

Will said:
Thanks for the suggestions, but isn't there anything simpler?

Looking at a smaller sub problem.

If i have a string, is there any way to execute the string as a
statement? For example if i have a string with the value
"System.out.println("OUT")"

Is there any way to execute the strings content as a statement?

That's not currently possible in Java. It's straightforward in
Common Lisp (among other languages), though. Do you have to use Java
for your solution?

Regards,

Patrick
 
W

Will

I do have to use Java.

I have found an alternative approach which is implementable in java.

Just thought that it seemed like such a simple idea.

Anyways,
Never mind.
 
T

Thomas Hawtin

Patrick said:
That's not currently possible in Java. It's straightforward in
Common Lisp (among other languages), though. Do you have to use Java
for your solution?

You would probably try to avoid doing it in Lisp, wouldn't you?

More natural would be to use closures, which is more or less what
anonymous inner classes are (although they are verbose).

You can compile Java from Java. javac is written in Java. Web app
servers compile Java in JSPs without issue.

Tom Hawtin
 
P

Patrick May

Thomas Hawtin said:
You would probably try to avoid doing it in Lisp, wouldn't you?

Yes, exec is a code smell.
More natural would be to use closures, which is more or less what
anonymous inner classes are (although they are verbose).

I'd say that closures are what anonymous inner classes dream
about being when they grow up.

On further thought, it seems that the original poster really
wants the equivalent of Scheme's continuations.

Regards,

Patrick
 
L

Luc The Perverse

Will said:
Thanks for the suggestions, but isn't there anything simpler?

Looking at a smaller sub problem.

If i have a string, is there any way to execute the string as a
statement? For example if i have a string with the value
"System.out.println("OUT")"

Is there any way to execute the strings content as a statement?

Um directly - and certainly not in a simpler fashion than what the other
people are describing.

Why not just store the strings of what you want to print out later in an
array and then call System.out.println at that time?

What are you trying to do? If there is some specific reason why the
suggestions made thus far will not work, then it would be better if you
could explain the reason, instead of dodging it. Likely, if we know what
your problem is, we can suggest a better way to solve it. Even if there
were some easy way of storing text java commands and then running them
later, it is not going to be particularly efficient.
 
W

Will

Like i say I have already solved my problem, but if you want to the
reasoning behind my initial question, it is this:

I am using java to generate latex code. I call methods in another java
class which in turn produce latex code. I was wishing to queue up a
number of method calls and then execute them all at once, once a
condition was true.

Thought there would be a simple way to do this, but alas no so I did it
a different way.
 
R

Roedy Green

Which i can then choose
to run at a later point in the programme by issuing a statement.

Presumably you can concoct some object that encapsulates the command
with a interface with a method to execute it. Now see
http://mindprod.com/jgloss/queue.html

You add to the queue to queue up work to do and you pop elements off
the queue to do them.
 
L

Luc The Perverse

Roedy Green said:
you could compose a bat file and exec it later.

I see where you are going with this - but in this case I think it is best to
try to prevent this person from doing whatever he is trying.
 
L

Luke Meyers

Will said:
I was wishing to queue up a
number of method calls and then execute them all at once, once a
condition was true.

Thought there would be a simple way to do this, but alas no so I did it
a different way.

I thought the suggestion you got to create a queue of Runnables was
perfectly reasonable and straightforward. Or Callables, depending on
your needs. Have a look at Executor and ExecutorService in
java.util.concurrent. They are tailor-made to do exactly what you said
you wanted. What was your less complex solution?

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top