tim.baker
tim.baker

Reputation: 3307

Run functions simultaneously in Java (Eclipse)

I am new to Eclipse and Java and I have researched this a fair amount, but most answers and examples (SO included) assume you have a basic understanding of where you are heading, so please explain simply (I am sure it must be simple).

I have a function such as this, which runs successfully in eclipse. However I need to be able to call this function twice and for them to run simultaneously, opposed to one after the other.

public static void reporting(String category, String message) {
     // Code for this function  
}

public static void main(String[] args) {
    // Call the function once
    reporting("one","Starting");

    // Call the function a second time
    reporting("two","Starting");
}

So currently instance 1 is running, then the second instance executes after the first completes. I need the first to start executing and then for the second to start straight away after. My only vague idea to this so far is this:

public static void main(String[] args) {
    // Call the function once
    async reporting("one","Starting");

    // Call the function a second time
    async reporting("two","Starting");
}

However this just throws errors about async not being a variable so clearly it isn't right.

As far as I can gather this is possible using async somehow - but as I say everywhere I look (SO answers included) assume you have an idea of where this should fit.

(Ps, I am fully away that I maybe to totally wrong about async or there maybe a more efficient way altogether, but anything to help me learn in the right direction is beneficial)

Upvotes: 1

Views: 190

Answers (3)

ortis
ortis

Reputation: 2223

You should read some Thread tutorials.

One of the many possibilities:

public static void main(String[] args)
{
    try{

    Thread t1 = new Thread()
    {
        public void run() {
            reporting("one","Starting");
        };

    };


    Thread t2 = new Thread()
    {
        public void run() {
            reporting("two","Starting");
        };

    };


    t1.start();//start the threads
    t2.start();

    t1.join();//wait for the threads to terminate
    t2.join();

    }catch(Exception e)
    {
        e.printStackTrace();

    }
}

Upvotes: 1

Mena
Mena

Reputation: 48434

You should extend Thread or implement Runnable for that. You then execute the code in run methods, and start the Runnables by invoking their start methods.

Self-contained, quick and dirty example (within a Main class):

public static void main(String[] args) throws Exception {
    // these are spawned as new threads, 
    // therefore there is no guarantee the first one runs before the second one
    // (although in this specific case it's likely the first one terminates first)
    new Reporter("one","Starting").start();
    new Reporter("two","Starting").start();
}

static class Reporter extends Thread {
    String category, message;

    Reporter(String category, String message) {
        this.category = category;
        this.message = message;
    }
    @Override
    public void run() {
        reporting(category, message);
    }

    void reporting(String category, String message) {
        System.out.printf("Category: %s, Message: %s%n", category, message);
    }
}

Upvotes: 2

nem035
nem035

Reputation: 35501

You can do create two Thread objects inside your main() like this:

Thread thread1 = new Thread () {
  public void run () {
      reporting("one","Starting");
  }
};

Thread thread2 = new Thread () {
  public void run () {
      reporting("two","Starting");
  }
};

Then start the 2 threads like:

thread1.start();
thread2.start();

Read more about the Thread Class and also check out some useful examples.

Upvotes: 1

Related Questions