我不记得究竟它是一个常见的模式,但我有一个类(工厂方法模式),它具有根据枚举参数创建其他类(抽象工厂模式)的方法:
public class FooFactoryFactory {
public FooFactory createFactory (FooFactoryType type) {
switch (type) {
case AFoo:
return new AFooFactory ();
break;
case BFoo:
return new BFooFactory ();
break;
default:
throw new RuntimeException ("...");
}
}
}
public interface FooFactory {
Foo createFoo ();
FooItem createFooItem ();
}
Run Code Online (Sandbox Code Playgroud)
FooFactory有几个实现以及Foo接口和FooItem接口(常见的抽象工厂模式).
那么,如何重命名FooFactoryFactory呢?
也许,FooFactoryCreator?(在写这个问题时想一想这个名字).恕我直言,这很好,您怎么看?
我需要让我的网络应用程序使用非常庞大的数据集.目前我得到的是OutOfMemoryException或输出1-2分钟.
让我们把它简单,假设我们有在DB 2个表:Worker并WorkLog在第一个约1000列,并在第二个10个000 000行.后期表有几个字段,包括'workerId'和'hoursWorked'字段等.我们需要的是:
计算每个用户的总工作小时数;
每个用户的工作时间列表.
纯SQL中每个任务最直接的方法(IMO)是:
1)
select Worker.name, sum(hoursWorked) from Worker, WorkLog
where Worker.id = WorkLog.workerId
group by Worker.name;
//results of this query should be transformed to Multimap<Worker, Long>
Run Code Online (Sandbox Code Playgroud)
2)
select Worker.name, WorkLog.start, WorkLog.hoursWorked from Worker, WorkLog
where Worker.id = WorkLog.workerId;
//results of this query should be transformed to Multimap<Worker, Period>
//if it was JDBC then it would be vitally
//to set resultSet.setFetchSize (someSmallNumber), ~100
Run Code Online (Sandbox Code Playgroud)
所以,我有两个问题:
我需要在Web应用程序中使用来自Java代码的Velocity(我将其用作邮件模板处理器).
所以,我有一个标准代码:
VelocityEngine ve = new VelocityEngine ();
try {
ve.init ();
Template t = ve.getTemplate (templatePath);
...
} catch (Exception e) {
throw new MailingException (e);
}
Run Code Online (Sandbox Code Playgroud)
这段代码总是抛出ResourceNotFoundException.我应该将模板放在web应用程序(WEB-INF?classpath?etc?)中,我应该如何指定路径(即我应该传递什么templatePath)?
我是缓存的新手,不知道数据如何存储在缓存中.我曾尝试在线阅读一些示例,但每个人都提供了存储和获取数据的代码片段,而不是解释如何使用memcache缓存数据.我已经读过它将数据存储在键值对中,但是我无法理解这些键值对存储在哪里?
还有人可以解释为什么进入缓存的数据被散列或加密?我在序列化数据和散列数据之间有点困惑.
我正在尝试使用Guice工作制作DI,对我来说(在我看来)确实是手册中的内容.
我无法解释这个问题,因为我并不真正理解它 - 一切看起来都非常符合逻辑并应该有效......但事实并非如此.所以,我只能附加代码和堆栈跟踪:
public class Runner {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TestModule());
//next line throws the exception
JMeterComponent jMeterComponent =
injector.getInstance(JMeterComponent.class);
....
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在尝试实例化JMeterComponent类的对象.它的构造函数(稍后会看到)接受3个参数:所有这些参数都应该由IoC实例化并注入.
而这里的TestModule用的这些3个参数配置:
public class TestModule extends AbstractModule {
@Override
protected void configure() {
bind(Callable.class).annotatedWith(Names.named("JMeter"))
.to(JMeterTask.class);
bind(Processor.class).annotatedWith(Names.named("JMeter"))
.to(JMeterResultsProcessor.class);
bind(Renderer.class).annotatedWith(Names.named("JMeter"))
.to(JMeterResultsWikiRenderer.class);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,让我们来看看这些具体的实现- JMeterTask,JMeterResultsProcessor和JMeterResultsWikiRenderer(所有的人都为简单起见假体):
public class JMeterTask implements Callable<JMeterRawResults> {
public JMeterRawResults call() throws Exception {
return new JMeterRawResults(); …Run Code Online (Sandbox Code Playgroud) Berkeley DB可能是最好的选择,但由于许可问题我无法使用它.
还有其他选择吗?
这是我在stackoverflow上的第一篇文章...我希望有人可以帮助我
我对Java 6有很大的性能回归LinkedBlockingQueue.在第一个线程中,我生成了一些我推入队列的对象.在第二个线程中,我将这些对象拉出来.当频繁调用take()方法时,会发生性能回归LinkedBlockingQueue.我监控了整个程序,并且该take()方法总体上占据了最多的时间.吞吐量从~58Mb/s到0.9Mb/s ......
队列弹出并使用此类中的静态方法调用方法
public class C_myMessageQueue {
private static final LinkedBlockingQueue<C_myMessageObject> x_queue = new LinkedBlockingQueue<C_myMessageObject>( 50000 );
/**
* @param message
* @throws InterruptedException
* @throws NullPointerException
*/
public static void addMyMessage( C_myMessageObject message )
throws InterruptedException, NullPointerException {
x_queue.put( message );
}
/**
* @return Die erste message der MesseageQueue
* @throws InterruptedException
*/
public static C_myMessageObject getMyMessage() throws InterruptedException {
return x_queue.take();
}
}
Run Code Online (Sandbox Code Playgroud)
如何调整take()方法以达到至少25Mb/s,或者是否有一个我可以使用的类,它将在"队列"满或空时阻塞.
亲切的问候 …
此问题涉及旧Java版本的行为和双重检查锁定算法的旧实现
较新的实现使用volatile并依赖于稍微改变的volatile语义,因此它们不会被破坏.
据说,除了long或double字段外,字段赋值总是原子的.
但是,当我读到为什么双重检查锁定被破坏的解释时,它说问题在于赋值操作:
// Broken multithreaded version
// "Double-Checked Locking" idiom
class Foo {
private Helper helper = null;
public Helper getHelper() {
if (helper == null) {
synchronized(this) {
if (helper == null) {
helper = new Helper();
}
}
}
return helper;
}
// other functions and members...
}
Run Code Online (Sandbox Code Playgroud)
- 线程A注意到该值未初始化,因此它获得锁定并开始初始化该值.
- 由于某些编程语言的语义,允许编译器生成的代码在A完成初始化之前更新共享变量以指向部分构造的对象.
- 线程B注意到共享变量已初始化(或显示),并返回其值.因为线程B认为该值已经初始化,所以它不会获得锁定.如果B在B看到A完成的所有初始化之前使用该对象(因为A尚未完成初始化或者因为对象中的某些初始化值尚未渗透到内存B使用(缓存一致性)) ,该程序可能会崩溃.
(来自http://en.wikipedia.org/wiki/Double-checked_locking).
什么时候可能?是否有可能在64位JVM分配操作中不是原子的?如果不是那么"双重检查锁定"是否真的被打破了?
我有一个简单的类角色:
@Entity
@Table (name = "ROLE")
public class Role implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column
private String roleName;
public Role () { }
public Role (String roleName) {
this.roleName = roleName;
}
public void setId (Integer id) {
this.id = id;
}
public Integer getId () {
return id;
}
public void setRoleName (String roleName) {
this.roleName = roleName;
}
public String getRoleName () {
return roleName;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想覆盖它的方法equals和hashCode.我的第一个建议是:
public boolean equals (Object obj) {
if …Run Code Online (Sandbox Code Playgroud) 假设我们有一组名字和价格的mp3播放器.
如何编写正确的solr查询以查找具有特定名称且价格低于100 $的所有商品?
q = "(name:(ipod) AND price ???? 100.0)"