Java 使用Executor框架演示应用程序

Java 使用Executor框架演示应用程序,java,multithreading,thread-safety,executorservice,Java,Multithreading,Thread Safety,Executorservice,我对executor框架非常陌生,因此请澄清我对下面提到的executor代码的疑问 任务要求: 1) 我在arraylist中有(10-20k)条记录,我将分配给固定线程池。 2) 我将执行代码中的所有任务。 3) 我希望每个线程都调用webservice(rest/soap) 4) 在得到响应后,它会将响应写入同一个文件(比如A) 我已经完成了下面的伪代码,我知道我需要验证,但我想知道我需要在线程端采取什么预防措施 问题: 应该同步哪些方法? 我应该做的更多(我将处理异常,但从数据安全的角度

我对executor框架非常陌生,因此请澄清我对下面提到的executor代码的疑问

任务要求:

1) 我在arraylist中有(10-20k)条记录,我将分配给固定线程池。 2) 我将执行代码中的所有任务。 3) 我希望每个线程都调用webservice(rest/soap) 4) 在得到响应后,它会将响应写入同一个文件(比如A)

我已经完成了下面的伪代码,我知道我需要验证,但我想知道我需要在线程端采取什么预防措施

问题: 应该同步哪些方法? 我应该做的更多(我将处理异常,但从数据安全的角度来看)

提前感谢。:)

import java.util.ArrayList;
导入java.util.concurrent.Executor;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.RunnableFuture;
导入java.util.concurrent.ThreadFactory;
/**
*由so_what于2017年7月20日创建。
*/
公共类执行器演示{
公共静态void main(字符串参数[])
{
TaskController=新的TaskController();
controller.startExec();
}
}
类任务控制器
{
公共任务控制器(){};
公共空间startExec()
{
//创建新线程执行器
ExecutorService ExecutorService=Executors.newFixedThreadPool(10);
ArrayList=新的ArrayList();
list.add(“One”);list.add(“Two”);//从
数据库

对于(int i=0;作为一般规则,您应该同步更改共享状态(可能包括文件)的任何内容。是的,因此在文件中写入内容应该是同步的。其他事情都可以吗?我只想确认假设线程1永远不会转到WriteFile方法,直到来自webservice的响应不会出现在线程1上
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.ThreadFactory;

/**
 * Created by so_what on 7/20/2017.
 */

public class ExecutorDemo {
    public static void main(String args[])
    {
TaskController controller=new TaskController();
        controller.startExec();
    }
}
 class TaskController
{

public TaskController(){};
    public void startExec()
    {
        //create new thread executor
        ExecutorService executorService= Executors.newFixedThreadPool(10);
        ArrayList<String>list=new ArrayList<String>();
        list.add("One");list.add("Two");//few more will come from the 
        database

        for(int i=0;i<list.size();i++)
        {
            executorService.execute(new TaskRunner(list.get(i)));
        }
        executorService.shutdown();
    }


}
class TaskRunner implements Runnable
        {
String param;

            public TaskRunner(String s) {
                this.param=s;
            }

            @Override
            public void run() {
                System.out.println("Thread is running "+ Thread.currentThread().getName());

                //need to call the webservice
                //after got the response ,need to wirte the response in the file
                //Question:Do this will fine?
                String response=webserviceCall("http://something.com/"+this.param); //will implement lets say dummy data
                writeToFile(response);






            }

            private void writeToFile(String response) {
                //Question :Do i need to synchronize this method?
                //here i will write the response to the flat file


            }
        }