使用多个线程访问时无法锁定同步方法

时间:2018-05-07 07:07:56

标签: java multithreading elasticsearch search synchronization

我有下面的代码,我正在测试弹性搜索的线程同步,但不知何故我无法成功,任何人都可以告诉我哪里出错了? 如果我启用Thread sleep in' startThreadProcess'方法然后一切正常,因为它睡了一定的时间段。我不想要的,我希望在不使用线程休眠的情况下获得正确的线程锁定。 但是在上面的代码中我发生了什么,我使用Executor来汇集线程。我正在为计数4运行循环,以便从池中启动4个线程。所以,当我使用执行程序提交的线程提交调用synchronized方法并在同步方法内部时,我正在调用其他同步方法,从中获取特定节点的总计数,并通过递增插入新文档来继续该计数我的第一个线程尚未完成第二个线程进入并尝试从同步方法调用的方法获取计数总数,因此我得到错误计数线程2因为我的第一个线程将在节点内插入10000个json文件所以我期待线程2应该得到10000计数然后它应该处理插入但是在这里我的线程2进入中间并获得计数任何随机数并从该数字开始插入而不是预期的场景

package com.acn.adt.main;

public class ESTest {

    private static final String dataPath = "C:\\Elastic Search\\Data.json";
    static ESTest esTest = new ESTest();
    private static TransportClient client = null;
    private Properties elasticPro = null;
    private InputStream input = null;

    ElasticSearchCrud esCRUD = null;

    private final Object lock = new Object();

    public static void main(String[] args) {
        String strArray[] = new String[] {"1"};
        esTest.startProcess(strArray);
    }

    public void startProcess(String strArray[]) {
        try {
        input = new FileInputStream(ElasticSearchConstants.ELASTIC_PROPERTIES);
        elasticPro = new Properties();
        //elasticPro.load(ElasticSearchClient.class.getResourceAsStream(ElasticSearchConstants.ELASTIC_PROPERTIES));
        elasticPro.load(input);
        System.out.println(elasticPro.getProperty("homeDir"));

        long startTime = System.currentTimeMillis();
        Settings setting = Settings.builder()
        //.put("client.transport.ping_timeout", "100s")
        .put("cluster.name", elasticPro.getProperty("cluster"))
        //.put("node.name", elasticPro.getProperty("node"))
        //.put("client.transport.sniff", Boolean.valueOf(elasticPro.getProperty("transport.sniff")))
        .put("client.transport.sniff", false)
        .put("cluster.routing.allocation.enable", "all")
        .put("cluster.routing.allocation.allow_rebalance", "always")
        //.put("client.transport.ignore_cluster_name", true)
        .build();

        client = new PreBuiltTransportClient(setting)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 
                    Integer.valueOf("9300")));

        long endTime = System.currentTimeMillis();

        System.out.println("Time taken for connecting " + TimeUnit.MILLISECONDS.toSeconds((endTime - startTime)));


        ExecutorService executorService = Executors.newFixedThreadPool(10);

        for(int i = 1; i <=4; i++) {

            if(i==1) {
                strArray = new String [] {"1"};
            }else if(i == 2) {
                strArray = new String [] {"1"};
            }else if(i == 3) {
                strArray = new String [] {"1"};
            }else if(i == 4) {
                strArray = new String [] {"1"};
            }

            executorService.execute(new ESThread(esTest,strArray,i));

        }

        }catch(Exception e) {

        }

    }

    public class ESThread implements Runnable {
        private final Object lock = new Object();
        ESTest esTester = null;
        String strArr [] = null;
        int i =0;
        public ESThread(ESTest esTester,String[] strArr,int i)  {
            this.esTester = esTester;
            this.strArr = strArr;
            this.i = i;
        }

        @Override
        public void run() {
            System.out.println("Name of Current thread is Thread_"+i);
            synchronized(lock) {
                esTester.startCRUDProcess(strArr);
            }
            System.out.println("Thread_"+i+" done.");
        }
    }

    public void startCRUDProcess(String [] strArr) {

        SearchAPI esSearch = new SearchAPIImpl();
        boolean caseFlg = false;
        String _indexName = "gcindex";
        String _indexType = "gctype";
        String _ids = "501,602,702#@#1,10000,10001";
        String _id = "10000";
        String[] _strIds = new String[] {"10000","9999"};

        System.out.println("Insert Multiple Process is started...");
        System.out.println("--------------------------------------");
        try {
            caseFlg = insertMultipleDocument(dataPath,client,_indexName,_indexType);
        } catch (IOException | ParseException e) {
            e.printStackTrace();
            caseFlg = false;
        }

    }

    public synchronized boolean insertMultipleDocument(String dataPath,TransportClient client,String _indexName,String _indexType) throws FileNotFoundException, ParseException {

        try {

            JSONParser parser = new JSONParser();
            // we know we get an array from the example data
            JSONArray jsonArray = (JSONArray) parser.parse( new FileReader( dataPath ) );

            BulkRequestBuilder bulkDocument = client.prepareBulk();

            @SuppressWarnings("unchecked")
            Iterator<JSONObject> it = jsonArray.iterator();
            int i = 0;

            i = _getTotalHits(client,_indexName,_indexType);

            System.out.println("Total number of hits inside index = "+_indexName+" of type = "+_indexType+" are : "+i);
            System.out.println("-------------------------------------------------------------------------------------");
            while( it.hasNext() ) {
                i++;
                JSONObject json = it.next();
                System.out.println("Insert document for "+i+": " + json.toJSONString() );   

                // either use client#prepare, or use Requests# to directly build index/delete requests
                bulkDocument.add(client.prepareIndex(_indexName, _indexType, i+"")
                        .setSource(json.toJSONString(), XContentType.JSON )
                        );
            }

            BulkResponse bulkResponse = bulkDocument.get();
            if (bulkResponse.hasFailures()) {
                System.out.println("process failures by iterating through each bulk response item : "+bulkResponse.buildFailureMessage());
                return false;
            } else {
                System.out.println("All Documents inserted successfully...");
                /*if(bulkResponse.getItems()!=null) {
                    for(BulkItemResponse response:bulkResponse.getItems()) {
                        System.out.println(response.toString());
                        System.out.println(response.getResponse());
                    }
                }*/
                return true;
            }

        } catch (IOException ex) {
            System.out.println("Exception occurred while get Multiple Document : " + ex/*, ex*/);
            return false;
        }
    }   

    public synchronized int _getTotalHits(TransportClient client,String _indexName,String _indexType){
        SearchHits hits = null;
        int recCount = 0;
        long totalCount = 0;
        try {
            SearchResponse seacrhResponse = client.prepareSearch(_indexName)
                    .setTypes(_indexType)
                    .setSearchType(SearchType.QUERY_THEN_FETCH)
                    .get();

            if (seacrhResponse != null) {
                hits = seacrhResponse.getHits();
                totalCount = hits.getTotalHits();
                System.out.println("count = "+totalCount);
            }

            recCount = Integer.parseInt(totalCount+"");

        }catch(Exception ex) {
            System.out.println("Exception occurred while search Index : " + ex/*, ex*/);
        }

        return recCount;
    }    


}

0 个答案:

没有答案