Home > Article > Backend Development > How to increase page views using threads and httpclient
I am just thinking of a request here. I verify the request once and the number of views increases by 1. So I pressed F5 to refresh, but it didn't actually increase every time. I continued to verify and found that it would increase by 1 if I pressed F5 again. Now that the basic feature analysis is complete, dear, do you have any ideas? I think of the previous crawlers here, which just request the page, get the returned HTML and then parse the string. So I also learned from this idea and used the server to request the link, and then the rest is the waiting time. If I keep browsing regardless, there may be suspicion of malicious requests, and my account will be banned. So what technology is suitable for this scenario? Have you thought about it? Yes, you can use threads to set the sleep time after each request.
Then the general idea is clear: httpClient sends a request, and the thread controls the pause time. Without further ado, let me get down to the code:
Everyone should be familiar with the main above. My idea here is that there are three variables in the thread class. Before I use , set the variables first after new comes out, so that they can be used later in the thread run method. Here are 4 ways to implement multi-threading. This seems to have been mentioned in a blog post before.
There are four ways to implement multi-threading, most of which are the first two without return values.
1. Inherit the Thread class to create a thread
The Thread class is essentially an instance that implements the Runnable interface and represents an instance of a thread. The only way to start a thread is through the start() instance method of the Thread class. The start() method is a native method that will start a new thread and execute the run() method. It is very simple to implement multi-threading in this way. By extending Thread directly through your own class and overriding the run() method, you can start a new thread and execute your own defined run() method. For example:
public class MyThread extends Thread { public void run() { System.out.println("MyThread.run()"); } } MyThread myThread1 = new MyThread(); MyThread myThread2 = new MyThread (); myThread1.start(); myThread2.start();
2. Implement the Runnable interface to create a thread
If your own class already extends another class, you cannot directly extend Thread. At this time, a Runnable interface can be implemented, as follows:
public class MyThread extends OtherClass implements Runnable { public void run() { System.out.println("MyThread.run()"); } }
In order to start MyThread, you need to instantiate a Thread first and pass in your own MyThread instance:
MyThread myThread = new MyThread(); Thread thread = new Thread(myThread); thread.start( );
In fact, when a Runnable target parameter is passed to Thread, Thread's run() method will call target.run(), refer to the JDK source code:
public void run() { if (target != null) { target.run(); } }
3. Implement the Callable interface and create a Thread thread through the FutureTask wrapper
Callable interface (also only A method) is defined as follows:
public interface Callable
public class SomeCallable
Callable
4. Use ExecutorService, Callable, and Future to implement threads that return results.
The three interfaces of ExecutorService, Callable, and Future actually belong to the Executor framework. The thread that returns the result is a new feature introduced in JDK1.5. With this feature, you no longer need to go through a lot of trouble to get the return value. And even if you implement it yourself, it may be full of loopholes.
Tasks that can return values must implement the Callable interface. Similarly, tasks that do not return a value must implement the Runnable interface.
After executing the Callable task, you can obtain a Future object. By calling get on the object, you can obtain the Object returned by the Callable task.
Note: The get method is blocking, that is: the thread returns no result, and the get method will wait forever.
Combined with the thread pool interface ExecutorService, the legendary multi-threading with returned results can be realized.
Getting back to the subject, I am using the first one here because I don’t need a return value.
What is added here is the idea of swiping multiple addresses, as well as the odd and even request method to avoid using the same request method every time and avoid being blocked by the system. Listed as a risk for malicious requests. And the sleep time can be set in main. Let’s take a look at the reading volume before the refresh:
I went to bed after writing this last night, and the computer was not turned off. Take a look at this How many requests have been received in the evening:
Then, I refresh the following list page and look at the reading volume again:
Did you see that the number of reads is now more than 600, compared with only more than 300 before.
Because the IP has always been this, the sleep time in the middle is a bit long. If there is an IP for switching and switching logic is added, the effect will be better.
Theoretically speaking, all reading volume can be increased by this method, of course, provided that the client does not implement strict policies such as requesting the same IP multiple times and counting it as one reading. I can’t find out how to rate those articles in Baidu Wenku as excellent documents, but I think it must have something to do with the number of requests. If you have the opportunity, you can try this idea. You can modify it to create two threads and alternate them, one thread brushes one website, haha.
But don’t blame me if your account is banned, haha.
Related recommendations:
How to use thinkPHP+ajax to achieve statistical page pv views
The above is the detailed content of How to increase page views using threads and httpclient. For more information, please follow other related articles on the PHP Chinese website!