📄 multithreadedrequester.java
字号:
/*
* Created on Jan 5, 2004
*
*/
package com.sri.oaa2.test.basic;
import com.sri.oaa2.com.LibCom;
import com.sri.oaa2.com.LibComTcpProtocol;
import com.sri.oaa2.icl.IclInt;
import com.sri.oaa2.icl.IclList;
import com.sri.oaa2.icl.IclStr;
import com.sri.oaa2.icl.IclStruct;
import com.sri.oaa2.icl.IclTerm;
import com.sri.oaa2.lib.LibOaa;
/**
* @author agno
*
*/
public class MultiThreadedRequester
{
private LibOaa oaa_;
private int echoNumber_ = 0;
public void init()
{
LibCom com = new LibCom(new LibComTcpProtocol(), null);
oaa_ = new LibOaa(com);
if(!oaa_.getComLib().comConnect("parent", IclTerm.fromString(true, "tcp(Host,Port)"), new IclList())) {
throw new RuntimeException("Could not connect to parent");
}
StringBuffer buf = new StringBuffer("MultiThreadedRequester");
buf.append(echoNumber_);
if(!oaa_.oaaRegister("parent", buf.toString(), new IclList(), new IclList())) {
throw new RuntimeException("Could not register empty list of solvables");
}
oaa_.oaaReady(true);
}
public synchronized void makeRequest()
{
System.out.println("makeRequest for number " + echoNumber_ + " in thread " + Thread.currentThread().getName());
this.init();
IclList results = new IclList();
oaa_.oaaSolve(new IclStruct("echo", new IclInt(echoNumber_), new IclStr(Thread.currentThread().getName())), new IclList(), results);
++echoNumber_;
if(results.size() > 0) {
System.out.println("Got a result: " + results.toString());
}
oaa_.oaaDisconnect(new IclList());
oaa_ = null;
}
private final static int MAXCALLS = 1000;
public static void main(String[] args) throws InterruptedException
{
final MultiThreadedRequester mtr = new MultiThreadedRequester();
mtr.init();
Thread t1 = new Thread("t1") {
public void run()
{
for(int i = 0; i < MAXCALLS; ++i) {
mtr.makeRequest();
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
Thread t2 = new Thread("t2") {
public void run()
{
for(int i = 0; i < MAXCALLS; ++i) {
mtr.makeRequest();
try {
Thread.sleep(50);
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
t1.start();
t2.start();
t1.join();
t2.join();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -