如何每20秒刷新一次连接?

时间:2015-07-18 23:38:23

标签: java url connection

我希望在网页更新时每20秒刷新一次打开的连接

如何添加以下代码以刷新连接?

import java.net.*;
import java.io.*;

public class URLConnectionReader {
  public static void main(String[] args) throws Exception {
    URL oracle = new URL("https://news.google.co.uk/");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
           System.out.println(inputLine);
    in.close();
  }
}

2 个答案:

答案 0 :(得分:2)

ScheduledExecutorService可能就是你想要的。您只需将逻辑包装在Runnable并将其提交给executor

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

executor.scheduleAtFixedRate(()->{
... your task
}, 0L, 20L, TimeUnit.SECONDS);

如果您不使用Java 8,则显式声明Runnable

Runnable runnable = new Runnable(){
 void run(){
   .. your code
 }
};

executor.scheduleAtFixedRate(runnable, 0L, 20L, TimeUnit.SECONDS);

答案 1 :(得分:1)

使用ScheduledExecutorService。将任务包装在一个类中,如下所示:

NewsTask.java

static void Main(string[] args)
{
    string name;

    Console.WriteLine("**************Let's play Master-Mined**************");
    Console.WriteLine();
    Console.Write("Please enter your name: ");
    name = Console.ReadLine();
    Console.WriteLine("Welcome {0}. Have fun!! ", name);

    int numberCount = 0;
    int difficultyLevel = 0;

    int digitNumber = GetRandomNumberCount(numberCount);
    Console.Write(digitNumber + " it is. Let's play.");
    Console.WriteLine();
    int[] PCArray = GenerateRandomNumbers(digitNumber);

    Console.WriteLine("A " + digitNumber + "-digit number has been chosen. Each possible digit may be the number 1, 2, 3 or 4.");
    Console.WriteLine("    ******");

    int difficulty = GetGameDifficulty(difficultyLevel);
    int attempts = difficulty * digitNumber;

    Console.WriteLine("Enter your guess ({0} guesses remaining)", attempts);
    for (int i = 0; i < attempts; i++)
    {
        int[] userArray = GetUserGuess(digitNumber);
        int hits = CountHits(PCArray, userArray, attempts);
        attempts = attempts - 1;
        Console.WriteLine("Enter your guess ({0} guesses remaining)", attempts);
    }
    Console.ReadLine();
}

public static int GetRandomNumberCount(int numberCount)
{
    int number = 0;
    do
    {
        try
        {
            Console.Write("How many numbers would you like to use in playing the game (4-10)? ");
            number = int.Parse(Console.ReadLine());
            Console.WriteLine();
        }
        catch
        {
            Console.WriteLine("You must pick a number between 4 and 10. Choose again.");
            Console.WriteLine();
        }
    } while ((number < 4) || (number > 10));

    return number;
}

public static int GetGameDifficulty(int difficultyLevel)
{
    int difficulty = 0;

    do
    {
        try
        {
            Console.Write("Choose a difficulty level (1=hard, 2=medium, 3=easy): ");
            difficulty = int.Parse(Console.ReadLine());
        }
        catch
        {
            Console.WriteLine("         Incorrect entry: Please re-enter.");
        }
    } while ((difficulty < 1) || (difficulty > 3));

    return difficulty;
}

public static int[] GenerateRandomNumbers(int PCSize)
{
    int eachNumber;
    int[] randomNumber = new int[PCSize];
    Random rnd = new Random();

    for (int i = 0; i < randomNumber.Length; i++)
    {
        eachNumber = rnd.Next(1, 5);
        randomNumber[i] = eachNumber;
        Console.Write(eachNumber);
    }
    Console.WriteLine();
    return randomNumber;
}

public static int[] GetUserGuess(int userSize)
{
    int number = 0;     
    int[] userGuess = new int[userSize];     
    for (int i = 0; i < userGuess.Length; i++)     
    {     
        Console.Write("Digit {0}: ", (i + 1));     
        number = int.Parse(Console.ReadLine());     
        userGuess[i] = number;     
        //Console.Write(number);     
    }     
    Console.WriteLine();     
    Console.Write("Your guess: ");     
    for (int i = 0; i < userGuess.Length; i++)     
    {     
        Console.Write(userGuess[i] + " ");     
    }     
    Console.WriteLine();     
    return userGuess;     
}     

public static int CountHits(int[] PCArray, int[] userArray, int attempts)     
{     
    int hit = 0;     
    int miss = 0;     
    int hits = 0;     

    for (int i = 0; i < attempts; i++)     
    {     
        if(PCArray[i] == userArray[i])     
        {     
            hit = hit + 1;     
            hits = hit;     
        }     
        else     
        {     
            miss = miss + 1; // count - hit;     
        }     
    }     
    Console.WriteLine("Results: {0} Hit(s), {1} Miss(es)", hit, miss);     
    return hits;     
}

然后,让任务像这样运行:

URLConnectionReader.java

import java.net.*;
import java.io.*;

public class NewsTask implements Runnable {
  @Override
  public void run() {
    URL oracle = new URL("https://news.google.co.uk/");
    URLConnection yc = oracle.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
       System.out.println(inputLine);
    in.close();
  }
}