我正在编写Eclipse插件,并且经常出现这样的情况,即正在运行的Job需要暂停一段时间,在UI线程上异步运行并恢复.
所以我的代码通常看起来像:
Display display = Display.getDefault();
display.syncExec(new Runnable() {
public void run() {
// Do some calculation
// How do I return a value from here?
}
});
// I want to be able to use the calculation result here!
Run Code Online (Sandbox Code Playgroud)
一种方法是让整个Job类都有一些字段.另一种方法是使用自定义类(而不是匿名类,并使用其生成的数据字段等.什么是最好和最优雅的方法?
我正在使用一些第三方库通过异步协议连接到服务器并获得响应.例如,通过用户名获取userid的方法如下所示:
public int getUserid(String username) {
int userid = 0;
connection.call("getUserid", new Responder() {
public void onResult(final int result) {
System.out.println("userid: " + result);
//how to assign received value to userid and return it?
}
}, username);
//wait for response
while (userid == 0) {
try{
Thread.sleep(100);
} catch (Exception e) {}
}
return userid;
}
Run Code Online (Sandbox Code Playgroud)
问题是我无法从服务器响应中将返回的"结果"分配给方法中的"userid"变量(以便在之后返回).怎么解决这个?我可能可以将它分配给某个类变量而不是方法变量,但我想将它保留在方法范围内,这样我就不必处理并发问题了.
谢谢.
任何人都可以解释以下代码的工作吗?
interface myInterface{}
public class Main {
public static void main(String[] args) {
System.out.println(new myInterface(){public String toString(){return "myInterfacetoString";}});
System.out.println(new myInterface(){public String myFunction(){return "myInterfacemyFunction";}});
}
}
Run Code Online (Sandbox Code Playgroud)
输出是......
myInterfacetoString
primitivedemo.Main$2@9304b1
Run Code Online (Sandbox Code Playgroud)
所有答案都说println()语句中的myInterface是匿名类.但是因为我已经将它声明为接口,为什么它允许我创建同名的匿名类....?
再次...如果这些是匿名类,那么class main应该允许我给这些匿名类赋予任何名称..但是如果尝试这样做..我得到编译错误
我在实践中阅读Java并发性,下面的例子来自于此.我的问题是这个参考逃脱是什么意思?会有什么问题?.这个引用是如何从doSomething(e)中逃脱的.
public class ThisEscape {
public ThisEscape(EventSource source) {
source.registerListener(
new EventListener() {
public void onEvent(Event e) {
doSomething(e);
}
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是如何解决这个问题的
public class SafeListener {
private final EventListener listener;
private SafeListener() {
listener = new EventListener() {
public void onEvent(Event e) {
doSomething(e);
}
};
}
public static SafeListener newInstance(EventSource source) {
SafeListener safe = new SafeListener();
source.registerListener(safe.listener);
return safe;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我试过以下的例子
public class Escape {
public Escape( Printer printer ){
printer.print(new Escaper(){
@Override …Run Code Online (Sandbox Code Playgroud) 匿名类和静态内部类的最佳实践(设计和性能)是什么?
我个人认为静态内部类提供了更好的封装,并且应该提供更好的性能,因为他们无法访问类外的最终变量.但我从来没有真正质疑这一点.
我发现了一篇关于此的帖子,但我觉得它实际上没有回答关于这个问题,只是人们对此的个人想法.
这是代码:
timer.schedule(new TimerTask()
{
public void run()
{
synchronized(this)
{
try
{
// System.out.println(" ITERATION = ");
pachubeCLI.update(78164);
}
catch (PachubeException e)
{
// If an exception occurs it will print the error message from the
// failed HTTP command
System.err.println(e.errorMessage);
}
catch (IOException e)
{
System.err.println(e);
}
}
}
}, 0, 5*1000);
Run Code Online (Sandbox Code Playgroud)
我可以说,代码基本上用于使用Timer类的对象来安排操作.schedule根据eclipse 传递给方法的参数是(TimerTask task,long delay, long period).但是看一下这段代码,整个代码块作为第一个参数而不是对TimerTask类的引用传递.我以前从未见过这样的方法.到底发生了什么?
一些背景:该对象的schedule方法Timer用于定期更新Xively(以前的COSM(以前的pachube))上的Feed.
此外,我不知道哪个标签描述了这里发生的事情.如果你这样做,请添加或发表评论.
关于匿名课和最终字段的解释我仍然不满意.有很多问题试图解释明显的问题,但我没有找到所有问题的答案:-)
假设以下代码:
public void method(final int i, int j) {
final int z = 6;
final int x = j;
int k = 5;
new Runnable() {
public void run() {
System.out.print(i);
System.out.print(x);
System.out.print(z);
System.out.print(k);
}
};
}
Run Code Online (Sandbox Code Playgroud)
k属性,无法编译此代码.z在编译期间用声明的值替换属性.当我搜索解决方案时,究竟是如何工作的i,x我发现这个答案说:
然后,编译器可以将匿名类中的lastPrice和price的使用替换为常量的值(在编译时,当然),并且您将不再有访问不存在的变量的问题
它如何适用于字段i,x如果它们是方法的参数?在编译期间不知道它们?这种方法可以发挥作用z.
另一方面,有关于堆栈问题的解释:
这允许Java编译器在运行时"捕获"变量的值,并将副本存储为内部类中的字段.一旦外部方法终止并且其堆栈框架已被移除,原始变量就会消失,但内部类的私有副本仍然存在于类的自身内存中
我会理解匿名类在创建过程中以某种方式复制了所有必需的内容(字段).缺失final有明显的问题,如果匿名类声明下面的某些代码会改变值,则执行使用可能的stale值.
但是,这可以解决当匿名类'方法在使用的属性范围之外执行时的问题.
但这种方法即使没有final声明也应该有效,因为它只是复制所有字段.
这两种方法对我来说都是独立的.说到哪 - 它可以解决我的问题 - …
在java中我们可以interface用这样的Anonymous类实现:
import java.util.function.Predicate;
public class Test {
public static void main(String[] args) {
System.out.println(testIf("", new Predicate<String>() {
@Override
public boolean test(String s) {
return s.isEmpty();
}
}));
}
public static <T> boolean testIf(T t, Predicate<T> predicate) {
return predicate.test(t);
}
}
Run Code Online (Sandbox Code Playgroud)
从Java 8开始:
System.out.println(testIf("", String::isEmpty));
Run Code Online (Sandbox Code Playgroud)
我们怎么能用c ++做呢?我编写了以下代码但是我收到了编译错误:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template <class T>
class Predicate
{
public:
virtual bool test(T) = 0;
};
template <class T>
bool testIf(T t, Predicate<T> predicate)
{ …Run Code Online (Sandbox Code Playgroud)
我想创建一个类,从匿名类定义中获取一个对象来存储.我使用泛型类来实现它.然后我想使用功能接口定义一些操作,这些接口将此对象作为要使用的参数.
代码说的不仅仅是文字.所以看看这个:
public class Test<T> {
@FunctionalInterface
public interface operation<T> {
void execute(T object);
}
private T obj;
public Test(T _obj){
obj = _obj;
}
public void runOperation(operation<T> op){
op.execute(obj);
}
public static void main(String[] args){
Test<?> t = new Test<>(new Object(){
public String text = "Something";
});
t.runOperation((o) -> {
System.out.println(o.text); // text cannot be resolved
});
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是o.text在功能接口的实现中无法解决.这是某种类型的擦除后果吗?
有趣的是,当我在构造函数中实现功能接口时,我可以使用此代码.
看看这段代码:
public class Test<T> {
@FunctionalInterface
public interface operation<T> {
void execute(T object);
}
private T obj;
private …Run Code Online (Sandbox Code Playgroud)