不好意思, 没写完。我觉得这样用sleep等在那里,好像实在占用系统资源多。 请问我怎么样让这个类的put()韩树被调用一次后, 自动激活get()函数继续运行下去? 请问可以做到吗? wait()和notify()这样的函数在这里用的上吗?
解决方案 »
- java webservice
- 麻烦大家帮我修改下多线程的代码
- 在spring quartz 如何控制两个调度任务的自动
- 如何统一ssh编码
- webservices缓存问题如何解决?
- 我用java调用.net的web service,上报数据的时候有中文参数的问题怎么解决?
- 有人用过Sun Java Studio Creator中文版吗?
- 求救高手!用hibernate怎么把.hbm文件转换成相应的.java文件?
- ejb真的好吗? 测试结果说明一切
- 求解关于bootstrap-multiselect的样式冲突问题
- 在JBuilder 6 中如何将Bean代码打包成Jar文件?
- 怎么知道几个复选框中选择了哪几项?
private int capacity;
public synchronized boolean isFull() {
return ( size == capacity );
}
public synchronized void add(Object obj) throws InterruptedException {
while ( isFull() ) {
wait();
}
queue[head] = obj;
head = ( head + 1 ) % capacity;
size++;
notifyAll(); // let any waiting threads know about change
}
public synchronized Object remove() throws InterruptedException {
while ( size == 0 ) {
wait();
}
Object obj = queue[tail];
queue[tail] = null; // don’t block GC by keeping reference
tail = ( tail + 1 ) % capacity;
size—;
notifyAll(); // let any waiting threads know about change
return obj;
}
还有, 这个类要从Thread继承出来吗? 那run()函数就这么空着也可以阿!?
private LinkedList list = new LinkedList(); public Queue() {
} public synchronized void put(Object v){
list.addFirst(v);
this.notify();
} public Object get(){
while (true){
synchronized(this){
if ( !isEmpty() )
return list.removeLast();
}
try{
wait();
}catch(Exception e){
System.err.println(e.toString());
}
}
} public boolean isEmpty(){
return list.isEmpty();
} //for debug
public static void main(String[] args) {
Queue queue1 = new Queue(); for(int i=0;i<10;i++)
queue1.put(Integer.toString(i)); while(!queue1.isEmpty())
System.out.println(queue1.get());
}
}
而且书上也说不可以在非线程类中使用wait()函数的嘛,虽然这是object的最近本的方法
public class SimpleObjectFIFO extends Object {
private Object[] queue;
private int capacity;
private int size;
private int head;
private int tail; public SimpleObjectFIFO(int cap) {
capacity = ( cap > 0 ) ? cap : 1; // at least 1
queue = new Object[capacity];
head = 0;
tail = 0;
size = 0;
} public synchronized int getSize() {
return size;
} public synchronized boolean isFull() {
return ( size == capacity );
} public synchronized void add(Object obj) throws InterruptedException {
while ( isFull() ) {
wait();
} queue[head] = obj;
head = ( head + 1 ) % capacity;
size++; notifyAll(); // let any waiting threads know about change
} public synchronized Object remove() throws InterruptedException {
while ( size == 0 ) {
wait();
}
Object obj = queue[tail];
queue[tail] = null; // don't block GC by keeping unnecessary reference
tail = ( tail + 1 ) % capacity;
size--; notifyAll(); // let any waiting threads know about change return obj;
} public synchronized void printState() {
StringBuffer sb = new StringBuffer(); sb.append("SimpleObjectFIFO:\n");
sb.append(" capacity=" + capacity + "\n"); sb.append(" size=" + size);
if ( isFull() ) {
sb.append(" - FULL");
} else if ( size == 0 ) {
sb.append(" - EMPTY");
}
sb.append("\n"); sb.append(" head=" + head + "\n");
sb.append(" tail=" + tail + "\n"); for ( int i = 0; i < queue.length; i++ ) {
sb.append(" queue[" + i + "]=" + queue[i] + "\n");
} System.out.print(sb);
}
}
可是 我在客户端使用一个线程不断地从服务器端读取任务的时候,如果
当前队列里面为空,那么这个客户端程序被挂起的时候,我本来以为不
会占用多少资源的,结果发现CPU占用率一直是100%, 这可怎么办?
能解决吗? 这样对客户端程序的其他操作肯定影响比较大的吧:(
to Norwaywoods() :
这个东东我也不知道该贴哪一段比较好,实在是太多了:(
我上面没有说清楚吗?我的意思是比如 服务器端的队列的get()方法被客户端调用, 但是正好
这个时候服务器端的队列空了, 于是这个客户端的调用过程自然就被挂
起。 出于我写的程序的要求,客户端的类继承了Thread方法,这个线程
的任务就是不停地从服务器端得到队列中的任务。 但是这个线程在运行而且被挂起的时候,CPU占用率实在是太吓人了,我
很担心会影响到主程序的运行,所以我想请问是否能够避免这么多的处理器
时间的占用? 不知道我描述清楚了没有,
XMLOutputter xo = new XMLOutputter();
String a = xo.outputString(doc);
然后直接传送string 的值, 这是SOAP支持的基本类型。 我就是用这
种方法的,没有对Document对象进行序列化。
现在我做这个东西的时间比较紧,所以没有空研究这些,如果有空我
会花点时间试试如何序列化的,成功了的话我再把方法贴出来。 不过本人能力... ... :(
我试过了, 500服务器错误
public String sendRegister(String str){
//return (String)QRegister.get();
String res="";
try{
res = (String)QRegister.get();
return res;
}catch(Exception e){
System.out.println(e.toString()+"Client Terminated");
try{
QRegister.put(res);
}catch(Exception e2){System.err.println(e2.toString());}
}
return "";
}
public class RegReceiver extends Thread{ Queue RegInfo = new Queue(100); public Document getRegInfo(){
Document res = new Document();
try{
res = (Document)RegInfo.get();
}catch(Exception e){System.err.println(e.toString());}
return res;
} public boolean isEmpty(){
if (RegInfo.getSize()==0) return true;
else return false;
} public void run(){
while(true){
System.out.println("Attempt to Receive info");
String endpoint ="http://"+IPAddress+":8080/axis/services/Server";
Service service = new Service();
while (true){
System.out.println("Connecting Server"); try{
Call call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName("sendRegister");
String Parameter = "";
String res = (String) call.invoke( new Object[] { Parameter } ); System.out.println("received"+res);
SAXBuilder ix = new SAXBuilder();
Document doc = ix.build(new StringReader(res)); }
catch(Exception e){
System.err.println(e.toString);
}
}
}
} public static void main(String[] args) {
RegReceiver reg = new RegReceiver(); //reg.setServerAddress(args[0]);
reg.start();
while(true){
Document doc = new Document();
if (!reg.isEmpty()) doc = reg.getRegInfo(); XMLOutputter out = new XMLOutputter();
try{
out.output(doc,System.out);
}catch(Exception e){ }
}
}
}
把这个放在console环境中运行很占资源, 可是放到有Frame的application中去运行这个线程(比如,单击button开始运行这个线程) 就不会有这样的问题,CPU占用率就很低虽然是没事了, 可是谁知道怎么回事吗?