小编Jos*_*lds的帖子

Java lambda表达式,转换和比较器

我正在查看Map界面的Java源代码,并遇到了这一小段代码:

    /**
     * Returns a comparator that compares {@link Map.Entry} in natural order on value.
     *
     * <p>The returned comparator is serializable and throws {@link
     * NullPointerException} when comparing an entry with null values.
     *
     * @param <K> the type of the map keys
     * @param <V> the {@link Comparable} type of the map values
     * @return a comparator that compares {@link Map.Entry} in natural order on value.
     * @see Comparable
     * @since 1.8
     */
    public static <K, …
Run Code Online (Sandbox Code Playgroud)

java lambda casting java-8

21
推荐指数
3
解决办法
8949
查看次数

如何确保通过另一个可执行文件打开可执行文件?

还有,你可以用它来从未有OS按住锁一个整洁的小把戏.dll的/ .exe是个.exe需要运行.

假设我们的目录如下所示:

>opener.exe
>actualProgram.exe
>dependency.dll

作为一个基本的例子,我们可以这样做:

namespace Opener
{
    class Program
    {
        static void Main(string[] args)
        {

            AppDomain domain = AppDomain.CurrentDomain;
            AppDomain newDomain = AppDomain.CreateDomain(
                domain.FriendlyName,
                domain.Evidence,
                domain.BaseDirectory,
                domain.RelativeSearchPath,
                true,  // "shadow copy" files: copy rather than lock
                domain.SetupInformation.AppDomainInitializer,
                domain.SetupInformation.AppDomainInitializerArguments
            );
            string assembly = 
                System.Reflection.Assembly.GetExecutingAssembly().Location 
                + "\\..\\actualProgram.exe";
            newDomain.ExecuteAssembly(assembly, args);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我们还可以在那里放置更多逻辑,actualProgram.exe尽管正在打开一个进程实例,我们可以使用它来实际替换/更新.试一试:当你通过"开启者"加载它时,你将能够删除/修改/替换依赖项和"实际"程序.显然这有其好处,即更新更容易.

但是在你的"actual.exe"中,你怎么能确定它是通过另一个可执行文件加载的,并且知道它是在没有锁定的情况下加载的.exe?如果有人要加载"actual.exe",即不通过"开启者",我想确保该过程立即退出.

提示?

c# windows

8
推荐指数
1
解决办法
134
查看次数

使用自定义WCF正文反序列化而不更改URI模板反序列化

这篇博文中,我能够创建一个IDispatchMessageFormatter使用JSON.NET序列化的自定义WCF .它有一点需要注意:使用它UriTemplate并不一定按预期工作.

这是博客文章提供的实现:

class NewtonsoftJsonDispatchFormatter : IDispatchMessageFormatter
{
    private readonly OperationDescription od;
    private readonly ServiceEndpoint ep;
    private readonly Dictionary<string, int> parameterNames = new Dictionary<string, int>();

    public NewtonsoftJsonDispatchFormatter(OperationDescription od, ServiceEndpoint ep, bool isRequest)
    {
        this.od = od;
        this.ep = ep;
        if (isRequest)
        {
            int operationParameterCount = od.Messages[0].Body.Parts.Count;
            if (operationParameterCount > 1)
            {
                this.parameterNames = new Dictionary<string, int>();
                for (int i = 0; i < operationParameterCount; i++)
                {
                    this.parameterNames.Add(od.Messages[0].Body.Parts[i].Name, i);
                }
            }
        }
    }
    public void …
Run Code Online (Sandbox Code Playgroud)

c# wcf json json.net

8
推荐指数
1
解决办法
2235
查看次数

如何在.NET框架中解析lambdas?

例如,您可以在Visual Studio 2010中使用lambda表达式,但仍然以.NET 2.0为目标.

编译器如何解析lambda表达式以使用不包含此功能的旧框架?

.net c# vb.net lambda

7
推荐指数
2
解决办法
209
查看次数

同步:为什么首选锁定私有的最终静态对象而不是类的类对象?

简单的问题:

为什么这是首选:

public class Foo {

    final private static Object foo = new Object();

    public static void doSomething() {
        synchronized(Foo.foo) {
            //code
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对此:

public class Foo {

    public static void doSomething() {
        synchronized(Foo.class) {
            //code
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或这个:

public class Foo {

    public synchronized static void doSomething() {
        //code
    }
}
Run Code Online (Sandbox Code Playgroud)

对我来说,这些看起来基本相同,所以我不确定什么是同步访问静态字段的最佳方法,或者为什么一个会比另一个好,但我听说第一个通常是首选.

java multithreading synchronization

6
推荐指数
1
解决办法
130
查看次数

使用内部检查异常抛出运行时异常是不好的做法吗?

这是不好的做法吗?

public String foo(File file) {
    StringBuilder sb = new StringBuilder();
    try(
            BufferedInputStream bis =
                    new BufferedInputStream(
                            new FileInputStream(file))
    ) {
        for(int c = bis.read(); c != -1; c = bis.read())
            sb.append((char) c);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)

假设没有办法从已检查的异常中优雅地恢复.

java exception

6
推荐指数
1
解决办法
2143
查看次数

从匿名类访问父类时解决歧义

我最近碰到了这样的事情......

public final class Foo<T>
implements Iterable<T> {

    //...

    public void remove(T t) { /* banana banana banana */ }

    //...

    public Iterator<T> Iterator {
        return new Iterator<T>() {

            //...

            @Override
            public void remove(T t) {
                // here, 'this' references our anonymous class...
                // 'remove' references this method...
                // so how can we access Foo's remove method?           
            }

            //...

        };
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么方法可以做我正在尝试的同时保持这个匿名课程?或者我们必须使用内部类或其他东西?

java anonymous-class

5
推荐指数
1
解决办法
374
查看次数

在VS 2005中解决没有lambda的问题

我从以下一些精彩的遗留代码中摘录了以下内容:

Private Sub SomeMethod()
    Dim deductibles As List(Of Integer) = GetDeductibles()    
    deductibles.RemoveAll(AddressOf LessThanMinDed)
EndSub
Private Function LessThanMinDed(ByVal i As Integer) As Boolean
    Return i < MinimumDeductible()
End Function
Run Code Online (Sandbox Code Playgroud)

如果你是一个语言势利小人,我们可以这样写:

private void SomeMethod() {
    List<int> deductibles = GetDeductibles();    
    deductibles.RemoveAll(LessThanMinDed);
}
private bool LessThanMinDed(int i) {
    return i < MinimumDeductible();
}
Run Code Online (Sandbox Code Playgroud)

MinimumDeductible()进行数据库调用.有没有办法写这个没有写像x = MinimumDeductible() : RemoveAll(Function(i) i < x)(因为lambda不在这个版本的VB.NET),只会调用一次数据库?

解决了(有点):

像这样解决:

Public Class Foo
    Private CachedMinimum As Integer
    Private Sub SomeMethod()
        Dim deductibles As List(Of Integer) = …
Run Code Online (Sandbox Code Playgroud)

.net vb.net lambda

4
推荐指数
1
解决办法
244
查看次数

当我在C++中打印未初始化的变量时会发生什么?

为什么打印32767(或其他随机数)?什么是std::cout印刷品?为什么不NULL(或0)?

int main() 
{
    int a;
    std::cout << a;
}
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
2
解决办法
878
查看次数

如何在C#中使进程(而不是线程)同步文件系统访问

今天早些时候我正在调试有点像这样的东西:

class Foo {

    void AccessCriticalSection() {
        try {
            if (IO.File.Exists("\\path\to\lock.txt")
                throw new Exception();
            System.IO.File.Create("\\path\to\lock.txt");
            criticalSection();          
        } catch (Exception ex) {
            // ignored
        } 
    }

    void CriticalSection() {
        // banana banana banana
        System.IO.File.Delete("\\path\to\lock.txt");
    }

}
Run Code Online (Sandbox Code Playgroud)

让我们甚至不知道这是多么可怕......但它本质上是试图使用一个名为lock.txtmutex 的文件.该操作不是原子操作,如果另一个进程正在使用它,其他进程只是没有通过关键部分(如果您可以相信它们,它们的目的是能够在锁定释放后继续)等等.显然它需要修复.

如何正确获取锁以跨多个进程同步对文件系统的访问?这些进程是同一进程的多个实例,因此它们可以共享一些协议而不是专门锁定目录(即,它们可以轻松地使用等同于某些类锁定的所有实例的东西,例如private final static Object lock = new Object();同步访问静态方法)

c# windows multithreading

4
推荐指数
1
解决办法
2265
查看次数