我想每天下午2点执行一份工作.java.util.Timer我可以用哪种方法安排工作?
2小时后,它将停止工作并重新安排在第二天下午2点.
假设我有一个数据数组,2个线程可以安全地同时写入同一个数组的不同索引吗?我关心写入速度,我想同步'get index to write at'位与实际写入.
我正在编写代码,让我假设2个线程不会获得相同的索引.
我们来看看Java中的以下代码片段.
package trickyjava;
class A
{
public A(String s)
{
System.out.println(s);
}
}
final class B extends A
{
public B()
{
super(method()); // Calling the following method first.
}
private static String method()
{
return "method invoked";
}
}
final public class Main
{
public static void main(String[] args)
{
B b = new B();
}
}
Run Code Online (Sandbox Code Playgroud)
按照惯例,Java中的super()构造函数必须是相关构造函数体中的第一个语句.在上面的代码中,我们在super()构造函数参数列表中调用静态方法本身是super(method()); .
这意味着在构造函数B()中对super的调用中,在调用super之前调用了一个方法!这应该被编译器禁止,但它很好用.这有点等同于以下陈述.
String s = method();
super(s);
Run Code Online (Sandbox Code Playgroud)
但是,它是非法的,导致编译时错误,指示"调用super必须是构造函数中的第一个语句".为什么?为什么它等价超级(method()); 是有效的,编译器不再抱怨?
我有一个可扩展的树(在 HTML 页面中):
+ Category 1
- Category 2
+ Subcategory 1
- Subcategory 2
|- Foo
|- Bar
|- Link 42
Run Code Online (Sandbox Code Playgroud)
它由一个结构体表示(在后端定义):
class Demo {
static ImmutableList<Item> sample() {
return ImmutableList.of(
new Item("Category 1", ImmutableList.of(
new Item("Some link title", "resource_id_1"),
new Item("Another link title", "resource_id_2"))),
new Item("Category 2", ImmutableList.of(
new Item("Subategory 1", ImmutableList.of(
new Item("Another link title", "resource_id_3"))),
new Item("Subcategory 2", ImmutableList.of(
new Item("Foo", "resource_id_1"),
new Item("Bar", "resource_id_2"),
new Item("Link 42", "resource_id_42"))))));
}
}
Run Code Online (Sandbox Code Playgroud)
和Item如下:
public class …Run Code Online (Sandbox Code Playgroud) 假设您有以下代码:
public int getSpeedX() {
speedLock.lock();
try {
return speedX;
} finally {
speedLock.unlock();
}
}
public void setSpeedX(int x) {
speedLock.lock();
try {
speedX = x;
} finally {
speedLock.unlock();
}
}
Run Code Online (Sandbox Code Playgroud)
返回速度X好吗?或应该是:
public int getSpeedX() {
int temp;
speedLock.lock();
try {
temp = speedX;
} finally {
speedLock.unlock();
}
return temp;
}
Run Code Online (Sandbox Code Playgroud)
哪个是对的?或者他们是等同的?
我的map函数必须为每个输入读取一个文件.该文件根本没有变化,仅供阅读.分布式缓存可能对我有很多帮助,但我无法找到使用它的方法.我需要覆盖的public void configure(JobConf conf)函数,我认为已弃用.好的JobConf肯定已被弃用.所有DistributedCache教程都使用不推荐的方式.我能做什么?有没有我可以覆盖的另一个配置功能?
这些是我的地图功能的第一行:
Configuration conf = new Configuration(); //load the MFile
FileSystem fs = FileSystem.get(conf);
Path inFile = new Path("planet/MFile");
FSDataInputStream in = fs.open(inFile);
DecisionTree dtree=new DecisionTree().loadTree(in);
Run Code Online (Sandbox Code Playgroud)
我想缓存那个MFile,这样我的map函数就不需要一遍又一遍地查看它
我想我是在询问协变返回类型.我有一些生成的代码,我正在尝试扩展和使用.我们假设我有以下两个类:
public class SuperParent
{
public List<SuperChild> getList()
{
return new ArrayList<SuperChild>();
}
}
public class SuperChild
{
}
Run Code Online (Sandbox Code Playgroud)
现在,我想从这些中派生出新的类:
public class SubParent extends SuperParent
{
public List<SubChild> getList()
{
return new ArrayList<SubChild>();
}
}
public class SubChild extends SuperChild
{
}
Run Code Online (Sandbox Code Playgroud)
问题是,显然我不能覆盖getList()方法,因为返回类型不匹配,尽管两个类都在同一方向上扩展.谁能解释一下?
例如,我有实体类User:
public class User
{
private long id;
private String name;
// setters and getters
}
Run Code Online (Sandbox Code Playgroud)
接下来,我添加新的实体类: Comment
public class Comment
{
private long id;
private String comment;
// setters and getters
}
Run Code Online (Sandbox Code Playgroud)
接下来,我可以添加越来越多的实体类.
而且,此时我想:我可以/必须在逻辑结构中绑定/连接我的实体类还是没有?
我的意思是说?我试着解释一下:
要点1:所有这些类:User,Comment和更多的其他- POJO.
想法1:需要通过接口或抽象类对这些类进行逻辑绑定.
第2点:我看到,所有实体类都有相同的方法:getId和setId().
想法2:需要避免在所有类中声明此方法.
我的解决方案
添加界面BaseEntity:
public interface BaseEntity
{
public long getId();
public void setId(long id);
}
Run Code Online (Sandbox Code Playgroud)
添加所有实体类必须实现此接口.
结果我们逻辑连接所有实体类.我们保证每个实体类的实现getId()和setId()方法.
但这种解决方案不能解决多申报的问题getId和setId.
解决方案是创建一般BaseEntity类: …
我有hadoop运行,基本上只是聚合键,它的代码:(映射器是身份映射器)
public void reduce(Text key, Iterator<Text> values,
OutputCollector<Text, Text> results, Reporter reporter) throws IOException {
String res = new String("");
while(values.hasNext())
{
res += values.next().toString();
}
Text outputValue = new Text("<all><id>"+key.toString()+"</id>"+res+"</all>");
results.collect(key, outputValue);
}
Run Code Online (Sandbox Code Playgroud)
它停留在这个水平:
12/11/26 06:19:23 INFO mapred.JobClient: Running job: job_201210240845_0099
12/11/26 06:19:24 INFO mapred.JobClient: map 0% reduce 0%
12/11/26 06:19:37 INFO mapred.JobClient: map 20% reduce 0%
12/11/26 06:19:40 INFO mapred.JobClient: map 80% reduce 0%
12/11/26 06:19:41 INFO mapred.JobClient: map 100% reduce 0%
12/11/26 06:19:46 INFO mapred.JobClient: map 100% …Run Code Online (Sandbox Code Playgroud) 我想使用一个java集合(列表,映射等)来缓存一些数据,这样我就可以使用这个缓存而不是直接检查数据库.我唯一担心的是集合大小,我希望这个缓存保存,假设只有1000个条目,一旦达到此计数,我想删除最旧的条目并添加一个新条目.这可能吗?
java ×9
concurrency ×2
hadoop ×2
mapreduce ×2
abstract ×1
collections ×1
covariance ×1
file-io ×1
generics ×1
heap ×1
interface ×1
locking ×1
map ×1
overriding ×1
static ×1
timer ×1
tree ×1