67,513
社区成员
发帖
与我相关
我的任务
分享
public class ATread extends Thread{
private boolean needToSleep = false;//是否需要sleep
private boolean isThreadSleeping = false;//是否在sleep
public boolean runFlag = false;
private String ip = "";
public ATread(String ip){
this.ip = ip;
}
public void run(){
try {
runFlag = true;
while(runFlag) {
doXXX(); //使用当前ip进行操作
//用于修改ip时的等待控制
while(needToSleep){
isThreadSleeping=true;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}finally{
//释放资源
}
}
public synchronized boolean changeLockInfo() {
boolean result = false;
this.needToSleep = true;//设置需要暂停
if(runFlag){
while( !isThreadSleeping ){
try {
//本意是让当前线程暂停,然后上面的run方法可以进入while循环并sleep,那么此线程可以继续往下执行
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return doXXX();//其他操作
}
}
}
public class BClass{
//修改Ip操作
public static boolean changeIp() {
boolean result = false;
//threadMap用来存放所有的线程,可以根据key值得到对应的AThread
AThread athread = threadMap.get(Key);
result = athread.changeLockInfo();
return result;
}
}