我的测试程序程序中,有一个是Requestor,它想Processor发一个事件,然后进入等待状态,由Processor处理这个事件后唤醒.Processor类是一个事件处理中心,接收事件,并唤醒(notify)等待的requestor.我发现有些Requestor不能被唤醒,为什么./*
* @(#)MyNotifer.java 1.0 04/02/08
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_1\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
import java.util.*;class MyNotifer {
public MyNotifer() {
} public static void main(String args[]) {
System.out.println("Starting MyNotifer...");
for(int i = 0; i <10000; i ++)
{
(new Thread((new Requestor()))).start();
if(i % 100 == 0)
{
try{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}class Requestor implements Runnable
{
public void run()
{
Object request = new Object();
EventObject evt = new EventObject(request);
Processor.getInstance().putEvent(evt);
synchronized(request)
{
long start = System.currentTimeMillis();
try
{
request.wait(5000);
}
catch(Exception e)
{
System.out.println(e);
}
long end = System.currentTimeMillis();
if(end - start > 1000)
{
System.out.println("request last too long " +(end - start));
}
}
}
}class Processor extends Thread
{
private Queue evtsQueue = new Queue(); private static Processor instance = null;
private static Object initLock = new Object(); boolean bShutdown = false;
private static int notifyCount = 0;
public static Processor getInstance()
{
if(instance == null)
{
synchronized(initLock)
{
if(instance == null)
{
instance = new Processor();
instance.start();
}
}
}
return instance;
}
public void putEvent(EventObject evt)
{
synchronized(evtsQueue)
{
evtsQueue.enqueue(evt);
evtsQueue.notifyAll();
}
}
public EventObject getEvent()
{
EventObject evt = null; synchronized(evtsQueue)
{
try
{
if(!evtsQueue.isEmpty())
{
evt = (EventObject) evtsQueue.dequeue();
}
else
{
evtsQueue.wait(100);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
} return evt;
}
public void run()
{
EventObject evt = null;
System.out.println("Processer Start...");
while(!bShutdown)
{
if((evt=getEvent()) != null)
{
try
{
handleEvent(evt);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
private void handleEvent(EventObject evt)
{
Object request = evt.getSource(); synchronized(request)
{
try
{
request.notify();
}
catch(Exception e)
{
System.out.println(e);
}
notifyCount++ ;
if(notifyCount %100 == 0)
{
System.out.println("NOTIFY COUNT " + notifyCount);
}
}
}
}class Queue
{
private int maxQueueSize;
private LinkedList queueData;
private Object mutex; public Queue()
{
maxQueueSize = 0;
queueData = new LinkedList();
mutex = this;
} public Queue(int maxSize)
{
maxQueueSize = 0;
queueData = new LinkedList();
maxQueueSize = maxSize;
mutex = this;
} public int size()
{
int i;
synchronized(mutex)
{
i = queueData.size();
}
return i;
} public boolean isEmpty()
{
boolean flag;
synchronized(mutex)
{
flag = queueData.isEmpty();
}
return flag;
} public Object dequeue()
{
Object obj1;
synchronized(mutex)
{
Object first = null;
if(size() > 0)
first = queueData.removeFirst();
obj1 = first;
}
return obj1;
} public Object dequeue(Object obj)
{
Object found = null;
synchronized(mutex)
{
found = find(obj);
if(found != null)
queueData.remove(found);
}
return found;
} public void enqueue(Object obj)
throws IndexOutOfBoundsException
{
synchronized(mutex)
{
if(maxQueueSize > 0 && size() >= maxQueueSize)
throw new IndexOutOfBoundsException("Queue is full. Element not added.");
queueData.add(obj);
}
} public Object find(Object obj)
{
synchronized(mutex)
{
for(ListIterator iter = queueData.listIterator(0); iter.hasNext();)
{
Object current = iter.next();
if(current.equals(obj))
{
Object obj2 = current;
return obj2;
}
}
}
return null;
}
}
* @(#)MyNotifer.java 1.0 04/02/08
*
* You can modify the template of this file in the
* directory ..\JCreator\Templates\Template_1\Project_Name.java
*
* You can also create your own project template by making a new
* folder in the directory ..\JCreator\Template\. Use the other
* templates as examples.
*
*/
import java.util.*;class MyNotifer {
public MyNotifer() {
} public static void main(String args[]) {
System.out.println("Starting MyNotifer...");
for(int i = 0; i <10000; i ++)
{
(new Thread((new Requestor()))).start();
if(i % 100 == 0)
{
try{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
}class Requestor implements Runnable
{
public void run()
{
Object request = new Object();
EventObject evt = new EventObject(request);
Processor.getInstance().putEvent(evt);
synchronized(request)
{
long start = System.currentTimeMillis();
try
{
request.wait(5000);
}
catch(Exception e)
{
System.out.println(e);
}
long end = System.currentTimeMillis();
if(end - start > 1000)
{
System.out.println("request last too long " +(end - start));
}
}
}
}class Processor extends Thread
{
private Queue evtsQueue = new Queue(); private static Processor instance = null;
private static Object initLock = new Object(); boolean bShutdown = false;
private static int notifyCount = 0;
public static Processor getInstance()
{
if(instance == null)
{
synchronized(initLock)
{
if(instance == null)
{
instance = new Processor();
instance.start();
}
}
}
return instance;
}
public void putEvent(EventObject evt)
{
synchronized(evtsQueue)
{
evtsQueue.enqueue(evt);
evtsQueue.notifyAll();
}
}
public EventObject getEvent()
{
EventObject evt = null; synchronized(evtsQueue)
{
try
{
if(!evtsQueue.isEmpty())
{
evt = (EventObject) evtsQueue.dequeue();
}
else
{
evtsQueue.wait(100);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
} return evt;
}
public void run()
{
EventObject evt = null;
System.out.println("Processer Start...");
while(!bShutdown)
{
if((evt=getEvent()) != null)
{
try
{
handleEvent(evt);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
}
private void handleEvent(EventObject evt)
{
Object request = evt.getSource(); synchronized(request)
{
try
{
request.notify();
}
catch(Exception e)
{
System.out.println(e);
}
notifyCount++ ;
if(notifyCount %100 == 0)
{
System.out.println("NOTIFY COUNT " + notifyCount);
}
}
}
}class Queue
{
private int maxQueueSize;
private LinkedList queueData;
private Object mutex; public Queue()
{
maxQueueSize = 0;
queueData = new LinkedList();
mutex = this;
} public Queue(int maxSize)
{
maxQueueSize = 0;
queueData = new LinkedList();
maxQueueSize = maxSize;
mutex = this;
} public int size()
{
int i;
synchronized(mutex)
{
i = queueData.size();
}
return i;
} public boolean isEmpty()
{
boolean flag;
synchronized(mutex)
{
flag = queueData.isEmpty();
}
return flag;
} public Object dequeue()
{
Object obj1;
synchronized(mutex)
{
Object first = null;
if(size() > 0)
first = queueData.removeFirst();
obj1 = first;
}
return obj1;
} public Object dequeue(Object obj)
{
Object found = null;
synchronized(mutex)
{
found = find(obj);
if(found != null)
queueData.remove(found);
}
return found;
} public void enqueue(Object obj)
throws IndexOutOfBoundsException
{
synchronized(mutex)
{
if(maxQueueSize > 0 && size() >= maxQueueSize)
throw new IndexOutOfBoundsException("Queue is full. Element not added.");
queueData.add(obj);
}
} public Object find(Object obj)
{
synchronized(mutex)
{
for(ListIterator iter = queueData.listIterator(0); iter.hasNext();)
{
Object current = iter.next();
if(current.equals(obj))
{
Object obj2 = current;
return obj2;
}
}
}
return null;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货