以下代码解释了我的问题.我知道列表不是线程安全的.但是这个潜在的"真正"原因是什么?
class Program
{
static void Main(string[] args)
{
List<string> strCol = new List<string>();
for (int i = 0; i < 10; i++)
{
int id = i;
Task.Factory.StartNew(() =>
{
AddElements(strCol);
}).ContinueWith((t) => { WriteCount(strCol, id.ToString()); });
}
Console.ReadLine();
}
private static void WriteCount(List<string> strCol, string id)
{
Console.WriteLine(string.Format("Task {0} is done. Count: {1}. Thread ID: {2}", id, strCol.Count, Thread.CurrentThread.ManagedThreadId));
}
private static void AddElements(List<string> strCol)
{
for (int i = 0; i < 20000; i++)
{
strCol.Add(i.ToString());
} …Run Code Online (Sandbox Code Playgroud) c# parallel-processing concurrency multithreading task-parallel-library
他们的文件简单而专业.但他们并没有过多提及这些开源项目之间的关系.我什么时候应该使用哪一个?哪一个适合什么情况?
如果您是熟悉这些项目的GIS开发人员,您能解释一下吗?
Success的flatMap方法实现如下:
def flatMap[U](f: T => Try[U]): Try[U] =
try f(value)
catch {
case NonFatal(e) => Failure(e)
}
Run Code Online (Sandbox Code Playgroud)
我有点理解这个方法在做什么,它有助于我们避免编写大量的代码.
但它在什么意义上类似于常规flatMap?
常规flatMap采用一系列序列,并将所有元素放入一个大的"平面"序列中.
但是Try的flatMap方法并没有真正扁平化.
那么,如何理解Try的flatMap方法呢?
error-handling monads functional-programming scala try-catch
我发现C#4.0中的"可选参数"功能非常有趣,所以我试图弄清楚它们是如何实现的.所以我写了一个像这样的方法:
private static void A(int a = 5) { }
Run Code Online (Sandbox Code Playgroud)
编译它,然后在IL DASM中反编译它,这是IL代码:
.method private hidebysig static void A([opt] int32 a) cil managed
{
.param [1] = int32(0x00000005)
// Code size 2 (0x2)
.maxstack 8
IL_0000: nop
IL_0001: ret
} // end of method Program::A
Run Code Online (Sandbox Code Playgroud)
它的元数据中有这个:
(1)ParamToken:(08000002)名称:标志:[可选] [HasDefault](00001010)默认值:(I4)5
所以我按照线索写了一个像这样的方法:
private static void B([Optional, DefaultParameterValue(78)]int b) { }
Run Code Online (Sandbox Code Playgroud)
编译并反编译它,我发现C#编译器为方法A和B生成了几乎相同的MSIL代码(名称除外).
正如我们所看到的,IL代码中没有属性的迹象,而且感觉不对,所以我写了一个这样的自定义属性:
[AttributeUsage(AttributeTargets.Parameter)]
public class MyTestAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
然后在方法C中使用它,如下所示:
private static void C([MyTest]int c) { }
Run Code Online (Sandbox Code Playgroud)
编译它然后反编译它,哈哈,我发现了这个:
.method private hidebysig static void …Run Code Online (Sandbox Code Playgroud) 例如,如果我有这样的页面:
public partial class Page1 : PhoneApplicationPage
{
DispatcherTimer timer = new DispatcherTimer();
public Page1()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(5);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("timer tick");
}
}
Run Code Online (Sandbox Code Playgroud)
在应用程序中,我导航到此页面,它将每5秒弹出一个消息框.然后我按下手机上的"后退"按钮并导航回上一页.但奇怪的是它仍然每5秒弹出一个消息框.我知道我可以在OnNavigatedFrom方法中停止计时器,但为什么会这样呢?按下后退按钮后页面是否处理完毕?
谢谢
object ScalaTrueRing {
def rule = println("To rule them all")
}
Run Code Online (Sandbox Code Playgroud)
这段代码将被编译成java字节码,如果我反编译它,那么等效的Java代码就像这样:
public final class JavaTrueRing
{
public static final void rule()
{
ScalaTrueRing..MODULE$.rule();
}
}
/* */ public final class JavaTrueRing$
/* */ implements ScalaObject
/* */ {
/* */ public static final MODULE$;
/* */
/* */ static
/* */ {
/* */ new ();
/* */ }
/* */
/* */ public void rule()
/* */ {
/* 11 */ Predef..MODULE$.println("To rule them all");
/* */ } …Run Code Online (Sandbox Code Playgroud) 
如屏幕截图所示,ClassLibrary1依赖于ClassLibrary2,ClassLibrary2依赖于ClassLibrary3.并且"复制本地"设置为"真".但是当我编译ClassLibrary1时,ClassLibrary3将不会被复制到bin文件夹中.
那么如何配置Visual Studio项目文件以使其复制间接引用?
使用Scala标准库时,我可以做这样的事情:
scala> val scalaList = List(1,2,3)
scalaList: List[Int] = List(1, 2, 3)
scala> scalaList.foldLeft(0)((acc,n)=>acc+n)
res0: Int = 6
Run Code Online (Sandbox Code Playgroud)
从许多Int中取出一个Int.
我可以这样做:
scala> scalaList.foldLeft("")((acc,n)=>acc+n.toString)
res1: String = 123
Run Code Online (Sandbox Code Playgroud)
从许多Int中制作一个String.
因此,foldLeft可以是同构的,也可以是异构的,无论我们想要什么,它都在一个API中.
在Spark中,如果我想要很多Int中的一个Int,我可以这样做:
scala> val rdd = sc.parallelize(List(1,2,3))
rdd: org.apache.spark.rdd.RDD[Int] = ParallelCollectionRDD[1] at parallelize at <console>:12
scala> rdd.fold(0)((acc,n)=>acc+n)
res1: Int = 6
Run Code Online (Sandbox Code Playgroud)
fold API类似于foldLeft,但它只是同类的,RDD [Int]只能生成带折叠的Int.
Spark中也有一个聚合API:
scala> rdd.aggregate("")((acc,n)=>acc+n.toString, (s1,s2)=>s1+s2)
res11: String = 132
Run Code Online (Sandbox Code Playgroud)
它是异构的,RDD [Int]现在可以生成一个String.
那么,为什么fold和聚合在Spark中实现为两个不同的API?
为什么它们的设计不像foldLeft那样可以是同构的还是异构的?
(我对Spark很新,请原谅我这是一个愚蠢的问题.)
scala aggregate heterogeneous homogenous-transformation apache-spark
这就是我在WPF中重现这个问题的方法:
创建自定义控件:
public class TestCustomControl : Control
{
static TestCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl)));
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(TestCustomControl), new PropertyMetadata("Hello"));
public double OffSet
{
get { return (double)GetValue(OffSetProperty); }
set { SetValue(OffSetProperty, value); }
}
// Using a DependencyProperty as the backing store …Run Code Online (Sandbox Code Playgroud) 我试图反编译包含JUnit测试的.class文件.我读了字节码,但是我没有看到@Test注释的任何线索(它在java源代码中使用).
作为元数据,如何在字节码中表示注释?
.net ×3
scala ×3
c# ×2
java ×2
silverlight ×2
aggregate ×1
annotations ×1
apache-spark ×1
bytecode ×1
c#-4.0 ×1
cil ×1
concurrency ×1
copy-local ×1
declarative ×1
decompiling ×1
dispose ×1
gdal ×1
gis ×1
il ×1
monads ×1
navigation ×1
ogr ×1
osgeo ×1
reference ×1
try-catch ×1
wpf ×1