简体   繁体   中英

How to await till counter finishes

I need the app to wait till counter finishes its job so it could return full array of values so the next function could use it instead of 'null'

Moving counter to other method works but sadly it's not an option.

        String[] rows;
        parser.beginParsing(file);
        String[] firstRow = parser.parseNext();
        while ((rows = parser.parseNext()) != null) {
            String lastRow = rows[0];
            content = rows;
            data.add(content);
            Double[] points = new Double[data.size()];
            for (int i = 0; i < data.size(); i++) {
                rows = data.get(i);
                points[i] = new Double(Double.parseDouble(rows[1]));                      
            }
            XYSeries dataSeries = new SimpleXYSeries(Arrays.asList(points), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1");       
            SampledXYSeries sampledSeries = new SampledXYSeries(dataSeries, OrderedXYSeries.XOrder.ASCENDING, 2,10);
            lineFormatter = new FastLineAndPointRenderer.Formatter(LINE_COLOUR, null,  null);
            csvPlot.addSeries(sampledSeries, lineFormatter);      
        } 

I expect the thread to await till counter finishes so 'points' will contain actual parsed data. Now it returns 'null' and calling sampledSeries crashes the app.

If you just need a counter of a specific number of seconds, you can post a runnable with below snippet

// Fill your array here

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        // work to be done after 6000 msec
        Toast.makeText(MainActivity.this, "6 sec passed", Toast.LENGTH_SHORT).show();
    }
},6000); // time in msec

but I believe that you need another approach like AsyncTask or AsnycTaskLoader , that is because in most of cases when you want to do something after something else; you actually don't know when the first operation will over, or in your case when your array will entirely be occupied with data; please let me know if you need some help for this.

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