问题:我需要编写一个函数,该函数从地图返回输入键的值.如果函数找不到map中的值,它将从数据库中获取值,写入map以供将来使用并返回相同的值.可以有多个线程调用此函数.
string GetData (const int key)
{
pthread_rwlock_rdlock(&rwlock); //read lock
string result = "not found";
my_map::const_iterator iter = m.find(key);
if ( iter != m.end() )//found
{
result = iter->second;
}
else //missing
{
pthread_rwlock_wrlock(&rwlock); // write lock
//fetch value from data base
//if successful, add to the map
m[key] = "missing data";
result = "missing data";
pthread_rwlock_unlock(&rwlock); // unlock write lock
}
pthread_rwlock_unlock(&rwlock); // unlock read lock
return result;
}
Run Code Online (Sandbox Code Playgroud)
这个功能线程安全吗?两个或多个线程是否有可能在写锁定时排队并从数据库中查询相同的密钥?如果是,我该如何避免这种情况?
我正在使用YUI Compressor压缩我的Web应用程序中的JS文件.
这是POM插件conf:
<plugin>
<groupId>net.sf.alchim</groupId>
<artifactId>yuicompressor-maven-plugin</artifactId>
<version>0.7.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jslint</goal>
<goal>compress</goal>
</goals>
</execution>
</executions>
<configuration>
<failOnWarning>true</failOnWarning>
<nosuffix>true</nosuffix>
<force>true</force>
<aggregations>
<aggregation>
<!-- remove files after aggregation (default: false) -->
<removeIncluded>false</removeIncluded>
<!-- insert new line after each concatenation (default: false) -->
<insertNewLine>false</insertNewLine>
<output>${project.basedir}/${webcontent.dir}/js/compressedAll.js</output>
<!-- files to include, path relative to output's directory or absolute path-->
<!--inputDir>base directory for non absolute includes, default to parent dir of output</inputDir-->
<includes>
<include>**/autocomplete.js</include>
<include>**/calendar.js</include>
<include>**/dialogs.js</include>
<include>**/download.js</include>
<include>**/folding.js</include>
<include>**/jquery-1.4.2.min.js</include>
<include>**/jquery.bgiframe.min.js</include> …Run Code Online (Sandbox Code Playgroud) 这是我使用的xml代码
但即使尝试增加最小高度也没用
请帮忙
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Here you put the rest of your current view-->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="ID:"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/cid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="Name:"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/con"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="Posted By:"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/poby"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="Posted Time:"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/poti"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="Annotation ID:"
android:textSize="15dp"
android:textStyle="bold" …Run Code Online (Sandbox Code Playgroud) 我是一个崭露头角的Android开发人员,如果没有简单的方法来配置adb服务器在另一个端口上运行那么工具的不灵活性将迫使我退出Android应用程序开发.
网络搜索没有返回任何解决方案.
我还在android sdk目录中的所有文件中搜索了"5037",但没有在那里找到设置.
许多标准C和POSIX函数返回-1的错误,并0成功,例如truncate,fflush,msync等.
int ret = truncate("/some/file", 42);
Run Code Online (Sandbox Code Playgroud)
用ret != -1或检查成功是否更好ret == 0?为什么?
这是我的经验,大多数人检查错误情况(ret != -1),因为通常只有一个(考虑返回NULL或EOF出错的函数).然而事后看来,人们可以看到这些功能可以从errno直接返回中受益(在哪里0被认为没有错误).
还担心函数会返回除0or -1之外的其他内容,或者稍后会添加其他返回值.在这些场景中,测试表示成功(ret == 0)的"最紧密"值的值是有意义的.
人们都知道这EOF是我的一个假设,通常被定义为-1.
像下面这样的代码将启动一个新线程来完成这项工作.有什么方法可以控制该线程的优先级吗?
Task.Factory.StartNew(() => {
// everything here will be executed in a new thread.
// I want to set the priority of this thread to BelowNormal
});
Run Code Online (Sandbox Code Playgroud) 我有一个http://www.example.com/startpage的起始页面,其中有1220个列表以标准方式分页,例如每页20个结果.
我有代码工作,解析结果的第一页,并在其网址中包含"example_guide/paris_shops"的链接.然后我使用Nokogiri来提取该最终页面的特定数据.一切正常,20个结果写入文件.
但是,我似乎无法弄清楚如何让Anemone爬到结果的下一页(http://www.example.com/startpage?page=2),然后继续解析该页面,然后再解析第3页页面(http://www.example.com/startpage?page=3)等.
所以我想问一下是否有人知道如何在页面上启动海葵,解析该页面上的所有链接(以及特定数据的下一级数据),然后按照分页到下一页的结果所以海葵可以再次开始解析,等等.鉴于分页链接与结果中的链接不同,Anemone当然不会遵循它们.
目前我正在加载第一页结果的网址,让它完成,然后粘贴到第二页结果等的下一个网址等.非常手动和低效,特别是对于获取数百页.
任何帮助将非常感激.
require 'rubygems'
require 'anemone'
require 'nokogiri'
require 'open-uri'
Anemone.crawl("http://www.example.com/startpage", :delay => 3) do |anemone|
anemone.on_pages_like(/example_guide\/paris_shops\/[^?]*$/) do | page |
doc = Nokogiri::HTML(open(page.url))
name = doc.at_css("#top h2").text unless doc.at_css("#top h2").nil?
address = doc.at_css(".info tr:nth-child(3) td").text unless doc.at_css(".info tr:nth-child(3) td").nil?
website = doc.at_css("tr:nth-child(5) a").text unless doc.at_css("tr:nth-child(5) a").nil?
open('savedwebdata.txt', 'a') { |f|
f.puts "#{name}\t#{address}\t#{website}\t#{Time.now}"
}
end
end
Run Code Online (Sandbox Code Playgroud) 我有一个MyObjects列表... MyObject {int id,String name}.现在我想将列表拆分为具有相同"id"值的子列表,任何人都可以建议这样做的有效方法.
假设我们想从数据库中选择数据,然后我们为此编写查询.
例:
SqlConnection con=new SqlConnection(Connetion name)
string selectPkId = @"SELECT PK_ID FROM TABLE"
SqlCommand cmd=new SqlCommand(selectPkId ,con);
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是为什么我们在sql查询之前基本上使用@.如果我之前没有使用@那么它再次正常工作(不会给出任何错误),那么使用"@"需要什么?请告诉我.
我想知道当用户切换回"你的"页面窗口时是否有可能获得一个事件?当用户打开新选项卡然后切换回页面选项卡或用户关闭safari然后再次打开时,可能会发生这种情况.
我希望能够在收到此活动后更新页面上的内容.我现在使用setInterval来实现这一点,但是当用户关注窗口时没有延迟会很好.
谢谢!
android ×2
c++ ×2
.net ×1
.net-4.0 ×1
adb ×1
asp.net ×1
c ×1
c# ×1
collections ×1
conventions ×1
correctness ×1
default ×1
java ×1
javascript ×1
listview ×1
maven-2 ×1
nokogiri ×1
port ×1
pthreads ×1
return-value ×1
ruby ×1
stl ×1
taskfactory ×1
verbatim ×1