多线程切换ip时 原子操作最大的子账号自减(AtomicInteger.getAndDecrement() 是返回自减之前的值故操作不到最小的子账号)
所以线程数=最大的子账号减去最小的子账号再+1

package com.example.dynamicscheduledemo.utils;

import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @title: Switchip
 * @Author kunkun
 * @Date: 2022/4/28 15:24
 * @Version 1.0
 */
public class Switchip {
    private static OkHttpClient client = OkhttpUtils.okHttpClient();

    public static void main(String[] args) throws InterruptedException {
        String user = "huangkun1zz-";
        String pass = "52B4EG20";
        String country = "in";
        Integer start = 0;
        Integer end = 99;
        switchIPThread(user,pass,country,start,end,client);
    }

    public static void switchIPThread(String user,String pass,String country,Integer start,Integer end,OkHttpClient client) throws InterruptedException {
        int threadNums = (end - start)+1;//应为是自减操作所以需要多一个线程才能切换到start的子账户
        AtomicInteger count = new AtomicInteger(end);//end的子账户开始自减
        Runnable taskTemp = new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    String authUser = user + (count.getAndDecrement());
                    String qiehuanIp = "http://s.hide-proxy.pro/index/switchIp?username="+authUser+"&password="+pass+"&cc="+country;
                    Request request = new Request.Builder()
                            .url(qiehuanIp)
                            .method("GET", null)
                            .build();
                    Response response = client.newCall(request).execute();
                    if(response.code() == 200){
                        System.out.println("当前子账号是: "+authUser+"切换代理成功 "+response.body().string());
                    }
                }
            };
            startTaskAllInOnce(threadNums, taskTemp);
    }



    public static long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
        final CountDownLatch startGate = new CountDownLatch(1);
        final CountDownLatch endGate = new CountDownLatch(threadNums);
        for(int i = 0; i < threadNums; i++) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        // 使线程在此等待,当开始门打开时,一起涌入门中
                        startGate.await();
                        try {
                            task.run();
                        } finally {
                            // 将结束门减1,减到0时,就可以开启结束门了
                            endGate.countDown();
                        }
                    } catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                }
            };
            t.start();
        }
        long startTime = System.nanoTime();
        System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
        // 因开启门只需一个开关,所以立马就开启开始门
        startGate.countDown();
        // 等等结束门开启
        endGate.await();
        long endTime = System.nanoTime();
        System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
        return endTime - startTime;
    }
}

上一篇 下一篇