我们正在与朋友一起开发一个有趣的项目,我们必须执行数百个 HTTP 请求,所有请求都使用不同的代理。想象一下,它类似于以下内容:
for (int i = 0; i < 20; i++)
{
HttpClientHandler handler = new HttpClientHandler { Proxy = new WebProxy(randomProxy, true) };
using (var client = new HttpClient(handler))
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "http://x.com"))
{
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
}
}
using (var request2 = new HttpRequestMessage(HttpMethod.Get, "http://x.com/news"))
{
var response = await client.SendAsync(request2);
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
}
}
}
} …Run Code Online (Sandbox Code Playgroud) 在启动我的应用程序时,我首先必须读取一些数据,必须初始化一些表格等等.在那个时候,用户看到只是灰色准备好显示某些表格.
这持续几秒钟......
我想到了一个Splash Screen,它将数据加载到一个单独的Thread中,并显示需要多长时间.或者只是一个状态栏?
你会怎么做这样的事情?
我正在使用C#.NET 3.5 + Winforms
最近我一直在用C++编写一个程序来ping三个不同的网站,然后根据通过或失败,它会在再次尝试之前等待5分钟或30秒.
目前我一直在使用ctime库和以下函数来处理我的等待.但是,根据我的CPU仪表,这是一个不可接受的解决方案.
void wait (int seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC;
while (clock () < endwait) {}
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案不可接受的原因是因为根据我的CPU仪表,程序在等待时运行在我CPU的48%到50%之间.我有一个Athlon 64 x2 1.2 GHz处理器.我的适度130线计划甚至不可能接近50%.
如何更好地编写我的等待函数,以便它只使用最少的资源?
我有一个使用的脚本Parallel::ForkManager.但是,即使在所有子进程完成后,wait_all_children()进程也需要非常长的时间.我知道的方法是打印一些时间戳(见下文).有谁知道可能导致这种情况的原因(我的机器上有16个CPU内核)?
my $pm = Parallel::ForkManager->new(16);
for my $i (1..16) {
$pm->start($i) and next;
... do something within the child-process ...
print (scalar localtime), " Process $i completed.\n";
$pm->finish();
}
print (scalar localtime), " Waiting for some child process to finish.\n";
$pm->wait_all_children();
print (scalar localtime), " All processes finished.\n";
Run Code Online (Sandbox Code Playgroud)
很明显,我会先得到Waiting for some child process to finish消息,比如说时间戳7:08:35.然后我会得到一个Process i completed消息列表,最后一个消息7:10:30.但是,All Processes finished直到7:16:33(!)我才收到消息.为什么在7:10:30到7:16:33之间有6分钟的延迟?谢谢!
我试图熟悉SCJP的Java线程,我有一个问题.
在下面编写的代码中,我简单地创建了:两个带有公共数据存储(一个数组)的Runnable和一个用于连续填充数据的同步write()方法,依次为每个Runnable(A和B)留下一个字母作为标记.
我知道代码很粗糙,可以更好地编写,但我正在寻求线程的道德.
所以现在当我运行它时,它永远不会终止,结果停在:
还好.A0.
但是,当我将wait()更改为wait(100)时,它可以正常计数从0到9并正常终止.有人可以解释一下这背后的原因吗?
谢谢.
public class ArrayThreads {
Object[] array = new Object[10];
boolean isA = true;
int position = 0;
int getIndex(){
return position;
}
class ThreadA implements Runnable{
synchronized void write(String value){
while(!isA){
try {
wait();
} catch (InterruptedException ex) {
System.out.println("An error in" + value);
ex.printStackTrace();
}
}
array[position] = value + position;
System.out.println(array[position]);
position++;
isA = !isA;
notify();
}
public void run() {
while(getIndex()<array.length){
if (getIndex()==9) return;
else
write("A");}
}
}
class ThreadB …Run Code Online (Sandbox Code Playgroud) 我需要在我的应用程序中等待一些事件,或者在做其他事情之前等待一段时间
我在vb6中尝试了这个伪代码
starttime=gettickcount
do
endtime=gettickcount
if endtime-starttime=>waittime then exit do
doevents()
loop
Run Code Online (Sandbox Code Playgroud)
但这似乎冻结了gui,我需要一种替代方法,等待没有freez gui
编辑我忘记了doevents,请注意
如何为wait_event_timeout函数设置超时= 1秒?功能:wait_event_timeout (wq,condition,timeout);
怎么能超时= 1秒.
如果调用函数那样: wait_event_timeout(queue,flag!='n',30*HZ);
超时= ???
我在linux中使用服务器套接字,我需要关闭它并在time_wait TCP状态到期之前重新打开.我在绑定之前设置了服务器套接字的重用地址选项,但它仍然抛出BindException.我也试过这个http://meteatamel.wordpress.com/2010/12/01/socket-reuseaddress-property-and-linux/,但它仍然不起作用.
要打开服务器套接字我使用:
ServerSocket ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(12345));
Run Code Online (Sandbox Code Playgroud)
并关闭:
ss.close();
Run Code Online (Sandbox Code Playgroud)
"已经在使用的地址"BindException在绑定调用时被抛出.
此代码生成异常:
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
final ServerSocket ss = new ServerSocket();
ss.setReuseAddress(true);
ss.bind(new InetSocketAddress(12345));
Socket s = ss.accept();
System.out.println((char) s.getInputStream().read());
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Thread.sleep(500);
Socket s = new Socket("localhost", 12345);
s.getOutputStream().write('c');
}
Run Code Online (Sandbox Code Playgroud) 我有一个测试代码wait(timeout).
public static void main(String[] args) throws Exception
{
Runnable r = new Runnable()
{
public void run()
{
while (true)
{
int random = (int)(Math.random() * 10);
synchronized(this)
{
try
{
wait(random);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.out.println(random);
}
}
};
new Thread(r).start();
}
Run Code Online (Sandbox Code Playgroud)
但是,这似乎不能正常工作,理想情况下它应该等待random方法给出的特定时间并打印出来.但每次打印几次后都会停止(随机次数).
无法确定问题所在.
我试图通过设置 tcp_fin_timeout 来减少连接处于 TIME_WAIT 状态的时间,详细信息如下:
root:~# sysctl -w net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_fin_timeout = 30
Run Code Online (Sandbox Code Playgroud)
但是,此设置似乎不会产生任何影响。当我查看机器的 netstat 时,连接仍然等待默认的 60 秒:
root:~# watch netstat -nato
tcp 0 0 127.0.0.1:34185 127.0.0.1:11209 TIME_WAIT timewait (59.14/0/0)
tcp 0 0 127.0.0.1:34190 127.0.0.1:11209 TIME_WAIT timewait (59.14/0/0)
Run Code Online (Sandbox Code Playgroud)
我有什么遗漏的吗?该机器运行的是 Ubuntu 14.04.1。
time-wait ×10
java ×3
c# ×2
wait ×2
.net-core ×1
c ×1
c++ ×1
httpclient ×1
linux-kernel ×1
perl ×1
progress-bar ×1
serversocket ×1
tcp ×1
timeout ×1
vb6 ×1
winforms ×1