日期:2014-05-20  浏览次数:20744 次

线程实现开门后三秒自动关门 在线等
线程要实现 通过系统开锁 三秒后自动关锁

------解决方案--------------------

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
}
}

------解决方案--------------------

if(opendoor()==true){
 try {                 
Thread.currentThread().sleep(3000);//线程休眠3000毫秒       
      } catch (Exception e) { 
                // TODO: handle exception        
         e.printStackTrace();       
      } 
closedoor();
}
opendoor和closedoor其实就是get和set方法。控制标记位flag这把锁
------解决方案--------------------

  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{