线程实现开门后三秒自动关门 在线等 线程要实现 通过系统开锁 三秒后自动关锁 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 开锁成功 会返回一个标志位 true 或者 false 如果返回的为true 则 三秒后自动上锁 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 } } boolean result = openDoor()if(true == result ){ 等待三秒 closeDoor()} 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{ private Condition openCondition; private Condition closeCondition; private Lock theLock; public TheLock(){ theLock = new ReentrantLock(); openCondition = theLock.newCondition(); closeCondition = theLock.newCondition(); } @Override public void run() { new Thread(new Open(theLock, openCondition)).start(); new Thread(new Close(theLock, closeCondition)).start(); while(true){ try { if (openTheLock() == true){ theLock.lock(); openCondition.signalAll(); theLock.unlock(); Thread.sleep(3000); theLock.lock(); closeCondition.signalAll(); theLock.unlock(); }else{ System.out.println("Open action failed"); } Thread.sleep(100); } catch (InterruptedException ex) { ex.printStackTrace(); theLock.unlock(); } } } private boolean openTheLock(){ return (System.nanoTime() % 3) == 1; } class Open implements Runnable{ private Lock lock; private Condition openCondition; Open(Lock lock, Condition openCondition){ this.lock = lock; this.openCondition = openCondition; } @Override public void run() { while (true){ lock.lock(); try { this.openCondition.await(); onOpen(); } catch (InterruptedException ex) { ex.printStackTrace(); } finally { lock.unlock(); } } } protected void onOpen(){ System.out.println("Opened"); } } class Close implements Runnable{ private Lock lock; private Condition closeCondition; Close(Lock lock, Condition closeCondition){ this.lock = lock; this.closeCondition = closeCondition; } @Override public void run() { while (true){ lock.lock(); try { this.closeCondition.await(); onClose(); } catch (InterruptedException ex) { ex.printStackTrace(); } finally { lock.unlock(); } } } protected void onClose(){ System.out.println("Closed"); } } public static void main(String[] args){ new Thread(new TheLock()).start(); }} 重载HashSet后,如何限定元素类型? 一个小js问题 JCreator怎样才能可以调整选中块的缩进? 求助帮解决这个问题 高分求金额正则表达式,在线等待 MSSQL的JDBC连接问题 为何编译就是不过? Swing 中的JList 下拉菜单等用法求救???? 有两个按钮,我点选其中一个,如何使之触发另一个按钮的事件? tomcat有什么用 JDK有什么关系??菜问题 请教:继承关系中,子类向上转型为父类后,访问的成员变量的问题 java,求解决。使用了未经检查或不安全的操作呢?请使用-Xlint:unchecked 重新编译.
如果返回的为true 则 三秒后自动上锁
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
}
}
{
等待三秒
closeDoor()
}
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{ private Condition openCondition;
private Condition closeCondition;
private Lock theLock;
public TheLock(){
theLock = new ReentrantLock();
openCondition = theLock.newCondition();
closeCondition = theLock.newCondition();
}
@Override
public void run() {
new Thread(new Open(theLock, openCondition)).start();
new Thread(new Close(theLock, closeCondition)).start();
while(true){
try {
if (openTheLock() == true){
theLock.lock();
openCondition.signalAll();
theLock.unlock();
Thread.sleep(3000);
theLock.lock();
closeCondition.signalAll();
theLock.unlock();
}else{
System.out.println("Open action failed");
}
Thread.sleep(100);
} catch (InterruptedException ex) {
ex.printStackTrace();
theLock.unlock();
}
}
}
private boolean openTheLock(){
return (System.nanoTime() % 3) == 1;
}
class Open implements Runnable{
private Lock lock;
private Condition openCondition;
Open(Lock lock, Condition openCondition){
this.lock = lock;
this.openCondition = openCondition;
}
@Override
public void run() {
while (true){
lock.lock();
try {
this.openCondition.await();
onOpen();
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.unlock();
}
}
}
protected void onOpen(){
System.out.println("Opened");
}
}
class Close implements Runnable{
private Lock lock;
private Condition closeCondition;
Close(Lock lock, Condition closeCondition){
this.lock = lock;
this.closeCondition = closeCondition;
}
@Override
public void run() {
while (true){
lock.lock();
try {
this.closeCondition.await();
onClose();
} catch (InterruptedException ex) {
ex.printStackTrace();
} finally {
lock.unlock();
}
}
}
protected void onClose(){
System.out.println("Closed");
}
}
public static void main(String[] args){
new Thread(new TheLock()).start();
}
}