人工智能搜索结果比百度搜索结果更准确

背景

我写代码,有个特点,就是凑功能,各种查找抄组装把功能给凑齐了,但好懒是不论的。我只有一次遇到过,一定要考虑性能问题,那次的需求是要把请求返回的结果存储在数据库中,大概要请求五六万的数据,不停的写数据入库,效率极慢。我当时请教了我大学同学,本都是计算机生,但他做了开发,我做了测试,从此代码水平相差十万八千里。但真的,我一直没搞清楚多线程杂用,我平时用的搜索引擎是百度,搜到的结果,要么差强人意,要么跟我这水平一样的,写个东西废话一框还总是不是我想要看的。
最近业务缩减,我除了技术支持以外,很多活都能提前完成,空闲成了我最大的工作特色,我开始想做个机器人陪我聊会人生,找了几个人工智能的api,别提有多智障了,给大家展示一下:
人工智能搜索结果比百度搜索结果更准确_第1张图片
这批实习生中,有个趣味跟我想投的孩子,给我发了一个chat:https://chat.plexpt.com/chat
我聊了几句,确实要更人一点,人家啥都能干,我就搜了一下
人工智能搜索结果比百度搜索结果更准确_第2张图片
看看人家出的结果,多好,我一眼就知道多线程是怎么个意思,怎么个用法,套一下,我就能实现N个多线程的功能了。

chat 出的多线程请求结果

package com.example.selenide.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MultiThreadedBaiduRequest implements Runnable {

    private final String url;

    public MultiThreadedBaiduRequest(String url) {
        this.url = url;
    }

    @Override
    public void run() {
        HttpURLConnection connection = null;
        try {
            URL urlObj = new URL(url);
            connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            //System.out.println(Thread.currentThread().getName() + " - Response: " + response.toString());
        } catch (Exception e) {
            System.out.println(Thread.currentThread().getName() + " - Error: " + e.getMessage());
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
    public static void main(String[] args) {
        String url = "https://www.baidu.com/";
        int numThreads = 10;
        // 开始时间
        long stime = System.currentTimeMillis();

        for (int i = 0; i < numThreads; i++) {
            Thread t = new Thread(new MultiThreadedBaiduRequest(url));
            t.setName("Thread " + (i+1));
            t.start();
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        System.out.printf("执行时长:%d 毫秒.", (etime - stime));
    }
}

在这里插入图片描述

手工单线程请求

package com.example.selenide.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Demo {
    public static void get(String url){
        HttpURLConnection connection = null;
        try {
            URL urlObj = new URL(url);
            connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //System.out.println(Thread.currentThread().getName() + " - Response: " + response.toString());
        } catch (Exception e) {
            System.out.println(Thread.currentThread().getName() + " - Error: " + e.getMessage());
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
    public static void main(String[] args) {
        String url = "https://www.baidu.com/";
        //int numThreads = 10;
        // 开始时间
        long stime = System.currentTimeMillis();

        for (int i = 0; i < 10; i++) {
            get(url);
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        System.out.printf("执行时长:%d 毫秒.", (etime - stime));

    }
}
`
![在这里插入图片描述](https://img-blog.csdnimg.cn/7162fcfa86cb4703b09fbc46783ca2b5.png)

你可能感兴趣的:(学习)