执行器框架的多项任务

multiple tasks with executor framework

我正在编写一个程序,以使用 Executor Framework 为 运行 实现两个不同的任务,作为学习多线程的一部分。早些时候,我使用同步方法来满足这个要求,但它给出了错误的结果。然后,我了解到使用 Executor Framework 是线程管理的更好方法。

下面的程序使用同步方法

import java.io.*;
import java.util.Scanner;
import java.nio.*;
class FileWriteThreadExample implements Runnable{
    /*This class needs to write some content into text file*/

    public synchronized void run() {
            StringBuilder thisProgamMessage = new StringBuilder();

            try(FileWriter fw = new FileWriter("C:\TestNotes.txt", true);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw))
            {
                for(int i=1; i<=50;i++){
                    //Thread.sleep(500);
                    //System.out.println(i);

                    thisProgamMessage.append(i+":"+Math.random()+"\n");

                }
                out.println(thisProgamMessage.toString());
            } catch (IOException e) {
                //exception handling left as an exercise for the reader
            }

    }
}

class FileWriteThreadExample2 implements Runnable{
    /*This class needs to write some content into text file*/

    public synchronized void run() {
            StringBuilder thisProgamMessage = new StringBuilder();
            try(FileWriter fw = new FileWriter("C:\TestNotes.txt", true);
                BufferedWriter bw = new BufferedWriter(fw);
                PrintWriter out = new PrintWriter(bw))
            {


                System.out.println("Starting Second Write Thread Task");
                for(int i=50; i>=1;i--){
                    //Thread.sleep(500);
                    //System.out.println(i);
                    thisProgamMessage.append(i+"====>"+Math.random()+"\n");
                }
                out.println(thisProgamMessage.toString());
                System.out.println("Completing Second Write Thread Task");
            }
            catch (FileNotFoundException fnfe){
                fnfe.printStackTrace();
            }
            catch(IOException ioex) {
                ioex.printStackTrace();
            }
            /*catch(InterruptedException ie){
                ie.printStackTrace();
            }*/     
    }
}
class SynchronizeTest {
        public static void main (String[] args) {
            FileWriteThreadExample t1 = new FileWriteThreadExample();
            FileWriteThreadExample2 t2 = new FileWriteThreadExample2();

            t1.start();

            t2.start();

        }
    }

这里的问题是我不知道为执行两个任务的执行器编写代码。我已经使用 ExecutorService 为 运行 单个任务实现了代码,即

ExecutorService es = Executors.newFixedThreadPool(5);
    public void doStuff() {


        es.submit(new MyRunnable());


    }

最后,有人可以建议我使用 Executor Framework 实现两个不同的任务吗?

PS:让我知道对理解问题陈述的任何困惑

不知道你的运动意图。在您的同步版本中。你没有同步。这两个线程按顺序访问 TestNotes.txt,因为只有一个文件可以打开一个文件进行写入 moment.is 这是你的意图吗?

你非常接近:

ExecutorService es = Executors.newFixedThreadPool(5);
public void doStuff() {
    es.submit(new FirstTask());  // FirstTask implements Callable
    es.submit(new SecondTask());  // SecondTask implements Callable
}

或者:

ExecutorService es = Executors.newFixedThreadPool(5);
public void doStuff() {
    Collection<Callable> tasks = Arrays.asList(new Callable[]
            { new FirstTask(), new SecondTask() });
    es.invokeAll(tasks);
}

每个任务都可以像往常一样相互同步,就好像您自己是 运行 原始线程中的任务一样。

请注意 ExecutorService 需要 Callable 接口而不是 Runnable 接口。