简体   繁体   中英

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)

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);
    }
}

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 .

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();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM