代码如下:
public class Test { private static Test instance = null;
private boolean stopFlag = false;
ArrayList users = new ArrayList();
Object syncObj = new Object();

public static Test getInstance()
{
if(instance == null)
instance = new Test();
return instance;
}

public Test()
{
InspetUser inspectUser = new InspetUser();
inspectUser.start();
}

public void add(Object data)
{
synchronized(syncObj)
{
//进行添加操作
}
}

public void remove(Object data)
{
synchronized(syncObj)
{
//进行移出操作
}
}

public int size()
{
synchronized(syncObj)
{
return users.size();
}
}

class InspetUser extends Thread
{
public void run()
{
long sleepTime = 5* 60 * 1000;
while(!stopFlag)
{
synchronized(syncObj)
{
Inspect();
}

try
{
sleep(sleepTime);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}

private void Inspect()
{
//使用users(ArrayList)中的数据进行检测操作
}
}
}
以上代码主要是用来监测登陆的用户,并隔一段时间监测用户的访问情况(用线程InspetUser监测)