scheduledexecutorservice工作不正常

z9zf31ra  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(489)

我使用scheduledexecutorservice每15秒从网站获取一次数据:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
scheduler.scheduleAtFixedRate(this::refresh, 0, 15, TimeUnit.SECONDS);

这是每15秒调用一次的方法:

public void refresh() {
        Site.refreshData();
        System.out.println("Refreshed Data");
        LinearLayout listView = findViewById(R.id.linearLayout);
        System.out.println("Size: " + List.getList().size());
        for (int i = 0; i < List.getList().size(); i++) {
            Entry entry = List.getList().get(i);

            CardView cardView = (CardView) LayoutInflater.from(
                    getApplicationContext()).inflate(R.layout.card, listView, false);

            ImageView checkedView = (ImageView) cardView.getChildAt(0);
            checkedView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
                    entry.isChecked() ? R.drawable.check : R.drawable.cross));

            TextView name = (TextView) cardView.getChildAt(1);
            name.setText(entry.getName());

            TextView comment = (TextView) cardView.getChildAt(2);
            comment.setText(entry.getComment());

            listView.addView(cardView, i);
        }
        System.out.println("Added Cardviews");
        listView.invalidate();
        System.out.println("Refreshed LinearLayout");
    }

我添加了多个打印,作为一个穷人的调试器,我只到了打印arraylist大小的地步。从那以后什么都没有印出来,就好像行刑停止了。我确信错误发生在for循环中。我添加了一个打印,显示当前 i 虽然列表大小是57,但它只停留在4。
有什么问题吗?

5kgi1eie

5kgi1eie1#

经过更多的搜索,我找到了一个方法,它完全符合我的要求:
runonuithread(可运行-可运行)

//scheduler.scheduleAtFixedRate(this::refresh, 0, 15, TimeUnit.SECONDS); ->

public void refresh() {
        Site.refreshData();
        runOnUiThread(() -> {
            ((TextView) findViewById(R.id.queueView)).setText("Total Entries: " + List.getList().size());
            LinearLayout listView = findViewById(R.id.linearLayout);
            listView.removeAllViews();
            for (Entry entry : List.getList()) {

                CardView cardView = (CardView) LayoutInflater.from(
                        getApplicationContext()).inflate(R.layout.card, listView, false);

                ImageView checkedView = (ImageView) cardView.getChildAt(0);
                checkedView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
                        entry.isChecked() ? R.drawable.check : R.drawable.cross));

                TextView name = (TextView) cardView.getChildAt(1);
                name.setText(entry.getName());

                TextView comment = (TextView) cardView.getChildAt(2);
                comment.setText(entry.getComment());

                listView.addView(cardView);
            }
            Toast.makeText(getApplicationContext(), "Refreshed data and UI.", Toast.LENGTH_SHORT).show();
        });
    }
ax6ht2ek

ax6ht2ek2#

在构建gui之前,请在控制台上执行计划的页面检索。
下面是一个示例应用程序。每五秒钟半分钟我们下载一个页面并将其各个部分转储到控制台。
此演示使用 HttpClient 根据jep 321:http客户机添加到java11的类。这里显示的网页访问代码是从本文复制的。
提示:一定要优雅地关闭executor服务,因为它的线程池可能会无限期地继续运行,即使在你的应用程序结束之后。

package work.basil.example;

import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class PageFetch
{
    public static void main ( String[] args )
    {
        PageFetch app = new PageFetch();
        app.demo();
    }

    private void demo ( )
    {
        Runnable pageFetchRunnable = ( ) -> { this.fetchPage(); };

        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.scheduleAtFixedRate( pageFetchRunnable , 1 , 5 , TimeUnit.SECONDS );  // Wait one second, then every five seconds.

        try
        {
            Thread.sleep( Duration.ofSeconds( 30 ).toMillis() );  // Let the executor service do its thing for a half-minute, then shut it down.
        }
        catch ( InterruptedException e )
        {
            e.printStackTrace();
        }
        finally
        {
            scheduledExecutorService.shutdown();
        }
    }

    private void fetchPage ( )
    {
        // Example code for using `HttpClient` framework of Java 11 taken from this article:
        // https://mkyong.com/java/java-11-httpclient-examples/

        HttpClient httpClient = HttpClient.newBuilder()
                .version( HttpClient.Version.HTTP_2 )
                .followRedirects( HttpClient.Redirect.NORMAL )
                .connectTimeout( Duration.ofSeconds( 20 ) )
//                .proxy( ProxySelector.of(new InetSocketAddress("proxy.yourcompany.com", 80)))
//                .authenticator( Authenticator.getDefault())
                .build();

        HttpRequest request = HttpRequest.newBuilder()
                .GET()
                .uri( URI.create( "https://httpbin.org/get" ) )
                .setHeader( "User-Agent" , "Java 11 HttpClient Bot" ) // add request header
                .build();

        HttpResponse < String > response = null;
        try
        {
            System.out.println( "\n-----|  Demo  |-------------------------------------------" );
            System.out.println( "INFO - Access attempt at " + Instant.now() );
            response = httpClient.send( request , HttpResponse.BodyHandlers.ofString() );
            // print response headers
            HttpHeaders headers = response.headers();
            headers.map().forEach( ( k , v ) -> System.out.println( k + ":" + v ) );

            // print status code
            System.out.println( response.statusCode() );

            // print response body
            System.out.println( response.body() );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
        catch ( InterruptedException e )
        {
            e.printStackTrace();
        }
    }
}

运行时:

-----|  Demo  |-------------------------------------------
INFO - Access attempt at 2020-11-20T21:54:37.905896Z
:status:[200]
access-control-allow-credentials:[true]
access-control-allow-origin:[*]
content-length:[242]
content-type:[application/json]
date:[Fri, 20 Nov 2020 21:54:38 GMT]
server:[gunicorn/19.9.0]
200
{
  "args": {}, 
  "headers": {
    "Host": "httpbin.org", 
    "User-Agent": "Java 11 HttpClient Bot", 
    "X-Amzn-Trace-Id": "Root=1-5fb83b1e-7a6acb893aec6fb310984adb"
  }, 
  "origin": "76.22.40.96", 
  "url": "https://httpbin.org/get"
}

-----|  Demo  |-------------------------------------------
INFO - Access attempt at 2020-11-20T21:54:42.907678Z
:status:[200]
access-control-allow-credentials:[true]
access-control-allow-origin:[*]
content-length:[242]
content-type:[application/json]
date:[Fri, 20 Nov 2020 21:54:43 GMT]
server:[gunicorn/19.9.0]
200
{
  "args": {}, 
  "headers": {
    "Host": "httpbin.org", 
    "User-Agent": "Java 11 HttpClient Bot", 
    "X-Amzn-Trace-Id": "Root=1-5fb83b23-3dbb566f5d58a8a367e0e528"
  }, 
  "origin": "76.22.40.96", 
  "url": "https://httpbin.org/get"
}

… and so on for a half-minute.

相关问题