我最近一直在看Go的goroutines,并认为在Java中有类似的东西会很好.据我所知,并行化方法调用的常用方法是执行以下操作:
final String x = "somethingelse";
new Thread(new Runnable() {
public void run() {
x.matches("something");
}
}).start();
Run Code Online (Sandbox Code Playgroud)
那不是很优雅.有没有更好的方法呢?我在项目中需要这样的解决方案,所以我决定围绕异步方法调用实现自己的包装类.
我在J-Go发布了我的包装类.但我不知道这是不是一个好的解决方案.用法很简单:
SampleClass obj = ...
FutureResult<Integer> res = ...
Go go = new Go(obj);
go.callLater(res, "intReturningMethod", 10); //10 is a Integer method parameter
//... Do something else
//...
System.out.println("Result: "+res.get()); //Blocks until intReturningMethod returns
Run Code Online (Sandbox Code Playgroud)
或者更简洁:
Go.with(obj).callLater("myRandomMethod");
//... Go away
if (Go.lastResult().isReady()) //Blocks until myRandomMethod has ended
System.out.println("Method is finished!");
Run Code Online (Sandbox Code Playgroud)
在内部,我正在使用一个实现Runnable的类,并做一些Reflection工作来获取正确的方法对象并调用它.
我想对我的小型库以及在Java中进行这样的异步方法调用的主题有所了解.安全吗?有没有更简单的方法?
有没有一种简单的方法可以使用Lucene的任何子类Analyzer
来解析/标记化String
?
就像是:
String to_be_parsed = "car window seven";
Analyzer analyzer = new StandardAnalyzer(...);
List<String> tokenized_string = analyzer.analyze(to_be_parsed);
Run Code Online (Sandbox Code Playgroud) 我有一组图像文件,我想将它们的颜色数减少到64.我怎么能用OpenCV做到这一点?
我需要这个,所以我可以使用64大小的图像直方图.我正在实施CBIR技术
我想要的是4位调色板的颜色量化.
这段代码有什么区别:
for(term <- term_array) {
val list = hashmap.get(term)
...
}
Run Code Online (Sandbox Code Playgroud)
和:
for(term <- term_array; val list = hashmap.get(term)) {
...
}
Run Code Online (Sandbox Code Playgroud)
在循环内部我用这样的东西改变了hashmap
hashmap.put(term, string :: list)
Run Code Online (Sandbox Code Playgroud)
虽然list
在使用第二个代码片段时检查它的头部似乎已经过时了.
考虑到:
abstract class Base {
def something() = println("Base")
}
trait TraitA extends Base {
abstract override def something() = { super.something(); println("TraitA"); }
}
class Child extends Base {
override def something() {
println("Child")
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
val x = new Child with TraitA
x.something()
Run Code Online (Sandbox Code Playgroud)
我明白了:
Child
TraitA
Run Code Online (Sandbox Code Playgroud)
但如果我使用:
class Child extends Base with TraitA {
override def something() {
println("Child")
}
}
val x = new Child
x.something()
Run Code Online (Sandbox Code Playgroud)
我只得到:
Child
Run Code Online (Sandbox Code Playgroud)
那么在建筑工地或申报地点使用特征有何不同?在类的声明中扩展/使用特征时,我可以拥有第一个行为(可堆叠的特征)吗?
MapReduce抽象是否适用于处理即使在单个机器中的问题?例如,我有一个12核的机器,我必须计算数千个文件中的单词(经典的MapReduce示例).
考虑到我们正在使用单个硬盘驱动器的单台机器上工作,在多线程中使用Mapper和Reducers的MapReduce实现是解决此问题的好方法吗?
我想我的问题归结为:MapReduce范例仅适用于在一组机器中工作吗?
我正在为博客文章编写一个模板,该文章有线程评论.一种为线程注释编写模板的自然方法,它使用递归方式构造Html.像这样的东西:
@showComment(comment: models.Comment) = {
<div class="comment">
<div class="comment-metadata">
<span class="comment-author">by @comment.author,</span>
<span class="comment-date">
@comment.postedAt.format("dd MMM yy")
</span>
</div>
<div class="comment-content">
<div class="about">Detail: </div>
@Html(comment.content.replace("\n", "<br>"))
</div>
<a href="@action(controllers.Application.replyComment(comment.id()))">Reply</a>
@comments filter { c => c.parent_id == comment.id } map {
c => @showComment(c)
}
</div>
}
Run Code Online (Sandbox Code Playgroud)
问题是使用递归块会产生错误:
引发的错误是:递归方法showComment需要结果类型
如果我尝试在showComment中放置一个返回类型,它会引发这个错误:
引发的错误是:找不到:值showComment
任何解决方法?
我使用JDBC连接发出了一个查询:
Connection conn = null
Class.forName("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection(dbHost, dbUser, dbPass)
s = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
s.setFetchSize(Integer.MIN_VALUE);
r = s.executeQuery (MY_SELECT_QUERY);
while(...) {
processResultSet(r);
}
Run Code Online (Sandbox Code Playgroud)
在while
很长一段时间处理ResultSet中运行。大约1小时后,我得到了例外:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败
从服务器成功接收到的最后一个数据包是74毫秒前。成功发送到服务器的最后一个数据包是4,351,980毫秒之前。
Run Code Online (Sandbox Code Playgroud)at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:407) at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3082) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2968) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3516) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:931) at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:1429) at com.mysql.jdbc.RowDataDynamic.nextRecord(RowDataDynamic.java:416) at com.mysql.jdbc.RowDataDynamic.next(RowDataDynamic.java:395) at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7171) at com.busk.indexer.AbstractIndexerClient.indexResultSet(AbstractIndexerClient.scala:34) at com.busk.indexer.ContinousIndexer.startIndexingLoop(ContinousIndexer.scala:18) at com.busk.indexer.IndexerServer$.main(IndexerServer.scala:95) at com.busk.indexer.IndexerServer.main(IndexerServer.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)
我已经四处搜寻,但是几乎所有错误都与连接池有关,并且使用了8小时以上的连接(默认的wait_timeout mysql设置)。就我而言,我处于ResultSet处理的中间,并且出现此错误。
我在AWS上有一个MySQL m2.2xlarge实例.MySQL数据目录位于根EBS /中.它是单个EBS而不是RAID.我们有三个主要表格.其中一个,Table C
内容最大,仅使用最后几天的数据.这些表中的插入速率大约为80.000行.这3个表有大约4200万行.innodb_buffer_pool_size具有~30GB的实例RAM.
Table A
是最重要的,它的数据长度是~33GB,索引~11GB的
Table B
数据长度是~8GB,索引~5GB
在我们的网站上,两个主要的查询(延迟方面)是这样的:
SELECT * FROM TableA WHERE id in (.....)
SELECT * FROM TableB JOIN .... WHERE id in (.....)
Run Code Online (Sandbox Code Playgroud)
在大多数页面中,(...)将是大约50个最近的ID,这些查询每个小于50毫秒.但在其他一些页面中,我们遇到了较旧的ID,这些查询的延迟时间猛增至500毫秒,800毫秒,最长1.5秒.
我做了一个测试,在Mysql重启之后,我做了一个SELECT id FROM TableB
强制索引进入缓存/内存.该Table B
查询将仍然是缓慢的.然后我做了一个SELECT * FROM TableB
.现在,在缓存/内存中的整个表中,查询变得非常快(<50ms).
我的问题:> 500毫秒,> 1000毫秒是一个合理的延迟,只是通过PRIMARY KEY检索行的查询?即使在42M的桌子上?即使所有行都在磁盘中?对我来说似乎太过分了.
将MySQL数据移动到短暂存储(/ mnt)会改善这个吗?使用预配置IOPS会有帮助吗?
我正在向httplib2拥有的REST服务发出HTTPS GET请求,但是我们收到错误:
[Errno 8] _ssl.c:504: EOF occurred in violation of protocol
Run Code Online (Sandbox Code Playgroud)
所有其他客户端运行良好(浏览器,Java客户端等...),但需要将PHP curl设置为使用SSL v3.
我已经四处搜索,似乎确实是关于SSL版本的错误,但我似乎找不到在httplib2中更改它的方法.除了更改源代码中的以下行之外,还有什么办法吗?
# We should be specifying SSL version 3 or TLS v1, but the ssl module
# doesn't expose the necessary knobs. So we need to go with the default
# of SSLv23.
return ssl.wrap_socket(sock, keyfile=key_file, certfile=cert_file,
cert_reqs=cert_reqs, ca_certs=ca_certs)
Run Code Online (Sandbox Code Playgroud) 我正在构建一个使用C++的系统,它使用Tokyo Cabinet(C中的原始API).问题是我想存储一个类,如:
class Entity {
public:
string entityName;
short type;
vector<another_struct> x;
vector<another_struct> y
vector<string> z;
};
Run Code Online (Sandbox Code Playgroud)
问题是向量和字符串具有可变长度.当我将void*(我的对象)传递给Tokyo Cabinet以便它可以存储它时,我还必须以字节为单位传递对象的大小.但这不可能轻而易举地完成.
确定对象字节数的最佳方法是什么?或者在东京内阁存储可变长度物体的最佳方法是什么.
我已经在考虑寻找序列化库.
谢谢
我想在Lucene的相似性方程中插入另一个得分因子.问题是我不能只是覆盖Similarity类,因为它不知道它是计算分数的文档和术语.
例如,在包含以下文本的文档中:
The cat is in the top of the tree, and he is going to stay there.
Run Code Online (Sandbox Code Playgroud)
我有一个自己的算法,为每个人分配本文档中的术语,分别表示每个术语对整个文档的重要程度.每个单词的可能得分是:
cat: 0.789212
tree: 0.633423
top: 0.412315
stay: 0.123912
there: 0.0999842
going: 0.00988412
...
Run Code Online (Sandbox Code Playgroud)
每个单词的得分因文档而异.例如,在另一个文档中cat
可能有得分:0.0023912
我想把这个分数加到Lucene的得分上,但我对如何做到这一点感到很失望.
有小费吗?
我正在尝试构建/运行一个旧的C++系统.我已经能够使用g ++ 4.4在Ubuntu 9.10中构建它
现在我正在尝试使用g ++ 4.2构建Ubuntu 8.04.我收到以下错误:
stringmap.h:353: erro: ISO C++ forbids declaration of ‘iterator’ with no type
stringmap.h:353: erro: extra qualification ‘stringmap<_Tp>::’ on member ‘iterator’
stringmap.h:353: erro: expected ‘;’ before ‘begin’
stringmap.h:354: erro: ISO C++ forbids declaration of ‘iterator’ with no type
stringmap.h:354: erro: extra qualification ‘stringmap<_Tp>::’ on member ‘iterator’
stringmap.h:354: erro: expected ‘;’ before ‘end’
stringmap.h:386: erro: no ‘typename stringmap<_Tp>::iterator stringmap<_Tp>::begin()’ member function declared in class ‘stringmap<_Tp>’
stringmap.h:386: erro: template definition of non-template ‘typename stringmap<_Tp>::iterator stringmap<_Tp>::begin()’
stringmap.h:398: …
Run Code Online (Sandbox Code Playgroud) java ×3
scala ×3
c++ ×2
concurrency ×2
lucene ×2
mysql ×2
algorithm ×1
amazon-ec2 ×1
analyzer ×1
asynchronous ×1
build ×1
cbir ×1
colors ×1
compilation ×1
for-loop ×1
generics ×1
goroutine ×1
httplib2 ×1
jdbc ×1
mapreduce ×1
opencv ×1
python ×1
scoring ×1
sizeof ×1
ssl ×1
templates ×1
tokenize ×1
traits ×1