我正在尝试多线程示例.我正在尝试使用以下代码生成竞争条件.但我总是得到相同(正确)的输出.
class Counter
attr_reader :count
def initialize
@count = 0
end
def increment
@count += 1
end
def decrement
@count -= 1
end
end
c = Counter.new
t1 = Thread.start { 100_0000.times { c.increment } }
t2 = Thread.start { 100_0000.times { c.increment } }
t1.join
t2.join
p c.count #200_0000
Run Code Online (Sandbox Code Playgroud)
我能够在每个线程中使用少得多的迭代次数来观察Java中的竞争条件.是不是我没有足够多次运行它来产生竞争条件,或者+/ -是Ruby中的线程安全?我使用的是ruby 2.0.0p247
Xcode 6.4中的Swift LLDB不打印框架内任何局部变量的值.这是我到目前为止所发现的.
(lldb) frame variable --no-args
cell = <Unable to determine byte size.>
(lldb) register read pc // get the current program counter value
rip = 0x00000001072177cf
(lldb) image lookup -va 0x00000001072177cf
Variable: id = {0x000a93a7}, name = "cell", type= "<NULL>", location = DW_OP_fbreg(-64), decl = CustomCollectionViewCell.swift:321
Run Code Online (Sandbox Code Playgroud)
好像它无法找到类型.
(lldb) image lookup -t CustomCollectionViewCell
(lldb)
Run Code Online (Sandbox Code Playgroud)
我可以看到包含该类型的图像已加载.
(lldb) image list
...
/path/to/CustomFramework
Run Code Online (Sandbox Code Playgroud)
此外,该文件还包含符号.
$strings /path/to/CustomFramework | grep CustomCollectionViewCell
_TtC17CustomFramework24CustomCollectionViewCell
Run Code Online (Sandbox Code Playgroud)
我似乎有调试符号,因为:
(lldb) script lldb.target.module['/data/Containers/Bundle/Application/23A3AF01-4F87-439F-B2F7-36E7BD09390B/MyApp.app/Frameworks/CustomFramework.framework/CustomFramework'].GetNumCompileUnits()
74
Run Code Online (Sandbox Code Playgroud)
另外,编译的swift文件的目标文件具有变量和类型.
$ dwarfdump /path/to/FileUsingCell.o
0x00003140: TAG_variable [44] …Run Code Online (Sandbox Code Playgroud) 我有旧服务和新服务,上面有图片.我想将图像从旧迁移到新的.为此我需要从我的旧服务中获取图像,然后将其发布到新的sinatra服务.我能够以我想要使用NET :: HTTP的格式发布图像
url = URI.parse('http://0.0.0.0:9292/')
File.open("./Belgium.png") do |jpg|
req = Net::HTTP::Post::Multipart.new url.path, {"image" => UploadIO.new(jpg, "image/jpeg", "Belgium.jpg"), id:4234}
res = Net::HTTP.start(url.host, url.port) do |http|
http.request(req)
end
end
Run Code Online (Sandbox Code Playgroud)
我在Sinatra服务上用post post方法得到的参数是
params = {"image"=>
{:filename=>"Belgium.jpg", :type=>"image/jpeg",:name=>"image",:tempfile=> #<File:/var/folders/h7/f9cvygqs3lg78kwh24v8yzp80000gn/T/RackMultipart20120827-76057-1f3inkn>,
:head=> "Content-Disposition: form-data; name=\"image\";filename=\"Belgium.jpg\"\r\nContent-Length: 28228\r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n"},
"id"=>"4234"}
Run Code Online (Sandbox Code Playgroud)
但我无法用法拉第获得相同的结果.我尝试过使用类似的东西:
payload = { :profile_pic => Faraday::UploadIO.new('avatar.jpg', 'image/jpeg') }
Faraday.post 'http://0.0.0.0:9292/', payload
Run Code Online (Sandbox Code Playgroud)
在我的服务中我得到这样的东西:
"#<'UploadIO:0x007fb950373de8>"一个对象.这不是我期望的服务.
我怎样才能获得与Net :: HTTP相同的行为.我认为它已经用中间件做了一些事情,我试过调整,但我仍然是一个新手.
我正在研究一个简单的守护进程示例,并注意到以下命令在每次迭代时都会打印相同的时间值.
/bin/bash -c "while true; do echo `date`; sleep 1; done"
Run Code Online (Sandbox Code Playgroud)
输出:
Wed Dec 25 08:00:47 UTC 2013
Wed Dec 25 08:00:47 UTC 2013
Wed Dec 25 08:00:47 UTC 2013
Run Code Online (Sandbox Code Playgroud)
但是,如果我将上面的代码放在脚本中然后运行脚本,它会给出预期的输出.
#!/bin/bash
while true; do
echo `date`
sleep 1
done
Run Code Online (Sandbox Code Playgroud)
输出:
Wed Dec 25 08:02:58 UTC 2013
Wed Dec 25 08:02:59 UTC 2013
Wed Dec 25 08:03:00 UTC 2013
Run Code Online (Sandbox Code Playgroud)
这怎么可能?这是预期的产出吗?
我有一个Android应用程序发布到我的sinatra服务.之前我无法读取sinatra服务的参数.但是,在我将内容类型设置为"x-www-form-urlencoded"之后.我能够看到params,但不是我想要的.我得到的东西是我在sinatra服务中提出的请求.
{"{\"user\":{\"gender\":\"female\"},\"session_id\":\"7a13fd20-9ad9-45c2-b308-
8f390b4747f8\"}"=> nil, "splat"=>["update_profile.json"], "captures"=>["update_profile.json"]}
Run Code Online (Sandbox Code Playgroud)
这就是我从我的应用程序发出请求的方式.
StringEntity se;
se = new StringEntity(getJsonObjectfromNameValueList(params.get_params(), "user");
se.setContentType("application/x-www-form-urlencoded");
postRequest.setEntity(se);
private JSONObject getJsonObjectfromNameValueList(ArrayList<NameValuePair> _params, String RootName) {
JSONObject rootjsonObject = new JSONObject();
JSONObject jsonObject = new JSONObject();
if (_params != null) {
if (!_params.isEmpty()) {
for (NameValuePair p : _params) {
try {
if (p.getName().equals(ApplicationFacade.SESSION_ID))
rootjsonObject.put((String) p.getName(), (String) p.getValue());
else
jsonObject.put((String) p.getName(), (String) p.getValue());
} catch (JSONException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} …Run Code Online (Sandbox Code Playgroud)