在这个例子中,Foo.Something和Bar.Something之间有什么有效的区别吗?
class Foo
{
public string Something;
}
class Bar
{
public string Something{get; set;}
}
class Program
{
static void Main(string[] args)
{
var MyFoo = new Foo();
MyFoo.Something = "Hello: foo";
System.Console.WriteLine(MyFoo.Something);
var MyBar = new Bar();
MyBar.Something = "Hello: bar";
System.Console.WriteLine(MyBar.Something);
System.Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
AFAIK他们的行为完全相同.如果他们为什么不在Foo中使用普通的字段?在java中,我们使用setter来强制执行新的不变量而不会破坏代码和getter来返回安全数据但是在c#中你总是可以将Foo重写为:
class Foo
{
private string _Something;
public string Something
{
get {
//logic
return _Something;
}
set {
//check new invariant
_Something = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
旧代码不会被破坏.
插入(通过粘贴)多行文本后,如何移动光标?在做内联插入(粘贴),p并且P工作得很好.但是,如果我粘贴多行文字,我的光标不管p或P移动到新的文本的顶部.
如何将光标留在新文本之后p或P?
这个超级简单的应用程序打印"Hello",但没有完成.我完全没有理由这样做.
JavaDoc,部分定稿,说
程序中不再引用且没有剩余线程的池将自动关闭.
tpe显然没有引用,这意味着线程没有完成.但我不明白为什么.有人能解释一下吗
在这种情况下的解决方案是在main结束时调用shutdown(),但我的实际应用程序更复杂.Runnables内部生成了新工作,所以我不知道什么时候会处理所有内容.
那么,我是否需要确定何时调用shutdown?或者是否有可能以某种方式指定,当tpe队列为空时,它应该自行关闭?
public class JavaApplication5 {
public static void main(String[] args) {
ThreadPoolExecutor tpe = new ThreadPoolExecutor(5, 15, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
tpe.execute(new Runnable() {
@Override
public void run() {
System.out.println("Hello");
}
});
}
Run Code Online (Sandbox Code Playgroud)
}
java concurrency multithreading java.util.concurrent threadpoolexecutor