A
A
artshelom2017-04-25 20:58:15
Java
artshelom, 2017-04-25 20:58:15

Can I read from a page in multiple threads??

I wondered if it is possible to make reading from a page in several threads ??
For example:

public StringBuffer getQuery(String urlToRead) {
        URL url;
        HttpURLConnection conn;
        BufferedReader rd;
        String line;
        StringBuffer result = new StringBuffer();
        try {
            url = new URL(urlToRead);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
        } catch (Exception e) {
            getQuery(urlToRead);
            e.printStackTrace();
        }
        return result;

here is the java code, if it is accessed from multiple threads, will there be packet loss and other terrible things??
If so, how can this problem be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2017-04-26
@artshelom

"Is it possible to read from a page in multiple threads?" - you should think about this even before writing the code.
Suppose you have a book, and you decide to read it at the same time by some company. How would you organize it?
The first thing that comes to mind is to find a responsible "dispatcher" who will give portions of pages to each participant.
But what if the book is not divided into pages? How to share it then? It turns out that at first it is necessary for someone to read it, then share it, then distribute the parts. But why, then, read an already read book (if, of course, the goal is only in the very fact of reading)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question