How to create java threads with multiple start and run signatures

anshuma
anshuma used Ask the Experts™
on
Hi,

with reference to the following question

http://www.experts-exchange.com/questions/28784460/Triggering-a-shell-command-from-a-java-servlet-30-minutes-after-the-http-call.html

I need some examples on how the start and run methods can have different signatures

i.e how can I pass arguments to run() and start() without creating static variables
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
You implement Runnable and/or Callable. The classes you create and submit to the executor can have whatever members you need. Those would be filled by the ctor
anshumaEngineering

Author

Commented:
Can you please attach some sample pseudo code
anshumaEngineering

Author

Commented:
What is ctor
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
Basically, you define a custom Runnable class such as this (a simple example)...

public class SendEmailTask implements Runnable {
    private String to;
    private String message;

    public SendEmailTask(String to, String message) {
        this.to = to;
        this.message = message;
    }

    @Override
    public void run() {
        // Have your code to create and send your email, you can use the variables 'to' and 'message'
        //   to populate the email message
    }
}

Open in new window



And when your request comes in and you want to schedule your delayed task, you can do this (borrowing a bit from Doug's code in the previous question)...

m_executor.schedule(new SendEmailTask("joebloggs@gmail.com", "Hi Joe, Here is an email!!"), 1, TimeUnit.MINUTES);

Open in new window

Top Expert 2016

Commented:
You implement Runnable and/or Callable.
That, of course, should have said

You implement Runnable or Callable.

What is ctor
ctor == constructor

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial