小编Sam*_*son的帖子

对局部变量进行同步

今天我面对constructServiceUrl()的是org.jasig.cas.client.util.CommonUtils班上的方法.我觉得他很奇怪:

final StringBuffer buffer = new StringBuffer();

synchronized (buffer)
{
    if (!serverName.startsWith("https://") && !serverName.startsWith("http://"))
    {
        buffer.append(request.isSecure() ? "https://" : "http://");
    }

    buffer.append(serverName);
    buffer.append(request.getRequestURI());

    if (CommonUtils.isNotBlank(request.getQueryString()))
    {
        final int location = request.getQueryString().indexOf(
                artifactParameterName + "=");

        if (location == 0)
        {
            final String returnValue = encode ? response.encodeURL(buffer.toString()) : buffer.toString();

            if (LOG.isDebugEnabled())
            {
                LOG.debug("serviceUrl generated: " + returnValue);
            }

            return returnValue;
        }

        buffer.append("?");

        if (location == -1)
        {
            buffer.append(request.getQueryString());
        }
        else if (location > 0)
        {
            final int …
Run Code Online (Sandbox Code Playgroud)

java multithreading

39
推荐指数
3
解决办法
9260
查看次数

使用C#5.0异步读取文件

我刚刚开始使用C#的新async功能.我已经阅读了很多关于并行下载的方法,但是在阅读/处理文本文件方面没有任何内容.

我有一个旧脚本,我用来过滤日志文件,并认为我有一个升级它.但是我不确定我对新async/ await语法的使用是否正确.

在我的脑海中,我看到这一行逐行读取文件并将其传递给不同的线程进行处理,以便它可以继续而无需等待结果.

我是否正确地思考它,或者实现这个的最佳方法是什么?

static async Task<string[]> FilterLogFile(string fileLocation)
{
    string line;

    List<string> matches = new List<string>();

    using(TextReader file = File.OpenText(fileLocation))
    {        
        while((line = await file.ReadLineAsync()) != null)
        {
            CheckForMatch(line, matches);
        }
    }

    return matches.ToArray();
}
Run Code Online (Sandbox Code Playgroud)

完整的脚本:http://share.linqpad.net/29kgbe.linq

c# asynchronous async-await c#-5.0 windows-8

9
推荐指数
1
解决办法
5100
查看次数

完成事件的异步方法

我使用.net 4.0,我试图弄清楚如何使用异步方法来等待DocumentCompleted事件完成并返回值.我的原始代码在上面,如何在这种情况下将其转换为异步/等待模型?

private class BrowserWindow
    {
        private bool webBrowserReady = false;
        public string content = "";


        public void Navigate(string url)
        {

            xxx browser = new xxx();

            browser.DocumentCompleted += new EventHandler(wb_DocumentCompleted);
            webBrowserReady = false;
            browser.CreateControl();
            if (browser.IsHandleCreated)
                browser.Navigate(url);


            while (!webBrowserReady)
            {
                //Application.DoEvents(); >> replace it with async/await 
            }

        }

        private void wb_DocumentCompleted(object sender, EventArgs e)
        {
            try
            {
               ...

                  webBrowserReady = true;

                  content = browser.Document.Body.InnerHtml;

            }
            catch
            {

            }

        }

        public delegate string AsyncMethodCaller(string url);
    }
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous

7
推荐指数
1
解决办法
5066
查看次数

寻找一种反复检查变量的更好方法

我最近创建了一个只有一个变量的站点,并根据各种可能的值进行检查并给出适当的响应.该程序使用了很多else if语句.

我确信有更好的方法可以做到这一点,但不知道该使用什么.我还在学习PHP.

以下是为您提供更好主意的源代码:

http://github.com/r3morse/isitup/blob/a7a972bcf125d1f058a44406a467438d46aa4b16/functions.php

php

2
推荐指数
1
解决办法
155
查看次数

Javascript - 获取未定义元素的宽度

我有一个div元素,没有定义的宽度或高度.它拥有其他元素和尺寸的模具.我需要得到div的最终大小,而无法检查其中元素的大小(动态元素).

我尝试解析对象的所有属性,但什么都没找到,都是"未定义".

有什么想法吗?

非常感谢

稍后编辑:

我有

<div id="xxx" style="position:relative;">  
    aaaa
</div>
Run Code Online (Sandbox Code Playgroud)

我试过了:

var a = mydiv.style.width;
Run Code Online (Sandbox Code Playgroud)

但它没有用.

我也尝试过:

var getKeys = function (obj) {  
    var keys = [];

    for(var key in obj){  
        document.write(key+"="+obj.key+"<br>");  
    }  

    return keys;  
}  

getKeys(mydiv.style);  
Run Code Online (Sandbox Code Playgroud)

要显示所有属性,但没有任何信息.

jQuery解决方案工作得很好,但getComputedStyle我正在寻找,因为我不能在这里使用jquery.

对不起,提供简短的信息.

javascript css jquery dimensions

2
推荐指数
1
解决办法
5011
查看次数

仅使用一个文件的自定义错误页面

虽然我确信我可能无法看到我如何能够做到以下几点.

使用.htaccess将html错误指向单个错误处理页面(这不是问题).

该页面然后检查错误代码实际是什么(404,500等)并根据每个显示消息.

我试图在PHP环境中这样做,它是一个简约的网站,所以文件越少越好.

这是我正在制作的404的一个例子,http://isitup.org/404,我想要的是相应地切换数字.

html php error-handling .htaccess

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

Java函数正在更新其输入变量

我正在做一个实验室,从事健身功能.问题是我正在制作一个SmallChange()函数来执行它所说的内容,令人烦恼的是它似乎更新了你给它的变量,而它不应该.

这是完整课程的副本:https://gist.github.com/1710367

第38行是问题.

以下是功能.当我把它solution作为输入时solution,它会根据它做出的小改动进行更新,但我无法弄清楚如何或为什么.

谁知道我哪里出错了?它开始伤害我的大脑.

// Edits either the angle or velocity of our solution
// by a small amount (about 1% of the diffrence between max and min)
public static Double[] SmallChange(Double[] sol) {
    // Pick a 0 or 1
    switch (r.nextInt(2)) {
    case 1: // Changing the angle
        // The angle change amount
        Double angle = (angleLimits[1] - angleLimits[0]) * 0.01;

        // Make the change, either + or -
        if (r.nextInt(2) …
Run Code Online (Sandbox Code Playgroud)

java eclipse java-6

0
推荐指数
1
解决办法
879
查看次数