日期:2014-05-20 浏览次数:20800 次
private boolean flag=true;
public void setFlase(){
this.flag=false;
}
public void setTrue(){
this.flag=true;
}
public boolean getFlag(){
return flag;
}
public void run(){
while(true){
try {
Thread.currentThread().sleep(3000);//每个100毫秒刷新一次。标准为3000毫秒发射一个
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
this.setTrue();
//System.out.println("置为true");//等待超过三秒,把Flag置为true
}
}
class T extends Thread{
boolean flag=false;
public void run(){
while(true){
if(!flag){
flag=true;
try{
Thread.currentThread().sleep(3000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(flag);
}
flag=false;
}
}
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class TheLock implements Runnable{