有一段逻辑:
在一段时间之内(比如5分钟),如果在一个map中有值,就跳出等待,如果没有值,就等待5分钟,返回空。
如何写这个逻辑,要求高效,谢绝for循环时间然后sleep,谢谢!
wait/NOTIFY 观察者模式
不用sleep如何休眠?
[code="java"]
package mains;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest {
static int index = 0;
private static Map<String, Object> map;
public static void main(String[] args) {
map = new HashMap<String, Object>();
//模拟启动线程改变map的值
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
int i = new Random().nextInt(20);
System.out.println(i);
if(i == 17) {
map.put("ret", "value");
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(map.get("ret") == null) {
System.out.println("map's value is null. continue.");
} else {
System.out.println(map.get("ret") + " find the value. exit.");
timer.cancel();
}
}
}, 0, 1000);
}
}
[/code]
我上面的代码是1秒钟检查一次,时间你具体设置
[code="java"]
package com.huawei.dom4j.test1;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class QueryTest {
private static Map map = Collections.synchronizedMap(new HashMap());
private static boolean flag = false;
private static String retVlue = null;
public static void main(String[] args) {
putValue();
queryValue();
}
public static void putValue(){
new Thread(){
@Override
public void run() {
super.run();
Random random = new Random();
while(true){
int i = random.nextInt(200);
if(i==10){
System.out.println("already get in .....");
if(map.isEmpty()){
map.put("key", "value");
System.out.println("already put key and value");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if(flag){
break;
}
}
}
}.start();
}
public static String queryValue(){
String ret = null;
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
retVlue = QueryTest.map.remove("key");
QueryTest.flag = true;
}
}, 5*1000*60);
ret = retVlue;
retVlue = null;
return ret;
}
}
[/code]
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Test {
Object lock=new Object();
private static Map map = Collections.synchronizedMap(new HashMap());
static boolean flag=true;
static int count=0;
private class CreateThread extends Thread
{
public CreateThread(String name)
{
super(name);
}
@Override
public void run()
{
while(flag)
{
synchronized(lock)
{
if(map.isEmpty())
{
map.put("key", count+"");
count++;
System.out.println("计数器:"+count);
System.out.println("不为空唤醒在监视器上等待的线程");
lock.notify();
}
}
if(count==10)
{
System.out.println("计数器为:"+count+"清除map");
map.remove("key");
try
{
Thread.sleep(100*60*10) ;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
try
{
Thread.sleep(1000) ;
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
private class CheckThread extends Thread
{
public CheckThread(String name)
{
super(name);
}
@Override
public void run()
{
while(flag)
{
synchronized(lock)
{
if(!map.isEmpty())
{
System.out.println("map不空,清除..."+map.get("key"));
map.remove("key");
}
else
{
try
{
System.out.println("map为空等待中(5分钟)...");
lock.wait(100*60*5);
if(!map.isEmpty())
{
System.out.println("等待过程钟扫描到map不为空...");
System.out.println(map.get("key"));
}
else
{
System.out.println("等待超时,返回null");
flag=false;
map=null;
return;
}
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public void start()
{
CreateThread c=new CreateThread("CreateThread:");
c.start();
CheckThread ck=new CheckThread("CheckThread:");
ck.start();
}
public static void main(String[] args)
{
new Test().start();
}
}
[code="java"]
protected void procss(Document document) throws IOException{
getWriter().write(document);
getWriter().flush();
}[/code]