Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

java - Running two independent tasks simultaneously using threads

I've studied lots of tutorials on threads in java but I'm unable to find my answer.

My question is: how to run two independent threads simultaneously?

My case is: I have two tasks;

  1. save some data to the database
  2. send a push notification on a mobile device.

Since these two tasks are independent I want to execute them simultaneously.

I tried using a thread pool with two threads but the problem is that the database tasks finishes quickly but it takes some time to send a push notification.

Consequently when one task is finished while the other is still pending, it throws an exception.

Also there is no problem in my code because it runs fine without using threads.

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
new Thread(new Runnable() {
    public void run() {
        System.out.println("Look ma, no hands");
    }
}).start();

new Thread(new Runnable() {
    public void run() {
        System.out.println("Look at me, look at me...");
    }
}).start();

Works just fine...

I'd prefer the use of an ExecutorService personally.

UPDATED with ExecutorService example

So I wrote this really quick example...

Basically it uses an ExecutorService to run a couple of simple tasks. As it stands, both task will run in parallel with each other (simultaneously)

public static void main(String[] args) throws InterruptedException {
    ExecutorService service = Executors.newFixedThreadPool(2);
    service.submit(new PathScanner());
    service.submit(new Counter());

    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);

    System.exit(0);
}

public static class PathScanner implements Callable<Object> {

    @Override
    public Object call() throws Exception {
        scan(new File("C:/"), 0);
        return null;
    }

    protected void scan(File path, int deepth) {
        if (deepth < 15) {
            System.out.println("Scanning " + path + " at a deepth of " + deepth);

            File[] files = path.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    scan(file, ++deepth);
                }
            }
        }
    }
}

public static class Counter implements Callable<Object> {

    @Override
    public Object call() throws Exception {
        for (int index = 0; index < 1000; index++) {
            Thread.sleep(1);
            System.out.println(index);
        }
        return null;
    }
}

Run it...

Now change ExecutorService service = Executors.newFixedThreadPool(2); to ExecutorService service = Executors.newFixedThreadPool(1); and run it again. Did you see the difference?

This is the way to control the number of simultaneously threads that executor can use while processing it's queue.

Make up some more tasks and add them to the queue and see what you get.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...