关于控制Java多线程的问题...

来源:百度知道 编辑:UC知道 时间:2024/06/06 13:58:19
起四个线程控制数据抓取...
但是被抓的网站只允许一分钟内40次以内的抓取...
否则会被封...
请问怎么解决?
(最好付程序代码...)

加一个int表示已抓取次数,每次抓取前检测是否超过40,一个主线程1分钟一次清零那个int。

public class test {

static int i;
static boolean run = true; //用它操作进程结束

public static void main(String[] args) {
thread t1 = new thread();
//……
t1.start();
//……
try {
while(run) {
i = 0;
Thread.sleep(1000);
}
} catch(Exception ex) {
ex.printStackTrace();
}

public class thread extends Thread {

public void run() {
while(test.run) {
synchronized(test.i) {
if(test.i < 40) {
//your code
}
}
}
}
}

线程sleep()就好了。至于sleep多长时间,看你的了。